栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

mybatis-plus(insertBatchSomeColumn批量添加)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

mybatis-plus(insertBatchSomeColumn批量添加)

目录

1 前言

2 搭建工程

1 前言

大家平时在做业务时肯定会遇到会向表中批量添加数据的方法,那么这种方法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 

2 搭建工程

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

        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.5.1
        
        
            com.baomidou
            mybatis-plus-extension
            3.5.1
        

2)编写sql注入器

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 {
    
    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了。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1035027.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号