• mybatis-plus(insertBatchSomeColumn批量添加)


    目录

    1 前言

    2 搭建工程

    1 前言

    大家平时在做业务时肯定会遇到会向表中批量添加数据的方法,那么这种方法mybatis-plus给我们提供了吗?首先baseMapper中肯定没有提供,如下:只是添加单个实体的

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

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

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

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

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

    但是这样批量插入的速度有时其实是很慢的,那么有没有真正的批量插入方法呢?其实mybatis-plus给我们预留了一个真正批量插入的扩展插件InsertBatchSomeColumn 

    2 搭建工程

    1)创建springboot项目,引入如下相关依赖

    1. <dependency>
    2. <groupId>com.baomidougroupId>
    3. <artifactId>mybatis-plus-boot-starterartifactId>
    4. <version>3.5.1version>
    5. dependency>
    6. <dependency>
    7. <groupId>com.baomidougroupId>
    8. <artifactId>mybatis-plus-extensionartifactId>
    9. <version>3.5.1version>
    10. dependency>

    2)编写sql注入器

    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. }

    3)注入插件

    1. @Configuration
    2. public class MyBatisPlusConfig {
    3. @Bean
    4. public MybatisPlusInterceptor mybatisPlusInterceptor(){
    5. MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
    6. //添加分页插件
    7. interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
    8. //添加乐观锁插件
    9. interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
    10. return interceptor;
    11. }
    12. @Bean
    13. public EasySqlInjector easySqlInjector() {
    14. return new EasySqlInjector();
    15. }
    16. }

    4)编写自己的mapper继承BaseMapper

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

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

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

    6)主启动类

    1. @SpringBootApplication
    2. @MapperScan("com.atguigu.mybatisplus.mapper")
    3. public class MybatisplusApplication {
    4. public static void main(String[] args) {
    5. SpringApplication.run(MybatisplusApplication.class, args);
    6. }
    7. }

    7)测试类测试

    1. @SpringBootTest
    2. public class MyBatisPlusServiceTest {
    3. @Autowired
    4. private UserMapper userMapper;
    5. @Test
    6. public void testInsertMore(){
    7. //批量添加
    8. //INSERT INTO user ( id, name, age ) VALUES ( ?, ?, ? )
    9. List list = new ArrayList<>();
    10. for (int i = 1; i <= 10; i++) {
    11. User user = new User();
    12. user.setName("ybc"+i);
    13. user.setAge(20+i);
    14. list.add(user);
    15. }
    16. userMapper.insertBatchSomeColumn(list);
    17. }
    18. }

    测试结果:

    1. ==> Preparing: INSERT INTO t_user (user_name,age,email,sex,is_deleted) VALUES (?,?,?,?,?) , (?,?,?,?,?) , (?,?,?,?,?) , (?,?,?,?,?) , (?,?,?,?,?) , (?,?,?,?,?) , (?,?,?,?,?) , (?,?,?,?,?) , (?,?,?,?,?) , (?,?,?,?,?)
    2. ==> 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
    3. <== Updates: 10
    4. 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了。

  • 相关阅读:
    二次开发MES管理系统的利与弊
    Django数据表修改方法
    OpenCV_06 图像平滑:图像噪声+图像平滑+滤波
    经典算法系列之(一):算法的基础概念,数据结构的基础概念,以及算法+数据结构=程序
    KVM虚拟机迁移
    深度|谁在为OpenAI和Anthropic的AI编程竞赛提供“军火”?已赚得盆满钵满
    以软硬协同最佳实践构建智能算力,燧原科技WAIC大会上发布云燧智算机
    w010基于Springboot大学生入学审核系统的设计与实现
    Go中原生http服务的实现方式
    专利的黑白图片处理:
  • 原文地址:https://blog.csdn.net/qq_50652600/article/details/126038809