mybatis-plus(insertBatchSomeColumn批量添加方式)

这篇文章主要介绍了mybatis-plus(insertBatchSomeColumn批量添加方式),具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

前言

大家平时在做业务时肯定会遇到会向表中批量添加数据的方法,那么这种方法mybatis-plus给我们提供了吗?

首先baseMapper中肯定没有提供,如下:只是添加单个实体的

但是IService貌似给我们提供了一个批量添加的方法:saveBatch(Collection entityList)

那我们就拿这个方法来测试一下

    @Test public void testInsertMore(){ //批量添加 //INSERT INTO user ( id, name, age ) VALUES ( ?, ?, ? ) List list = new ArrayList<>(); for (int i = 1; i <= 10; i++) { User user = new User(); user.setName("ybc"+i); user.setAge(20+i); list.add(user); } boolean b = userService.saveBatch(list); System.out.println(b); }

插入成功后:以下是打印出的sql语句 

 可以发现:虽然saveBatch(Collection entityList)这个方法我们代码中只是一行代码,但是底层其实还是单条插入的。

但是这样批量插入的速度有时其实是很慢的,那么有没有真正的批量插入方法呢?

其实mybatis-plus给我们预留了一个真正批量插入的扩展插件InsertBatchSomeColumn 

搭建工程

1)创建springboot项目

引入如下相关依赖

         com.baomidoumybatis-plus-boot-starter3.5.1 com.baomidoumybatis-plus-extension3.5.1

2)编写sql注入器

import com.baomidou.mybatisplus.annotation.FieldFill; import com.baomidou.mybatisplus.core.injector.AbstractMethod; import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector; import com.baomidou.mybatisplus.core.metadata.TableInfo; import com.baomidou.mybatisplus.extension.injector.methods.InsertBatchSomeColumn; import java.util.List; public class EasySqlInjector extends DefaultSqlInjector { @Override public List getMethodList(Class mapperClass, TableInfo tableInfo) { // 注意:此SQL注入器继承了DefaultSqlInjector(默认注入器),调用了DefaultSqlInjector的getMethodList方法,保留了mybatis-plus的自带方法 List methodList = super.getMethodList(mapperClass, tableInfo); methodList.add(new InsertBatchSomeColumn(i -> i.getFieldFill() != FieldFill.UPDATE)); return methodList; } }

3)注入插件

@Configuration public class MyBatisPlusConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor(){ MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); //添加分页插件 interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); //添加乐观锁插件 interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor()); return interceptor; } @Bean public EasySqlInjector easySqlInjector() { return new EasySqlInjector(); } }

4)编写自己的mapper继承BaseMapper

public interface EasyBaseMapper extends BaseMapper { /** * 批量插入 仅适用于mysql * * @param entityList 实体列表 * @return 影响行数 */ Integer insertBatchSomeColumn(Collection entityList); }

5)实体类的mapper继承自己编写的mapper

@Repository public interface UserMapper extends EasyBaseMapper { }

6)主启动类

@SpringBootApplication @MapperScan("com.atguigu.mybatisplus.mapper") public class MybatisplusApplication { public static void main(String[] args) { SpringApplication.run(MybatisplusApplication.class, args); } }

7)测试类测试

@SpringBootTest public class MyBatisPlusServiceTest { @Autowired private UserMapper userMapper; @Test public void testInsertMore(){ //批量添加 //INSERT INTO user ( id, name, age ) VALUES ( ?, ?, ? ) List list = new ArrayList<>(); for (int i = 1; i <= 10; i++) { User user = new User(); user.setName("ybc"+i); user.setAge(20+i); list.add(user); } userMapper.insertBatchSomeColumn(list); } }

测试结果:

==>  Preparing: INSERT INTO t_user (user_name,age,email,sex,is_deleted) VALUES (?,?,?,?,?) , (?,?,?,?,?) , (?,?,?,?,?) , (?,?,?,?,?) , (?,?,?,?,?) , (?,?,?,?,?) , (?,?,?,?,?) , (?,?,?,?,?) , (?,?,?,?,?) , (?,?,?,?,?)
==> Parameters: ybc1(String), 21(Integer), null, null, null, ybc2(String), 22(Integer), null, null, null, ybc3(String), 23(Integer), null, null, null, ybc4(String), 24(Integer), null, null, null, ybc5(String), 25(Integer), null, null, null, ybc6(String), 26(Integer), null, null, null, ybc7(String), 27(Integer), null, null, null, ybc8(String), 28(Integer), null, null, null, ybc9(String), 29(Integer), null, null, null, ybc10(String), 30(Integer), null, null, null
<==    Updates: 10
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@496a31da]

可以看到只执行了一条sql语句。

采坑点:希望自己注意自己的目录结构(图左)

我之前采用的是右边的目录结构导致一直报错:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userServiceImpl': Unsatisfied dependency expressed through field 'baseMapper'; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.atguigu.mybatisplus.mapper.UserMapper' available: expected single matching bean but found 2: easyBaseMapper,userMapper

 一直不知道原因出在什么地方,后来考虑到可能是mapper文件扫描时出现了问题,于是进行了修改,就OK了。

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持0133技术站。

以上就是mybatis-plus(insertBatchSomeColumn批量添加方式)的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » Java