mybatis的foreach官方文档解释:对一个集合进行遍历,通常是在构建IN条件语句的时候使用。例如
<select id="selectPostIn" resultType="domain.blog.Post">
SELECT *
FROM POST P
WHERE ID in
<foreach item="item" index="index" collection="list" open="(" separator="," close=")">
#{item}
</foreach>
</select>
foreach 元素的功能非常强大,它允许你指定一个集合,声明可以在元素体内使用的集合项(item)和索引(index)变量。它也允许你指定开头与结尾的字符串以及在迭代结果之间放置分隔符。这个元素是很智能的,因此它不会偶然地附加多余的分隔符。
注意:
你可以将任何可迭代对象(如 List、Set 等)、Map 对象或者数组对象传递给 foreach 作为集合参数。当使用可迭代对象或者数组时,index 是当前迭代的次数,item 的值是本次迭代获取的元素。当使用 Map 对象(或者 Map.Entry 对象的集合)时,index 是键,item 是值。
foreach元素的属性主要有 item,index,collection,open,separator,close。
item表示集合中每一个元素进行迭代时的别名,
index指 定一个名字,用于表示在迭代过程中,每次迭代到的位置,
open表示该语句以什么开始,
separator表示在每次进行迭代之间以什么符号作为分隔 符,
close表示以什么结束。
在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况 下,该属性的值是不一样的,主要有一下3种情况:
1. 如果传入的是单参数且参数类型是一个List的时候,collection属性值为list
2. 如果传入的是单参数且参数类型是一个Array数组的时候,collection的属性值为array
3. 如果传入的是Param修饰(@Param("ids") List<String> ids)的时候,collection的属性值为ids
4. 如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可
以封装成map,实际上如果你在传入参数的时候,在breast里面也是会把它封装成一个Map的,map的key就是参数名,所以这个时候collection属性值就是传入的List或array对象在自己封装的map里面的key 下面分别来看看上述三种情况的示例代码:
单参数List的类型:
java:
public int dynamicForeachList(List<String> ids);
<!--错误写法:注意观察collection中参数的写法-->
<delete id="dynamicForeachList">
DELETE FROM s_user
WHERE id in
<foreach item="ids" collection="ids" open="(" separator=","close=")">
#{ids}
</foreach>
</delete>
<!--正确写法-->
<delete id="dynamicForeachList">
DELETE FROM s_user
WHERE id in
<foreach item="ids" collection="list" open="(" separator=","close=")">
#{ids}
</foreach>
</delete>
你也可以使用@Param(“ids”) 来指定参数名称,这样就可以用自己的list名称了,如以下写法正确
java:
public int dynamicForeachList(@Param("ids")List<String> ids);
sql:
<delete id="dynamicForeachList">
DELETE FROM s_user
WHERE id in
<foreach item="ids" collection="ids" open="(" separator=","close=")">
#{ids}
</foreach>
</delete>
这样则正确。.
单参数array数组的类型:
同样注意collection属性的值:array
java:
public List dynamicForeachArray(int[] ids);
sql:
<select id="dynamicForeachArray" parameterType="java.util.ArrayList" resultType="Blog">
select * from t_blog where id in
<foreach collection="array" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>
自己把参数封装成Map的类型
java
public List dynamicForeachMap(Map params);
sql
<select id="dynamicForeachMap" parameterType="java.util.HashMap" resultType="Blog">
select * from t_blog where title like "%"#{title}"%" and id in
<foreach collection="ids" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>
注意:上面说的是传入参数是单个的List、Array、Map,如果是 传入的一个 对象或者 map, 其中有个属性是 list 或者array,那么colletion=“属性名”。
java:
class example{
List<String> subCodes;
}sql:
<!--传递的参数是对象-->
<if test="subCodes != null">
and s.Code in
<foreach collection="subCodes" item="subCode" open="(" close=")" separator=",">
${subCode}
</foreach>
</if>
其他例子:
1、List<Integer> IntList 、 List<String> strList 集合中存的是基本类型的
<select id="dynamicForeachTest" parameterType="java.util.List" resultMap="Users">
select id,name from t_blog where id in
<foreach collection="list" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>
其中的传的参数是List集合所以collection 的值直接使用了list 来代替,而index 就是这个集合遍历的索引值,item 就是list当前索引位置的值 open 和close 成对出现,就是开始和结束的符号,而separator 就是分隔符。
2、List<Obect> objList 、List<Users> userList 引用类型的数据
package soufun.com;
public class Users {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
以这个类为例来看看:
java:
public int dynamicForeachList(List<Users> uusers);
<select id="dynamicForeachTest" parameterType="java.util.List" resultMap="Users">
select id,name from t_blog where id in
<foreach collection="list" index="index" item="item" open="(" separator="," close=")">
#{item.id}
</foreach>
</select>
循环插入
insert into t_blog(id,name) values
<foreach collection="list" item="item" index="index" separator=",">
(#{item.id},#{item.name})
</foreach>
List 和array 是相似的。
下面我们看看map的遍历:
HashMap<String,String> foreachMap= new HashMap<String,String>();
map.put("name1", "www1");
map.put("name2", "www2");
map.put("name3", "www3");
map.put("name4", "www4");
map.put("name5", "www5");
map.put("name6", "www6");
sqlSession.selectList(" getByMap", foreachMap);
<select id="getByMap" resultMap="Users" >
SELECT * FROM t_blog where
<foreach collection="foreachMap" index="key" item="value" separator="or" >
name=#{value}
</foreach>
</select>
其中的foreachMap 就是传递的Map集合 index 就是map的key item 就是map的value
还可以使用如下的方式对map 进行遍历:
1、循环key:
<foreach collection="condition.keys" item="k" separator="and">
${k} = #{k}
</foreach>
2、循环values
<foreach collection="condition.values" item="v" separator="and">
${v} = #{v}
</foreach>
3、 循环获取key和值:
<foreach collection="condition.keys" item="k" separator="and">
<if test="null != condition[k]">
${k} = ${condition[k]}
</if>
</foreach>
${k} = #{condition[${k}]} 是不太好用在mybatis3.3下 修改成 ${k} = #{condition.${k}} 是好用的
到这里都结束了,但还有个小事说一下,那就是in 的那个遍历,一般我们都是使用foreach 来组装 的但是有看到这样写的:
String name ="'w1','w2','w3','w4'";
<select id="getByMap" resultMap="Users">
SELECT * FROM t_blog where name in (${name})
</select>
这就是自己组装了 in的字符串,而没有使用mybatis的foreach 来组装其实是一样的。
转载自:https://blog.csdn.net/zzhongcy/article/details/102659104