当传入的参数为多个参数时

1 可以不封装为Javabean直接传入,写法如下

 

public List<XXXBean> getXXXBeanList(String xxId, String xxCode);

<select >

  select t.* from tableName where id = #{0} and name = #{1}

</select>

由于是多参数那么就不能使用parameterType, 改用#{index}是第几个就用第几个的索引,索引从0开始

 也可以在dao接口中使用@Param参数

List <SysRole>  selectR o lesByUseridAndRoleEnabled(@Param ("userId ")  Long  userId ,@Param ("enabled ")  Integer  enabled);

就可以直接用#{userId}代替#{0} #{enabled}代替#{1}

 

 

2 可以封装成Javabean传入

 

 

3 可以封装为  Array

<select >  
    select * from t_table where id in  
    <foreach collection="array" index="index" item="id" open="(" separator="," close=")">  
        #{id}  
    </foreach>  
</select> 
 

4 可以封装为  list 

<select >  
    select * from t_table where id in  
    <foreach collection="list" index="index" item="id" open="(" separator="," close=")">  
        #{id}  
    </foreach>  
</select> 

5 可以封装为 Map

<select >  
    select * from t_table where id in  
    <foreach collection="ids" index="index" item="id" open="(" separator="," close=")">  
        #{id}  
    </foreach>  
</select> 

   这里的ids指的的是你放入map里面的key

 

  map中也可以放list,如下

    需求: 需要通过map传入一个list和一个String类型的参数,而且没有封装成Javabean

      实现

      service层

 

    // 根据roomId查询所有的roomQtId
    public int queryRoomQtIdByRoomId(ArrayList<Long> roomIdList, String meetingId) {
        List<Long> roomQtIdlist = hotelRoomDao2.queryRoomQtIdByRoomId(roomIdList);
        // 根据roomQtId查询是否已经录入了房间了
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("roomQtIdlist", roomQtIdlist);
        map.put("meetingId", meetingId);
        int i = hotelRoomDao2.ifExistRoomQtId(map);
        return i;
    }

 

 

 

Dao接口

 

//查询房间是否已经录入了房间号
    int ifExistRoomQtId(Map<String, Object> map);

 

 

 

xml

 

    <!-- 查询房间是否已经录入了房间号 -->
    <select  >
        SELECT COUNT(1) FROM PM.T_MEETING_ROOM_ARRANGE
        WHERE MEETING_ID =
        #{meetingId} AND ROOM_QT_ID IN
        <foreach collection="roomQtIdlist" item="item" index="index" open="("
            close=")" separator=",">
            #{item}
        </foreach>
    </select>

 

 

 

 

 

过程中遇到bug

 

MyBatis:The expression 'list' evaluated to a null value

 

原因:直接赋值,将

 

 <foreach collection="roomQtIdlist" item="item" index="index" open="("
            close=")" separator=",">
            #{item}
  </foreach>
中的
 

collection="roomQtIdlist"
写为了
 

collection="list"

 

 

  

相关文章:

  • 2022-01-17
  • 2022-12-23
  • 2021-09-25
  • 2021-05-16
  • 2021-11-07
  • 2022-02-23
  • 2021-06-15
猜你喜欢
  • 2021-12-06
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-03-05
相关资源
相似解决方案