MyBatis批量插入的五种方式,哪种最强???

前言:

这里我列举了MyBatis和MyBatis-Plus常用的五种批量插入的方式,进行了详细的总结归纳,写的非常详细,整体思路清晰明了,只分享干货。

目录

一、准备工作

二、MyBatis利用For循环批量插入

三、MyBatis的手动批量提交

四、MyBatis以集合方式批量新增(推荐)

五、MyBatis-Plus提供的SaveBatch方法

六、MyBatis-Plus提供的InsertBatchSomeColumn方法(推荐)

七、总结


一、准备工作

1、导入pom.xml依赖

  1. <dependency>
  2. <groupId>mysql</groupId>
  3. <artifactId>mysql-connector-java</artifactId>
  4. <scope>runtime</scope>
  5. </dependency>
  6. <!--Mybatis依赖-->
  7. <dependency>
  8. <groupId>org.mybatis.spring.boot</groupId>
  9. <artifactId>mybatis-spring-boot-starter</artifactId>
  10. <version>2.2.2</version>
  11. </dependency>
  12. <!--Mybatis-Plus依赖-->
  13. <dependency>
  14. <groupId>com.baomidou</groupId>
  15. <artifactId>mybatis-plus-boot-starter</artifactId>
  16. <version>3.5.2</version>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.projectlombok</groupId>
  20. <artifactId>lombok</artifactId>
  21. <optional>true</optional>
  22. </dependency>

2、配置yml文件

  1. server:
  2. port: 8080
  3. spring:
  4. datasource:
  5. username: mysql用户名
  6. password: mysql密码
  7. url: jdbc:mysql://localhost:3306/数据库名字?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
  8. driver-class-name: com.mysql.cj.jdbc.Driver
  9. mybatis:
  10. mapper-locations: classpath:mapping/*.xml

 3、公用的User类

  1. @Data
  2. public class User {
  3. private int id;
  4. private String username;
  5. private String password;
  6. }

二、MyBatis利用For循环批量插入

1、编写UserService服务类,测试一万条数据耗时情况

  1. @Service
  2. public class UserService {
  3. @Resource
  4. private UserMapper userMapper;
  5. public void InsertUsers(){
  6. long start = System.currentTimeMillis();
  7. for(int i = 0 ;i < 10000; i++) {
  8. User user = new User();
  9. user.setUsername("name" + i);
  10. user.setPassword("password" + i);
  11. userMapper.insertUsers(user);
  12. }
  13. long end = System.currentTimeMillis();
  14. System.out.println("一万条数据总耗时:" + (end-start) + "ms" );
  15. }
  16. }

2、编写UserMapper接口

  1. @Mapper
  2. public interface UserMapper {
  3. Integer insertUsers(User user);
  4. }

3、编写UserMapper.xml文件

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.ithuang.demo.mapper.UserMapper">
  6. <insert id="insertUsers">
  7. INSERT INTO user (username, password)
  8. VALUES(#{username}, #{password})
  9. </insert>
  10. </mapper>

4、进行单元测试

  1. @SpringBootTest
  2. class DemoApplicationTests {
  3. @Resource
  4. private UserService userService;
  5. @Test
  6. public void insert(){
  7. userService.InsertUsers();
  8. }
  9. }

5、结果输出

一万条数据总耗时:26348ms

三、MyBatis的手动批量提交

1、其他保持不变,Service层作稍微的变化

  1. @Service
  2. public class UserService {
  3. @Resource
  4. private UserMapper userMapper;
  5. @Resource
  6. private SqlSessionTemplate sqlSessionTemplate;
  7. public void InsertUsers(){
  8. //关闭自动提交
  9. SqlSession sqlSession = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH, false);
  10. UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
  11. long start = System.currentTimeMillis();
  12. for(int i = 0 ;i < 10000; i++) {
  13. User user = new User();
  14. user.setUsername("name" + i);
  15. user.setPassword("password" + i);
  16. userMapper.insertUsers(user);
  17. }
  18. sqlSession.commit();
  19. long end = System.currentTimeMillis();
  20. System.out.println("一万条数据总耗时:" + (end-start) + "ms" );
  21. }
  22. }

2、结果输出

一万条数据总耗时:24516ms

四、MyBatis以集合方式批量新增(推荐)

1、编写UserService服务类

  1. @Service
  2. public class UserService {
  3. @Resource
  4. private UserMapper userMapper;
  5. public void InsertUsers(){
  6. long start = System.currentTimeMillis();
  7. List<User> userList = new ArrayList<>();
  8. User user;
  9. for(int i = 0 ;i < 10000; i++) {
  10. user = new User();
  11. user.setUsername("name" + i);
  12. user.setPassword("password" + i);
  13. userList.add(user);
  14. }
  15. userMapper.insertUsers(userList);
  16. long end = System.currentTimeMillis();
  17. System.out.println("一万条数据总耗时:" + (end-start) + "ms" );
  18. }
  19. }

3、编写UserMapper接口

  1. @Mapper
  2. public interface UserMapper {
  3. Integer insertUsers(List<User> userList);
  4. }

4、编写UserMapper.xml文件

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.ithuang.demo.mapper.UserMapper">
  6. <insert id="insertUsers">
  7. INSERT INTO user (username, password)
  8. VALUES
  9. <foreach collection ="userList" item="user" separator =",">
  10. (#{user.username}, #{user.password})
  11. </foreach>
  12. </insert>
  13. </mapper>

5、输出结果

一万条数据总耗时:521ms

五、MyBatis-Plus提供的SaveBatch方法

1、编写UserService服务

  1. @Service
  2. public class UserService extends ServiceImpl<UserMapper, User> implements IService<User> {
  3. public void InsertUsers(){
  4. long start = System.currentTimeMillis();
  5. List<User> userList = new ArrayList<>();
  6. User user;
  7. for(int i = 0 ;i < 10000; i++) {
  8. user = new User();
  9. user.setUsername("name" + i);
  10. user.setPassword("password" + i);
  11. userList.add(user);
  12. }
  13. saveBatch(userList);
  14. long end = System.currentTimeMillis();
  15. System.out.println("一万条数据总耗时:" + (end-start) + "ms" );
  16. }
  17. }

2、编写UserMapper接口

  1. @Mapper
  2. public interface UserMapper extends BaseMapper<User> {
  3. }

3、单元测试结果

一万条数据总耗时:24674ms

六、MyBatis-Plus提供的InsertBatchSomeColumn方法(推荐)

1、编写EasySqlInjector 自定义类

  1. public class EasySqlInjector extends DefaultSqlInjector {
  2. @Override
  3. public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {
  4. // 注意:此SQL注入器继承了DefaultSqlInjector(默认注入器),调用了DefaultSqlInjector的getMethodList方法,保留了mybatis-plus的自带方法
  5. List<AbstractMethod> methodList = super.getMethodList(mapperClass, tableInfo);
  6. methodList.add(new InsertBatchSomeColumn(i -> i.getFieldFill() != FieldFill.UPDATE));
  7. return methodList;
  8. }
  9. }

2、定义核心配置类注入此Bean

  1. @Configuration
  2. public class MybatisPlusConfig {
  3. @Bean
  4. public EasySqlInjector sqlInjector() {
  5. return new EasySqlInjector();
  6. }
  7. }

3、编写UserService服务类

  1. public class UserService{
  2. @Resource
  3. private UserMapper userMapper;
  4. public void InsertUsers(){
  5. long start = System.currentTimeMillis();
  6. List<User> userList = new ArrayList<>();
  7. User user;
  8. for(int i = 0 ;i < 10000; i++) {
  9. user = new User();
  10. user.setUsername("name" + i);
  11. user.setPassword("password" + i);
  12. userList.add(user);
  13. }
  14. userMapper.insertBatchSomeColumn(userList);
  15. long end = System.currentTimeMillis();
  16. System.out.println("一万条数据总耗时:" + (end-start) + "ms" );
  17. }
  18. }

4、编写EasyBaseMapper接口

  1. public interface EasyBaseMapper<T> extends BaseMapper<T> {
  2. /**
  3. * 批量插入 仅适用于mysql
  4. *
  5. * @param entityList 实体列表
  6. * @return 影响行数
  7. */
  8. Integer insertBatchSomeColumn(Collection<T> entityList);
  9. }

5、编写UserMapper接口

  1. @Mapper
  2. public interface UserMapper<T> extends EasyBaseMapper<User> {
  3. }

6、单元测试结果

一万条数据总耗时:575ms

七、总结

以上就是我对目前MyBatis常用的批量插入方法的总结

来源:https://blog.csdn.net/HJW_233/article/details/126909439
posted @ 2023-02-11 21:37  程序员小明1024  阅读(895)  评论(0编辑  收藏  举报