微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

mybatisplus中insertBatchSomeColumn批量添加的方法是什么

这篇文章主要介绍了mybatisplus中insertBatchSomeColumn批量添加方法是什么的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇mybatisplus中insertBatchSomeColumn批量添加方法是什么文章都会有所收获,下面我们一起来看看吧。

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

mybatisplus中insertBatchSomeColumn批量添加的方法是什么

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

mybatisplus中insertBatchSomeColumn批量添加的方法是什么

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

    @Test
    public void testInsertMore(){
        //批量添加
        //INSERT INTO user ( id, name, age ) VALUES ( ?, ?, ? )
        List<User> 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语句 

mybatisplus中insertBatchSomeColumn批量添加的方法是什么

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

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

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

搭建工程

1)创建springboot项目

引入如下相关依赖

        <!--mybatis-plus启动器-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.1</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-extension</artifactId>
            <version>3.5.1</version>
        </dependency>

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<AbstractMethod> getmethodList(Class<?> mapperClass, TableInfo tableInfo) {
        // 注意:此sql注入器继承了DefaultsqlInjector(认注入器),调用了DefaultsqlInjector的getmethodList方法,保留了mybatis-plus的自带方法
        List<AbstractMethod> 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<T> extends BaseMapper<T> {
    /**
     * 批量插入 仅适用于MysqL
     *
     * @param entityList 实体列表
     * @return 影响行数
     */
    Integer insertBatchSomeColumn(Collection<T> entityList);
}

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

@Repository
public interface UserMapper<T> extends EasyBaseMapper<User> {
 
 
}

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<User> 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语句。

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

mybatisplus中insertBatchSomeColumn批量添加的方法是什么

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

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<com.atguigu.mybatisplus.pojo.User>' available: expected single matching bean but found 2: easyBaseMapper,userMapper

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

关于“mybatisplus中insertBatchSomeColumn批量添加方法是什么”这篇文章内容就介绍到这里,感谢各位的阅读!相信大家对“mybatisplus中insertBatchSomeColumn批量添加方法是什么”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注编程之家行业资讯频道。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐