zoukankan      html  css  js  c++  java
  • MyBatis 多参问题

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

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

    public List<XXXBean> getXXXBeanList(String xxId, String xxCode);
    
    <select id="getXXXBeanList" resultType="XXBean">
    
      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 id="queryPojoById" parameterType="String[]" resultMap="pojo">  
        select * from t_table where id in  
        <foreach collection="array" index="index" item="id" open="(" separator="," close=")">  
            #{id}  
        </foreach>  
    </select> 
     

    4 可以封装为  list 

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

    5 可以封装为 Map

    <select id="queryPojoById" parameterType="map" resultMap="pojo">  
        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 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"

      

  • 相关阅读:
    洛谷1098 字符串的展开 解题报告
    洛谷1086 花生采摘 解题报告
    洛谷1125 笨小猴 解题报告
    洛谷1056 排座椅 解题报告
    洛谷1067 多项式输出 解题报告
    洛谷1003 铺地毯 解题报告
    JS在不同js文件中互相调用
    JS鼠标滚轮事件
    JS闭包
    PHP中header('content-type:text/html;charset="utf-8')和error_reporting()的作用
  • 原文地址:https://www.cnblogs.com/miye/p/7066157.html
Copyright © 2011-2022 走看看