方式一:把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