一、foreach 属性使用
<foreach collection="list" index="index" item="mchntcd" open="(" close=")" separator=",">
#{mchntcd}
</foreach>- item: 集合中元素迭代时的别名,该参数为必选,通过别名进行取值
- index:在list和数组中,index是元素的序号,在map中,index是元素的key,非必填
- open: foreach代码的开始符号,一般是(和close=")"合用。常用在in(),values()时。非必填
- separator:元素之间的分隔符,例如在in()的时候,separator=","会自动在元素中间用“,“隔开,避免手动输入逗号导致sql错误,如in(1,2,)这样。非必填
- close: foreach代码的关闭符号,一般是)和open="("合用。常用在in(),values()时。非必填
- collection: 要做foreach的对象,作为入参
- 传入是集合,也就是接口里面用的 list<string> namelist 那么 使用 collection = “list”
- 传入的是数组,接口里面 string[] namestrs ,那么 使用 collection = “array”
- 如果传入的是一个实体bean,实体bean里面有一个属性为 list<string> ids 那么collection = “ids ”,如果实体bean中对应ids是一个对象,ids 里面有一个属性为 list<stirng> usernames,那么collection = “ids.usernames”
二、代码使用
1、实体类 list<string> mchntcds
mapper接口 ---》 list方式
int querydiscderatecount(list<string> mchntcds);
mapper映射xml:
<select id="querydiscderatecount" resulttype="integer">
select count(*) from t_mchnt_disc_config where mchnt_cd in
<foreach collection="list" index="index" item="mchntcd" open="(" close=")" separator=",">
#{mchntcd}
</foreach>
</select>2、实体类 list<user> userlist,实体类中有 list 属性
实体类:
@data
public class mchntdiscderatedto {
private string mchntcd = "";
}mapper接口
list<tmchntderateinfodto> getdiscderatelist(list<mchntdiscderatedto> discdto);
mapper映射xml:
<select id="getdiscderatelist" parametertype="mchntdiscderatedto" resulttype="tmchntderateinfodto">
select
t_mchnt_disc_derate_config
where
mchnt_cd in
<foreach collection="list" index="index" item="user" open="(" close=")" separator=",">
#{user.mchntcd}
</foreach>
</select>3、数组 string[] params
用 array
mapper 接口
int querydiscderatecount(string[] mchntcds);
mapper映射xml:
<select id="querydiscderatecount" resulttype="integer">
select count(*) from t_mchnt_disc_derate_config where mchnt_cd in
<foreach collection="array" index="index" item="mchntcd" open="(" close=")" separator=",">
#{mchntcd}
</foreach>
</select>4、传入的参数是实体类,并且实体中包含数组和集合
实体类:
@data
public class uservo {
private long id;
private long supplierid;
private long[] ids;
private list<long> clientidlist;
}mapper接口
list<uservo> querylist(uservo select);
mapper映射文件xml
<select id="querylist" resulttype="uservo" parametertype="uservo">
select *
from bms_bills_memo
<where>
and id in
<foreach collection="ids" open="(" close=")" item="id" separator=",">
#{id}
</foreach>
and
client_id in
<foreach collection="clientidlist" separator="," item="detail" open="(" close=")" >
#{detail}
</foreach>
</where>
</select>5、传入多个list或者array,不使用实体进行封装。用注解@params, collection使用到param中定义的别名
mapper接口
list<uservo> querylist(@param("idarray") long[] array, @param("clientidlist") list<long> list);mapper映射文件xml
<select id="querylist" resulttype="uservo">
select *
from t_user_inf
<where>
and id in
<foreach collection="idarray" open="(" close=")" item="id" separator=",">
#{id}
</foreach>
and
client_id in
<foreach collection="clientidlist" separator="," item="detail" open="(" close=")" >
#{detail}
</foreach>
</where>
</select>6、map参数
当我们传入的参数为 map<string,objject>的时候,那么能不能正常使用 foreach呢,答案是肯定的,因为对象也是类似于map结构啊
/**
* map 获取数据
* @param map
* @return
*/
list<sysuser> getuserbyids(map<string,object> map);
// xml
<select id="getuserbyids" resultmap="baseresultmap">
select
<include refid="base_column_list" />
from t_sys_user
where status = #{status}
and id in
<foreach collection="ids" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</select>实际调用:
@test
public void testmapusers() {
map<string,object> params = new hashmap<>();
params.put("status", "1");
params.put("ids", arrays.aslist(1,2,3,4,5,6,7,8));
list<sysuser> all = sysuserdao.getuserbyids(params);
try {
system.out.println(new jsonmapper().writevalueasstring(all));
} catch (jsonprocessingexception e) {
e.printstacktrace();
}
}调用结果:

三、总结
1、mapper 接口中添加 @param注解的场合,list,array将会失效;
2、使用了 @param注解 collection的内容需要使用param中的别名来指定你需要用到的list或者array
扩展:mybatis中foreach的使用
首先我们要明白的是foreach的本质就是把数据库能执行的sql在xml中按照一定语法来进行拼接,所以拼接之前,我们了解一下foreach标签中几个常见元素的作用
1.collection
list或array:如果传入的参数类型是list或array,collection属性的默认值分别是list和array。如果需要自定义集合名称。
map:如果传入的参数是map,collection属性可以指定遍历map的keys、values或entryset
2.item
集合遍历中每一个元素的别名
3.open
拼接sql时最前面拼接的字符串
4.separator
拼接sql时候两个元素之间的分隔字符串
5.close
拼接sql时最后面拼接的字符串
6.index
index:在list或array中,index为元素的序号索引;在map中,index为遍历元素的key值。
举一个简单的例子
一个简单的sql
select * from blog where title is not null and (id=1 or id=2 or id=3)
1.我们使用map集合作为参数实现拼接
<select id="queryblogforeach" parametertype="map" resulttype="blog"> select * from blog <where> title is not null <foreach collection="ids" item="id" open="and (" separator="or" close=")"> id=#{id} </foreach> </where> </select>2.我们使用list集合作为参数实现拼接
<select id="queryblogforeach2" parametertype="list" resulttype="blog"> select * from blog <where> title is not null <foreach collection="list" item="id" open="and (" separator="or" close=")"> id=#{id} </foreach> </where> </select>到此这篇关于mybatis中foreach的使用的文章就介绍到这了,更多相关mybatis foreach使用内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论