方法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. LeetCode 605. 种花问题(Can Place Flowers) 6

    605. 种花问题 605. Can Place Flowers 题目描述 假设你有一个很长的花坛,一部分地块种植了花,另一部分却没有.可是,花卉不能种植在相邻的地块上,它们会争夺水源,两者都会死去. ...

  2. 021 Android 查询已经导入到工程中的数据库+抖动效果

    1.将数据库(.db)文件放入工程中 在project状态下,新建assets文件夹,并将数据库文件放入assets目录下. 注意:assets目录.java目录.res目录是同级的 new---&g ...

  3. [转帖]AMD三代锐龙线程撕裂者命名曝光:24核心3960X

    AMD三代锐龙线程撕裂者命名曝光:24核心3960X https://www.cnbeta.com/articles/tech/900271.htm 一直搞不懂TDP啥意思 可能会高于TDP的功率.. ...

  4. (五)Spring Boot官网文档学习

    文章目录 SpringApplication SpringApplication 事件 `ApplicationContext ` 类型 访问传递给 `SpringApplication` 的参数 A ...

  5. P1308(字符串类,处理字符串查找)

    题目描述 一般的文本编辑器都有查找单词的功能,该功能可以快速定位特定单词在文章中的位置,有的还能统计出特定单词在文章中出现的次数. 现在,请你编程实现这一功能,具体要求是:给定一个单词,请你输出它在给 ...

  6. ORA-01618 ORA-19809: limit exceeded for recovery files

    由于DB_RECOVERY_FILE_DEST_SIZE 参数设置太小,导致redo只能创建一组,无法创建节点二的日志组,节点二数据库无法启动,如下图: 修改参数后,节点二无法启动到mount模式,无 ...

  7. TZOJ3591这个真不会

    #include<stdio.h> int main() { ],b[],c,x,y; scanf("%d",&t); while(t--) { c=; x=; ...

  8. Django数据库基本操作(MySQL)

    以一个示例工程为例: 下面是工程文件目录: untited为项目文件(一般与根目录同名),CommunityModel为一个定义数据库模型的APP 一.定义模型 1.首先配置好数据库,在untited ...

  9. flutter从入门到精通二

    静态方法和静态属性(static): 通过static修饰的方法和属性称为静态方法和静态属性,注意静态方法和静态属性只能通过类名访问,不能通过对象访问. 静态方法不能访问非静态的属性和非静态方法,反正 ...

  10. 在Windows平台搭建C语言开发环境

    一.在Windows平台搭建DEV C++集成开发环境     官网 https://sourceforge.net/projects/orwelldevcpp/ 中下载Dev C++运行即可 环境准 ...