MyBatis In的使用

http://blog.csdn.net/unei66/article/details/17792503

 MyBatis In的使用
标签: mybatisin
2014-01-03 16:23 7454人阅读 评论(0) 收藏 举报
 分类: Java Web(12)  
版权声明:本文为博主原创文章,未经博主允许不得转载。
目录(?)[+]
             项目中where条件中用到in,我理所当然的拼了个字符串传进去了,郁闷的是程序一直运行正常,测试case一直没有覆盖到这种情况,今天发现了,原来是程序的问题,我以为mybatis有bug呢。。。。。故记下此问题,留作笔记。
           
           1.解决方法(多参数)
               Map.xml
              
[html] view plaincopy在CODE上查看代码片派生到我的代码片
<select id="getEntityList" resultType="App" parameterType="map">  
    select * from t_app   
    where status=#{status}  
    <if test="flag!=null ">  
        and id not in  
        <foreach item="item" index="index" collection="ids" open="("  
            separator="," close=")">  
            #{item}  
        </foreach>  
    </if>  
</select>  
          传入的参数为Map<String,Object>
          数据:status:1
                     ids:int[]{101,103,61,75}
 
         2.一个参数           
            a.如果参数的类型是List, 则在使用时,collection属性要必须指定为 list
                           findByIds(List<Long> ids)
                         
[html] view plaincopy在CODE上查看代码片派生到我的代码片
<select id="findByIdsMap" resultMap="BaseResultMap">  
  
         Select  
  
         <include refid="Base_Column_List" />  
  
         from jria where ID in  
                  <foreach item="item" index="index" collection="list"   
                         open="(" separator="," close=")">  
                        #{item}  
                </foreach>  
  </select>   

           b.如果参数的类型是Array,则在使用时,collection属性要必须指定为 array
                     findByIds(Long[] ids)
                   
[html] view plaincopy在CODE上查看代码片派生到我的代码片
<select id="findByIdsMap" resultMap="BaseResultMap">  
                select  
                <include refid="Base_Column_List" />  
         from jria where ID in  
                 <foreach item="item" index="index" collection="array"   
                        open="(" separator="," close=")">  
                       #{item}  
               </foreach>  
 </select>   
原文地址:https://www.cnblogs.com/jcz1206/p/5038484.html