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>