我是靠谱客的博主 温婉西牛,最近开发中收集的这篇文章主要介绍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 









最后

以上就是温婉西牛为你收集整理的mybatis foreach中集合的使用的全部内容,希望文章能够帮你解决mybatis foreach中集合的使用所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(50)

评论列表共有 0 条评论

立即
投稿
返回
顶部