方法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批量插入的两种方式的更多相关文章

  1. mybatis中批量插入的两种方式(高效插入)

    MyBatis简介 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以使用 ...

  2. mybatis批量保存的两种方式(高效插入)

    知识点:mybatis中,批量保存的两种方式 1.使用mybatis foreach标签 2.mybatis ExecutorType.BATCH 参考博客:https://www.jb51.net/ ...

  3. mysql批量更新的两种方式效率试验<二>

    Mysql两种批量更新的对比 简介: mysql搭载mybits框架批量更新有两种方式,一种是在xml中循环整个update语句,中间以‘:’隔开,还有一种是使用case when 变相实现批量更新, ...

  4. .NET环境下导出Excel表格的两种方式和导入两种类型的Excel表格

    一.导出Excel表格的两种方式,其中两种方式指的是导出XML数据类型的Excel(即保存的时候可以只需要修改扩展名为.xls)和真正的Excel这两种. using System; using Sy ...

  5. 【ADO.NET-中级】百万级数据的批量插入的两种方法测试

    在SQL Server 中插入一条数据使用Insert语句,但是如果想要批量插入一堆数据的话,循环使用Insert不仅效率低,而且会导致SQL一系统性能问题.下面介绍SQL Server支持的两种批量 ...

  6. Linux中crontab下scp文件传输的两种方式

    Linux下文件传输一般有两个命令scp.ftp(工具需要下载安装) 本文主要讲讲scp的文件传输脚本 1.scp ssh-keygen -t rsa免输入密码,传输 这里假设主机A 用来获到主机B的 ...

  7. eclipse下部署web工程的两种方式

    习惯了Eclipse 的开发,就觉得不想那么懒去用MyEclipse傻瓜式的部署工程. 第一种,手动部署工程. 情况一:如果工程目录在tomcat目录的webapp目录下,这种情况就不需要有工程部署的 ...

  8. Dynamics CRM 非声明验证方式下连接组织服务的两种方式的性能测试

    今天看了勇哥的博文"http://luoyong0201.blog.163.com/blog/static/1129305201510153391392/",又认识到了一种新的连接 ...

  9. 【linux】linux查看资源任务管理器,使用top命令 + 查看java进程下的线程数量【两种方式】

    ================================ 详解:https://blog.csdn.net/achenyuan/article/details/77867661 ======= ...

随机推荐

  1. 查看Mysql是否开启binlog

    show variables like 'log_bin';

  2. java获取当前年、半年、季度、月、日、小时 开始结束时间等

    import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; /** * 获取 当前年.半年 ...

  3. 生命不息,奔跑不止,持之以恒,勇攀高峰——JasonCeng的跑步打卡

    生命不息,奔跑不止,持之以恒,勇攀高峰!JasonCeng的跑步打卡博文,持续更新,培养好习惯,一起变成更优秀的自己! 2019-12-20-5.05km Avg-speed(均速): 5'56'' ...

  4. mac install azure-cli

    安装 CLI 时,可以先更新 brew 存储库信息,然后运行 install 命令: brew update && brew install azure-cli 更新: brew up ...

  5. Convolutional neural network (CNN) - Pytorch版

    import torch import torch.nn as nn import torchvision import torchvision.transforms as transforms # ...

  6. 用php做管理后台

    最近因处理家庭之事,技术上没有提高,这段时间也陆续的恢复了正常的开发,由于要做一个管理后台,所以在选择语言和架构上搜了不少资料, php 和java 的选择上,后来选择用php作为管理后台开发的语言. ...

  7. Python03之单引号、双引号、三单引号、三双引号

    今天在学习字符串的时候,发现字符串有时使用单引号,有时使用双引号,而有时还使用三引号.至此我整理如下: 单引号和双引号区别.三单引号和三双引号也没什么区别. 一: 两单引号之间不可以出现单引号,如果出 ...

  8. 100天搞定机器学习|day54 聚类系列:层次聚类原理及案例

    几张GIF理解K-均值聚类原理 k均值聚类数学推导与python实现 前文说了k均值聚类,他是基于中心的聚类方法,通过迭代将样本分到k个类中,使每个样本与其所属类的中心或均值最近. 今天我们看一下无监 ...

  9. 机器学习支持向量机SVM笔记

    SVM简述: SVM是一个线性二类分类器,当然通过选取特定的核函数也可也建立一个非线性支持向量机.SVM也可以做一些回归任务,但是它预测的时效性不是太长,他通过训练只能预测比较近的数据变化,至于再往后 ...

  10. vagrant root 登录虚拟机

    这个问题本来觉得是个特别简单的问题,昨天弄的时候折腾了半晚上.所以打算记录下过程,主要也被网上的各种信息误导了. 1 先看下我这vagrant配置信息 Vagrant.configure(" ...