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);
}
}
---恢复内容结束---
MyBatis之foreach的更多相关文章
- mybatis map foreach遍历
mybatis map foreach遍历 转至http://www.cnblogs.com/yg_zhang/p/4314602.html mybatis 遍历map实例 map 数据如下 Map& ...
- mybatis中foreach的用法(转)
foreach一共有三种类型,分别为List,[](array),Map三种. foreach属性 属性 描述 item 循环体中的具体对象.支持属性的点路径访问,如item.age,item.inf ...
- mybatis的foreach标签
今天写sql发现了一点问题,乱弄了好久算是搞定了.关于mybatis的批量插入使用foreach插入形式为: insert into role_privilege( role_id, privileg ...
- mybatis之foreach用法
在做mybatis的mapper.xml文件的时候,我们时常用到这样的情况:动态生成sql语句的查询条件,这个时候我们就可以用mybatis的foreach了 foreach元素的属性主要有item, ...
- MyBatis 使用 foreach 批量插入
MyBatis 使用 foreach 批量插入 参考博文 老司机学习MyBatis之动态SQL使用foreach在MySQL中批量插入 使用MyBatis一次性插入多条数据时候可以使用 <for ...
- MyBatis的foreach查询(List、Array、Map)
mybatis 中 foreach collection的三种用法 foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合. foreach元素的属性主要有 item,index ...
- MyBatis map foreach
以下资料来源于网络,仅供参考学习. mybatis 遍历map实例 map 数据如下 Map<String,List<Long>>. 测试代码如下: public vo ...
- mybatis中foreach使用
mybatis中的<foreach collection="list" item="item" index="index" open= ...
- mybatis 中 foreach collection的三种用法(转)
文章转自 https://blog.csdn.net/qq_24084925/article/details/53790287 oreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集 ...
- mybatis的foreach写用法
一.mybatis查询 public abstract List<Model> findByIds(@Param("ids")List<Integer> i ...
随机推荐
- 黑马程序员_ADO.Net(ExecuteReader,Sql注入与参数添加,DataSet,总结DataSet与SqlDataReader )
转自https://blog.csdn.net/u010796875/article/details/17386131 一.执行有多行结果集的用ExecuteReader SqlDateReader ...
- flutter 如何实现文件读写(使用篇)
flutter文件读写可以对磁盘文件进行操作,实现某些业务场景,那么我们开始来讲下这个文件读写操作. 使用的库插件(package) dart:io(用于数据处理) path_provider (用于 ...
- Maven项目读取resources下文件的路径
要取编译后的路径,而不是你看到的src/main/resources的路径.如下: URL url = 类名.class.getClassLoader().getResource("conf ...
- 关于安装了git或者小乌龟(TortoiseGit)使用之后出现红色! 绿色√ 蓝色?的解决办法:
在当前目录中新建文件保存为(删除git信息.bat)在其写入:for /r . %%a in (.) do @if exist "%%a\.git" rd /s /q " ...
- C++11/14笔记
目录 语言层面 模板表达式中的空格 nullptr和std::nullptr_t 自动推导类型----auto 一致性初始化----Uniform Initialization 初始化列表(initi ...
- 数据结构-双向链表(Python实现)
数据结构在编程世界中一直是非常重要的一环,不管是开发还是算法,哪怕是单纯为了面试,数据结构都是必修课,今天我们介绍链表中的一种--双向链表的代码实现. 好了,话不多说直接上代码. 双向链表 首先,我们 ...
- JAVA面试题 浅析Java中的static关键字
面试官Q1:请说说static关键字,你在项目中是怎么使用的? static 关键字可以用来修饰:属性.方法.内部类.代码块: static 修饰的资源属于类级别,是全体对象实例共享的资源: 使用 s ...
- Java 源码学习系列(三)——Integer
Integer 类在对象中包装了一个基本类型 int 的值.Integer 类型的对象包含一个 int 类型的字段. 此外,该类提供了多个方法,能在 int 类型和 String 类型之间互相转换,还 ...
- springboot与springcloud的关系
1 . 问题描述 随着springboot.springcloud的不断迭代升级,开发效率不断提升,越来越多的开发团队加入到spring的大军中,今天用通俗的语言,介绍下什么是springboot,s ...
- 企业如何从“API优先”的策略中获益
在过去的几年里,全球API经济在以难以置信的速度进行快速地增长.物联网.人工智能.自动驾驶等等众多令人充满期待的技术正蓬勃发展,这也证明了API对于如今整个技术圈子的重要性,也预示着在不久的将来它还将 ...