MyBatis动态SQL foreach标签实现批量插入
需求:查出给定id的记录:
- <select id="getEmpsByConditionForeach" resultType="com.test.beans.Employee">
- SELECT * FROM tb1_emplyee WHERE id IN
- <foreach collection="list" item="item_id" separator="," open="(" close=")">
- #{item_id}
- </foreach>
- </select>
关于foreach标签,有几个属性应该注意一下:
- collection:指定要遍历的集合:
- list类型的参数会特殊处理封装在map中,map的key就叫list
- item:将当前遍历出的元素赋值给指定的变量
- separator:每个元素之间的分隔符
- open:遍历出所有结果拼接一个开始的字符
- close:遍历出所有结果拼接一个结束的字符
- index:索引。遍历list的时候是index就是索引,item就是当前值
- 遍历map的时候index表示的就是map的key,item就是map的值
- #{变量名}就能取出变量的值也就是当前遍历出的元素
测试方法:
- @Test
- public void testDynamicSqlTest() throws IOException{
- SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
- //1、获取到的SqlSession不会自动提交数据
- SqlSession openSession = sqlSessionFactory.openSession();
- try
- {
- EmployeeMapperDymanicSQL mapper=openSession.getMapper(EmployeeMapperDymanicSQL.class);
- /*Employee employee=new Employee(1,"lili",null,"1");*/
- List<Employee> emps=mapper.getEmpsByConditionForeach(Arrays.asList(1,2,3,4));
- for (Employee e:emps){
- System.out.println(e);
- }
- }
- finally {
- openSession.close();
- }
- }
foreach标签也可以实现实现批量插入(删除)数据:
这里以批量插入数据为例:
- <insert id="addEmps">
- INSERT INTO tb1_emplyee(last_name,email,gender,d_id)
- VALUES
- <foreach collection="emps" item="emp" separator=",">
- (#{emp.lastName},#{emp.email},#{emp.gender},#{emp.dept.id})
- </foreach>
- </insert>
对应的接口:
- public void addEmps(@Param("emps")List<Employee> emps);
测试方法
- @Test
- public void testBatchSave() throws IOException{
- SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
- //1、获取到的SqlSession不会自动提交数据
- SqlSession openSession = sqlSessionFactory.openSession();
- try
- {
- EmployeeMapperDymanicSQL mapper=openSession.getMapper(EmployeeMapperDymanicSQL.class);
- List<Employee> emps=new ArrayList<Employee>();
- emps.add(new Employee(null,"Eminem","Eminem@126.com","1",new Department(1)));
- emps.add(new Employee(null,"2Pac","2Pac@126.com","1",new Department(1)));
- mapper.addEmps(emps);
- openSession.commit();
- }
- finally {
- openSession.close();
- }
- }
MyBatis动态SQL foreach标签实现批量插入的更多相关文章
- mybatis动态sql foreach的用法
<select id="findUserByIds" parameterType="com.pojo.QueryVo" resultType=" ...
- Java EE数据持久化框架 • 【第4章 MyBatis动态SQL】
全部章节 >>>> 本章目录 4.1 MyBatis动态标签 4.1.1 MyBatis动态标签介绍 4.1.2 < if >标签 4.1.3 update语 ...
- mybatis动态sql和分页
mybatis动态sql foreach BookMapper.xml <select id="selectBooksIn" resultType="com.lin ...
- MyBatis从入门到精通(第4章):MyBatis动态SQL【foreach、bind、OGNL用法】
(第4章):MyBatis动态SQL[foreach.bind.OGNL用法] 4.4 foreach 用法 SQL 语句中有时会使用 IN 关键字,例如 id in (1,2,3).可以使用 ${i ...
- 9.mybatis动态SQL标签的用法
mybatis动态SQL标签的用法 动态 SQL MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句有多么 ...
- MyBatis基础入门《二十》动态SQL(foreach)
MyBatis基础入门<二十>动态SQL(foreach) 1. 迭代一个集合,通常用于in条件 2. 属性 > item > index > collection : ...
- Mybatis动态SQL单一基础类型参数用if标签
Mybatis动态SQL单一基础类型参数用if标签时,test中应该用 _parameter,如: 1 2 3 4 5 6 <select id="selectByName" ...
- MyBatis动态SQL之一使用 if 标签和 choose标签
bootstrap react https://segmentfault.com/a/1190000010383464 xml 中 < 转义 to thi tha <if test=&qu ...
- 超全MyBatis动态SQL详解!( 看完SQL爽多了)
MyBatis 令人喜欢的一大特性就是动态 SQL. 在使用 JDBC 的过程中, 根据条件进行 SQL 的拼接是很麻烦且很容易出错的. MyBatis 动态 SQL 的出现, 解决了这个麻烦. My ...
随机推荐
- BZOJ4816 Sdoi2017数字表格
一开始只推出O(TN)的做法,后来看了看发现再推一步就好了. 我们只需要枚举gcd就可以啦. 然后我们改变一下枚举顺序 设T为dk 预处理中间那部分前缀积就好了. #include<bits/s ...
- 回忆Ajax ๑乛◡乛๑
东西越多,记不完,也记不住,笔记是最好的记忆了. 回顾以前的ajax的写法,简单封装一个ajax. //data = { // url: "url", // method: &qu ...
- 【BZOJ-3218】a+b Problem 最小割 + 可持久化线段树
3218: a + b Problem Time Limit: 20 Sec Memory Limit: 40 MBSubmit: 1320 Solved: 498[Submit][Status] ...
- Xtreme9.0 - Light Gremlins 容斥
Xtreme9.0 - Light Gremlins 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenge ...
- OpenAI Gym
https://blog.openai.com/openai-gym-beta/ https://openai.com/
- SpringMVC介绍之视图解析器ViewResolver
在前一篇博客中讲了SpringMVC的Controller控制器,在这篇博客中将接着介绍一下SpringMVC视图解析器.当我们对SpringMVC控制的资源发起请求时,这些请求都会被SpringMV ...
- 为Qemu aarch32添加BeautifulSoup4模块
环境 Qemu:2.8.0 开发板:vexpress-ca9 概述 上一篇博文已经可以让我们的开发板可以成功的ping通百度了,据说Python的网络功能也很强大,而Beautiful Soup是 ...
- CRC8算法DELPHI源码
unit Crc8; interface Uses Classes, Windows; Function Crc_8n(p : array of BYTE; len : BYTE) : Byte; i ...
- Delphi 获取命令行输出的函数
function GetDosOutput(CommandLine: string; Work: string = 'C:\'): string; var SA: TSecurityAttribute ...
- Delphi处理Android的路径信息
路径操作就使用TPath的方法都很方便.usesSystem.IoUtilsTPath.GetTempPath//临时目录TPath.GetCameraPath//照相机目录(照片/录像)TPath. ...