MyBatis In的使用

来源:互联网 发布:淘宝店如何退出放心淘 编辑:程序博客网 时间:2024/06/08 08:14

    项目中where条件中用到in,我理所当然的拼了个字符串传进去了,郁闷的是程序一直运行正常,测试case一直没有覆盖到这种情况,今天发现了,原来是程序的问题,我以为mybatis有bug呢。。。。。故记下此问题,留作笔记。

           


参考http://www.blogjava.net/xmatthew/archive/2011/08/31/355879.html


           1.解决方法(多参数)

               Map.xml

              

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <select id="getEntityList" resultType="App" parameterType="map">  
  2.     select * from t_app   
  3.     where status=#{status}  
  4.     <if test="flag!=null ">  
  5.         and id not in  
  6.         <foreach item="item" index="index" collection="ids" open="("  
  7.             separator="," close=")">  
  8.             #{item}  
  9.         </foreach>  
  10.     </if>  
  11. </select>  
          传入的参数为Map<String,Object>

          数据:status:1

                     ids:int[]{101,103,61,75}

 

         2.一个参数           

  •             a.如果参数的类型是List, 则在使用时,collection属性要必须指定为 list

                           findByIds(List<Long> ids)

                         

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <select id="findByIdsMap" resultMap="BaseResultMap">  
  2.   
  3.          Select  
  4.   
  5.          <include refid="Base_Column_List" />  
  6.   
  7.          from jria where ID in  
  8.                   <foreach item="item" index="index" collection="list"   
  9.                          open="(" separator="," close=")">  
  10.                         #{item}  
  11.                 </foreach>  
  12.   </select>   

  •            b.如果参数的类型是Array,则在使用时,collection属性要必须指定为 array

                     findByIds(Long[] ids)

                   

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <select id="findByIdsMap" resultMap="BaseResultMap">  
  2.                 select  
  3.                 <include refid="Base_Column_List" />  
  4.          from jria where ID in  
  5.                  <foreach item="item" index="index" collection="array"   
  6.                         open="(" separator="," close=")">  
  7.                        #{item}  
  8.                </foreach>  
  9.  </select>   
0 0
原创粉丝点击