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

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

那我们就拿这个方法来测试一下
- @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
但是这样批量插入的速度有时其实是很慢的,那么有没有真正的批量插入方法呢?其实mybatis-plus给我们预留了一个真正批量插入的扩展插件InsertBatchSomeColumn
1)创建springboot项目,引入如下相关依赖
-
- <dependency>
- <groupId>com.baomidougroupId>
- <artifactId>mybatis-plus-boot-starterartifactId>
- <version>3.5.1version>
- dependency>
- <dependency>
- <groupId>com.baomidougroupId>
- <artifactId>mybatis-plus-extensionartifactId>
- <version>3.5.1version>
- dependency>
2)编写sql注入器
- 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
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
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了。