MyBatis_动态sql_foreach_mysql下foreach批量插入的两种方式
方法1:
笔记要点
出错分析与总结
工程组织
数据库组织
0.重新修改Bean类 修改
1.定义接口
//批量插入
public void addEmps(@Param("emps")List<Employee> emps);
2.定义XML映射文件
<!--//批量插入-->
<!--public void addEmps(@Param("emps")List<Employee> emps);-->
<insert id="addEmps">
INSERT INTO tbl_employee(`last_name`,`email`,`gender`,`d_id`)
VALUES
<foreach collection="emps" item="emp" separator=",">
(#{emp.lastName},#{emp.email},#{emp.gender},#{emp.dept.id})
</foreach> </insert>
3.编写测试代码
public SqlSessionFactory getSqlSessionFactory() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
return new SqlSessionFactoryBuilder().build(inputStream);
}
@Test
public void test12() throws Exception {
SqlSession openSession = getSqlSessionFactory().openSession();
try {
System.out.println("++++++++++---- 2.测试动态SQL: mysql下的foreach的批量插入");
EmployeeMapper_DynamicSQL mapper = openSession.getMapper(EmployeeMapper_DynamicSQL.class);
List<Employee> emps=new ArrayList<>();
emps.add(new Employee(null,"smith" ,"smith@qq.com" , "1",
new Department(1)));
emps.add(new Employee(null,"aliex" ,"aliex@qq.com" , "0",
new Department(1)));
mapper.addEmps(emps);
openSession.commit();
} finally {
openSession.close();
}
}
测试结果
++++++++++---- 2.测试动态SQL: mysql下的foreach的批量插入
DEBUG 12-05 16:43:52,848 ==> Preparing: INSERT INTO tbl_employee(`last_name`,`email`,`gender`,`d_id`) VALUES (?,?,?,?) , (?,?,?,?) (BaseJdbcLogger.java:145)
DEBUG 12-05 16:43:52,873 ==> Parameters: smith(String), smith@qq.com(String), 1(String), 1(Integer), aliex(String), aliex@qq.com(String), 0(String), 1(Integer) (BaseJdbcLogger.java:145)
DEBUG 12-05 16:43:52,875 <== Updates: 2 (BaseJdbcLogger.java:145)
方法2: 使用多条mysql语句
1.开启allowMultiQueries=true属性--支持一次查询多条语句, 进入全局配置文件中

2.定义XML映射文件
<insert id="addEmps">
<foreach collection="emps" item="emp" separator=";">
INSERT INTO tbl_employee(last_name,email,gender,d_id)
VALUES (#{emp.lastName},#{emp.email},#{emp.gender},#{emp.dept.id});
</foreach>
</insert>
3.编写测试代码
@Test
public void test12() throws Exception {
SqlSession openSession = getSqlSessionFactory().openSession();
try {
System.out.println("++++++++++---- 2-2.测试动态SQL: mysql下的foreach的批量插入");
EmployeeMapper_DynamicSQL mapper = openSession.getMapper(EmployeeMapper_DynamicSQL.class);
List<Employee> emps=new ArrayList<>();
emps.add(new Employee(null,"smith2" ,"smith@qq.com" , "1",
new Department(1)));
emps.add(new Employee(null,"aliex2" ,"aliex@qq.com" , "0",
new Department(1)));
mapper.addEmps(emps); openSession.commit();
} finally {
openSession.close();
}
}
测试结果
idea环境下, 测试失败!原因如下:
++++++++++---- 2-2.测试动态SQL: mysql下的foreach的批量插入
DEBUG 12-05 17:14:10,399 ==> Preparing: INSERT INTO tbl_employee(last_name,email,gender,d_id) VALUES (?,?,?,?); ; INSERT INTO tbl_employee(last_name,email,gender,d_id) VALUES (?,?,?,?); (BaseJdbcLogger.java:145)
DEBUG 12-05 17:14:10,424 ==> Parameters: smith2(String), smith@qq.com(String), 1(String), 1(Integer), aliex2(String), aliex@qq.com(String), 0(String), 1(Integer) (BaseJdbcLogger.java:145) org.apache.ibatis.exceptions.PersistenceException:
### Error updating database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ';
INSERT INTO tbl_employee(last_name,email,gender,d_id)
' at line 3
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: INSERT INTO tbl_employee(last_name,email,gender,d_id) VALUES (?,?,?,?); ; INSERT INTO tbl_employee(last_name,email,gender,d_id) VALUES (?,?,?,?);
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ';
INSERT INTO tbl_employee(last_name,email,gender,d_id)
' at line 3
........
....
MyBatis_动态sql_foreach_mysql下foreach批量插入的两种方式的更多相关文章
- mybatis中批量插入的两种方式(高效插入)
MyBatis简介 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以使用 ...
- mybatis批量保存的两种方式(高效插入)
知识点:mybatis中,批量保存的两种方式 1.使用mybatis foreach标签 2.mybatis ExecutorType.BATCH 参考博客:https://www.jb51.net/ ...
- mysql批量更新的两种方式效率试验<二>
Mysql两种批量更新的对比 简介: mysql搭载mybits框架批量更新有两种方式,一种是在xml中循环整个update语句,中间以‘:’隔开,还有一种是使用case when 变相实现批量更新, ...
- .NET环境下导出Excel表格的两种方式和导入两种类型的Excel表格
一.导出Excel表格的两种方式,其中两种方式指的是导出XML数据类型的Excel(即保存的时候可以只需要修改扩展名为.xls)和真正的Excel这两种. using System; using Sy ...
- 【ADO.NET-中级】百万级数据的批量插入的两种方法测试
在SQL Server 中插入一条数据使用Insert语句,但是如果想要批量插入一堆数据的话,循环使用Insert不仅效率低,而且会导致SQL一系统性能问题.下面介绍SQL Server支持的两种批量 ...
- Linux中crontab下scp文件传输的两种方式
Linux下文件传输一般有两个命令scp.ftp(工具需要下载安装) 本文主要讲讲scp的文件传输脚本 1.scp ssh-keygen -t rsa免输入密码,传输 这里假设主机A 用来获到主机B的 ...
- eclipse下部署web工程的两种方式
习惯了Eclipse 的开发,就觉得不想那么懒去用MyEclipse傻瓜式的部署工程. 第一种,手动部署工程. 情况一:如果工程目录在tomcat目录的webapp目录下,这种情况就不需要有工程部署的 ...
- Dynamics CRM 非声明验证方式下连接组织服务的两种方式的性能测试
今天看了勇哥的博文"http://luoyong0201.blog.163.com/blog/static/1129305201510153391392/",又认识到了一种新的连接 ...
- 【linux】linux查看资源任务管理器,使用top命令 + 查看java进程下的线程数量【两种方式】
================================ 详解:https://blog.csdn.net/achenyuan/article/details/77867661 ======= ...
随机推荐
- iOS-UIDocumentInteractionController打开和预览文档
iOS提供了使用其他app预览文件的支持,这就是Document Interaction Controller.此外,iOS也支持文件关联,允许其他程序调用你的app打开某种文件.而且,从4.2开始, ...
- RF之条件判断、初始化清除-4
条件判断: rf中用run keyword if 关键字做条件判断,以此来达到类似在python中if ...else...条件判断的功能. 注意:ELSE IF一定都是大写的,不然运行 ...
- WIN10桌面无创建文件夹选项,无法创建文件
在桌面或其他磁盘,右键没有新建选项,无法新建文件夹或文档. 右键桌面左下角开始按钮,点击:命令提示符(管理员) 弹出,Windows命令处理程序对话框,点击是 粘贴内容: cmd /k r ...
- 034 Android NavigationView和DrawerLayout实现抽屉式导航设计(侧边栏效果)
1.创建带侧滑效果的activity 右击,new---->activity---->选择NavgationDrawer Activity 2.xml文件布局 (1)activity_ma ...
- [转帖]Nginx Image Module图片缩略图 水印处理模块
Nginx Image Module图片缩略图 水印处理模块 https://www.cnblogs.com/jicki/p/5546972.html Nginx Image Module图片缩略图 ...
- 转:Cesium 和 Webpack
原文地址:https://www.jianshu.com/p/85917bcc023f 注意:webpack 和 webpack-cli 的安装参考 https://www.cnblogs.com/m ...
- Java基础---Java 练习题49
1.分别用do-while和for循环计算1+1/2!+1/3!+…前20项和 /** * 用do-while计算1+1/2!+1/3!+……的前20项的和 * @Hoagn * */ public ...
- C++利用权重方法将二进制正数转换为十进制数
#include <iostream> #include <Windows.h> #include <string> using namespace std; in ...
- 04 Python的继承、方法重写、super()类、父类私密属性的调用
继承 A类继承B类,A即可获得B类的全部公共属性和方法(包括内置属性和方法).格式如:class A(B): class Animal: def sleep(self): print("zZ ...
- Android--单选对话框
import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterfa ...