mybatis中foreach的基本使用

1.<foreach collection="" index="" item="" open="" separator="" close=""></foreach>

其中属性:

collection:此属性必须指定 且有三种形式,如果是传的单参数数组 则在属性中填array,如果传的是单参数集合List 

则在属性中填list  ,如果传的是Map或者实体 则填响应的key值  。

index:表示迭代过程中 每次迭代的位置 

item:集合中每个元素进行迭代时的名称

open:该语句以什么开始

separator:每次迭代之间用什么分隔符

close:表示以什么结束

2.实例

2.1 数组的形式 传递参数为:int[] ids

<update id="updateUserStatusByCustomerId" parameterType="java.util.ArrayList">
  update ll_user.userlogin set status=1 where customer_id in
  <foreach collection="array" index="index" item="id" open="(" separator="," close=")">
  #{id}
  </foreach>
  </update>

2.3 Map的形式  参数的封装:

Map map = new HashMap<String,Object>();
 map.put("status", status);
map.put("ids", id);

mapper.xml中的书写:

<update id="updateUserStatusofMap" parameterType="java.util.HashMap">
  update ll_user.userlogin set status=#{status} where customer_id in
  <foreach collection="ids" index="index" item="id" open="(" separator="," close=")">
  #{id}
  </foreach>
  </update>

3.List集合的形式  List<Integer> ids

<update id="updateUserStatusofList" parameterType="java.util.List">
  update ll_user.userlogin set status=1 where customer_id in
  <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
  #{item}
  </foreach>
  </update>


  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值