MyBatis之foreach
foreach
foreach 元素是非常强大的,它允许你指定一个集合,声明集合项和索引变量,它们可以用在元素体内。它也允许你指定开放和关闭的字符串,在迭代之间放置分隔符。这个元素是很智能的,它不会偶然地附加多余的分隔符。
注意 你可以传递一个 List 实例或者数组作为参数对象传给 MyBatis。当你这么做的时候,MyBatis 会自动将它包装在一个 Map 中,用名称在作为键。List 实例将会以“list” 作为键,而数组实例将会以“array”作为键。
属性 |
描述 |
item |
循环体中的具体对象。支持属性的点路径访问,如item.age,item.info.details。 具体说明:在list和数组中是其中的对象,在map中是value。 该参数为必选。 |
collection |
要做foreach的对象,作为入参时,List<?>对象默认用list代替作为键,数组对象有array代替作为键,Map对象用map代替作为键。 当然在作为入参时可以使用@Param("keyName")来设置键,设置keyName后,list,array,map将会失效。 除了入参这种情况外,还有一种作为参数对象的某个字段的时候。举个例子: 如果User有属性List ids。入参是User对象,那么这个collection = "ids" 如果User有属性Ids ids;其中Ids是个对象,Ids有个属性List id;入参是User对象,那么collection = "ids.id" 上面只是举例,具体collection等于什么,就看你想对那个元素做循环。 该参数为必选。 |
separator |
元素之间的分隔符,例如在in()的时候,separator=","会自动在元素中间用“,“隔开,避免手动输入逗号导致sql错误,如in(1,2,)这样。该参数可选。 |
open |
foreach代码的开始符号,一般是(和close=")"合用。常用在in(),values()时。该参数可选。 |
close |
foreach代码的关闭符号,一般是)和open="("合用。常用在in(),values()时。该参数可选。 |
index |
在list和数组中,index是元素的序号,在map中,index是元素的key,该参数可选。 |
多数时候用foreach仅作为sql的inst<User> selIn(@Param("alsit") List<Integer> list);
<select id="selIn" resultType="User"> select * from user <where> id in <foreach collection="alsit" open="(" separator="," close=")" item="item"> #{item} </foreach> </where> </select> @Test public void selectIn() throws IOException { SqlSession session = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis.xml")).openSession(); UserMapper mapper = session.getMapper(UserMapper.class); List<Integer> integerList = new ArrayList<>(); integerList.add(1); integerList.add(2); integerList.add(3); integerList.add(4); integerList.add(5); List<User> userList = mapper.selIn(integerList); for (User user : userList) { System.out.println(user); } }
foreach之map的使用
当foreach的collection为map的时候,它的index就位map的key,value为值
int insUser(@Param("map") Map<Integer,User> map); <insert id="insUser"> insert into user values <foreach collection="map" item="item" index="key" separator=","> (default,#{item.userCode},#{item.userPassword}) </foreach> </insert> @Test public void insertUser() throws IOException { SqlSession session = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis.xml")).openSession(); UserMapper mapper = session.getMapper(UserMapper.class); User u1 = new User(0,"a","a"); User u2= new User(0,"a1","a"); User u3 = new User(0,"a2","a"); User u4 = new User(0,"a3","a"); Map<Integer, User> map = new HashMap<>(); map.put(1,u1); map.put(2,u2); map.put(3,u3); map.put(4,u4); int result = mapper.insUser(map); if (result > 0) { System.out.println("success"); session.commit(); } else { System.out.println("failed"); session.rollback(); } }
使用foreach遍历list对象,对象里面又包含listd的嵌套查询
List<User> selIn(@Param("list") List<Entry> list);
<select id="selIn" resultType="User">
select * from user
<where>
userCode in
<foreach collection="list" item="item">
<foreach collection="item.list" open="(" separator="," close=")" item="son">
#{son.userCode}
</foreach>
</foreach>
</where>
</select>
实体
package cn.arebirth.pojo;
import java.util.List;
public class Entry {
int id;
List<User> list;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public List<User> getList() {
return list;
}
public void setList(List<User> list) {
this.list = list;
}
}
@Test
public void selectIn() throws IOException {
SqlSession session = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis.xml")).openSession();
UserMapper mapper = session.getMapper(UserMapper.class);
User u1 = new User(0, "a", "a");
User u2 = new User(0, "a1", "a");
User u3 = new User(0, "a2", "a");
User u4 = new User(0, "a3", "a");
List<User> addUser = new ArrayList<>();
addUser.add(u1);
addUser.add(u2);
addUser.add(u3);
addUser.add(u4);
Entry entry = new Entry();
entry.setId(1);
entry.setList(addUser);
List<Entry> entryList = new ArrayList<>();
entryList.add(entry);
List<User> userList = mapper.selIn(entryList);
for (User user : userList) {
System.out.println(user);
}
}
---恢复内容结束---
a rebirth

浙公网安备 33010602011771号