首页 > 代码库 > MyBatis通过map传入多个参数

MyBatis通过map传入多个参数

需求: 需要通过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 id="ifExistRoomQtId" parameterType="map" resultType="int" >
        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"

MyBatis通过map传入多个参数