首页 技术 正文
技术 2022年11月21日
0 收藏 484 点赞 3,377 浏览 2076 个字

MyBatis 使用 foreach 批量插入

参考博文

老司机学习MyBatis之动态SQL使用foreach在MySQL中批量插入

使用MyBatis一次性插入多条数据时候可以使用 <foreach> 标签。

yml文件

spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/db3?serverTimezone=Asia/Shanghai&allowMultiQueries=true
username: root
password: rootmybatis:
type-aliases-package: com.mozq.boot.sbmybatis02.domain
mapper-locations: classpath:mapper/*Mapper.xml

第1种方式 单条语句插入多个值

可以使用 useGeneratedKeys 返回每个插入记录的主键。

修改 Mapper 添加批量插入方法

@Mapper
public interface UserMapper {
void batchSave(List<User> userList);
}

修改映射文件 添加批量插入映射语句

<insert id="batchSave">
insert into user(name, password) values
<foreach collection="list" item="user" separator=",">
(#{user.name}, #{user.password})
</foreach>
</insert>

测试接口

@RunWith(SpringRunner.class)
@SpringBootTest
public class SbMybatis02ApplicationTests {
@Test
public void testBatchSave(){
User user1 = new User();
user1.setName("关羽");
user1.setPassword("guanyu");
User user2 = new User();
user2.setName("张飞");
user2.setPassword("zhangfei");
List<User> userList = new ArrayList<>();
userList.add(user1);
userList.add(user2); userMapper.batchSave(userList);
}
}

第2种方式 多条语句插入多个值

如果插入的同时获取主键,则只有第1条记录可以获取到,其他记录获取不到生成的主键。

修改 Mapper 添加批量插入方法

@Mapper
public interface UserMapper {
void batchSave(List<User> userList);
}

修改映射文件 添加批量插入映射语句

<insert id="batchSave">
<foreach collection="list" item="user" separator=";">
insert into user(name, password) values
(#{user.name}, #{user.password})
</foreach>
</insert>

修改 jdbcUrl 允许执行多条语句

jdbc:mysql://localhost:3306/db3?serverTimezone=Asia/Shanghai&allowMultiQueries=true

测试接口

@RunWith(SpringRunner.class)
@SpringBootTest
public class SbMybatis02ApplicationTests {
@Test
public void testBatchSave(){
User user1 = new User();
user1.setName("关羽");
user1.setPassword("guanyu");
User user2 = new User();
user2.setName("张飞");
user2.setPassword("zhangfei");
List<User> userList = new ArrayList<>();
userList.add(user1);
userList.add(user2); userMapper.batchSave(userList);
}
}

bugs

Caused by: java.sql.SQLSyntaxErrorException: 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 user(name, password) values('张飞', 'zhangfei')' at line 4
方案:
jdbcUrl 添加参数 allowMultiQueries=true
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,078
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,553
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,402
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,177
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,814
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898