MyBatis In的使用

     在项目中where条件中用到in,我理所当然传了字符串,但是并不能达到理想的效果,百度了一下,找到如下解决方法,作为笔记记录一下。。。

           1.解决方法(多参数)

               Map.xml

              

[html]  view plain  copy
 print ?
  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
 print ?
  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
 print ?
  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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值