mybatis foreach中集合的使用

方式一:把list或者数组包装到map中,传递到mapper文件中的参数是map

1、把数组包装到map中:

java代码:

Long[] id = {1,2};
Map queryMap = new HashMap();
queryMap.put("id", id);
getSqlSession().update(queryMap, "deleteConbineCode");

xml

<update id="deleteConbineCode" parameterType="map">
        DELETE from combine_code_normal
        where id in
        <foreach item="item" index="index" collection="id" open="("  separator="," close=")">  
            #{item}  
        </foreach>  
    </update>

注:parameterType中接受的是map类型,foreach中collection中是map中放的key。

2、把list<对象>包装到map中

java代码

<pre code_snippet_id="612746" snippet_file_name="blog_20150305_1_7164380" name="code" class="java" style="font-size: 14px;">Map tMap = new HashMap();tMap.put("id",1);tMap.put("id",2);
List<Map> list = new ArrayList<Map>();<pre code_snippet_id="612746" snippet_file_name="blog_20150305_1_7164380" name="code" class="java" style="font-size: 14px;"><pre name="code" class="javascript" style="font-size: 14px;">list.add(tMap);<pre name="code" class="javascript" style="font-size: 14px;">//上面构造了一个集合,集合里面是Map对象

 
 
Map queryMap = new HashMap();
queryMap.put("id",<span style="font-family: Arial, Helvetica, sans-serif;">list</span><span style="font-family: Arial, Helvetica, sans-serif;">);</span>
getSqlSession().update(queryMap, "deleteConbineCode");
 
 xml 

<update id="deleteConbineCode" parameterType="map">
        DELETE from combine_code_normal
        where id in
        <foreach item="item" index="index" collection="id" open="("  separator="," close=")">  
            #{item.id}  
        </foreach>  
    </update>
3、把list<基本类型>包装到map中

java代码

//上面构造了一个集合,集合里面是基本类型
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(3);
Map queryMap = new HashMap();
queryMap.put("id",list);
getSqlSession().update(queryMap, "deleteConbineCode");

xml

<update id="deleteConbineCode" parameterType="map">
        DELETE from combine_code_normal
        where id in
        <foreach item="item" index="index" collection="id" open="("  separator="," close=")">  
            #{item}  
        </foreach>  
    </update>



方式二:直接使用数组或者list作为参数传递到mapper文件
1、list方式:

java代码:

//构造了一个集合,集合里面是基本类型
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
List<Map> = getSqlSession().selectList(sqlName,list);
xml:

<select id="selectTotalImpression1"  resultType="java.util.Map">
	SELECT * FROM iams_adplacement_impression
	WHERE TO_DAYS(NOW()) - TO_DAYS(FROM_UNIXTIME(time)) >= 1
		AND adplacement_id in 
		<foreach collection="list" index="index" item="item" open="(" separator="," close=")">  
	            	#{item}   
	    	</foreach> 
</select>
2、数组
java代码:

//构造了一个数组,数组里面是基本类型
Integer[] ids = {1,2};
List<Map> = getSqlSession().selectList(sqlName,ids);
xml代码:

<select id="selectTotalImpression1"  resultType="java.util.Map">
	SELECT * FROM iams_adplacement_impression
	WHERE TO_DAYS(NOW()) - TO_DAYS(FROM_UNIXTIME(time)) >= 1
	AND adplacement_id in 
		<foreach collection="array" index="index" item="item" open="(" separator="," close=")">  
	            	#{item}   
	    	</foreach> 
</select>
注意:对于数组、list作为参数传给mapper时,parameterType可以不用写,直接在foreach中按下面要求指定即可

如果传入的是单参数且参数类型是一个List的时候,collection属性值为list .
如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array 









  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赶路人儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值