mybatis-plus(insertBatchSomeColumn批量添加方式)
前言
大家平時在做業(yè)務時肯定會遇到會向表中批量添加數(shù)據(jù)的方法,那么這種方法mybatis-plus給我們提供了嗎?
首先baseMapper中肯定沒有提供,如下:只是添加單個實體的

但是IService貌似給我們提供了一個批量添加的方法:saveBatch(Collection<T> entityList)

那我們就拿這個方法來測試一下
@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語句

可以發(fā)現(xiàn):雖然saveBatch(Collection<T> entityList)這個方法我們代碼中只是一行代碼,但是底層其實還是單條插入的。
但是這樣批量插入的速度有時其實是很慢的,那么有沒有真正的批量插入方法呢?
其實mybatis-plus給我們預留了一個真正批量插入的擴展插件InsertBatchSomeColumn
搭建工程
1)創(chuàng)建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 影響行數(shù)
*/
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]
可以看到只執(zhí)行了一條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<com.atguigu.mybatisplus.pojo.User>' available: expected single matching bean but found 2: easyBaseMapper,userMapper
一直不知道原因出在什么地方,后來考慮到可能是mapper文件掃描時出現(xiàn)了問題,于是進行了修改,就OK了。
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
淺析javax.servlet.Servlet,ServletContext接口
本篇文章是對javax.servlet.Servlet,ServletContext接口進行了纖細的分析介紹,需要的朋友參考下2013-07-07
java?String到底有多長?String超出長度該如何解決
在Java中,由于字符串常量池的存在,String常量長度限制取決于String常量在常量池中的存儲大小,下面這篇文章主要給大家介紹了關于java?String到底有多長?String超出長度該如何解決的相關資料,需要的朋友可以參考下2023-01-01
Java Socket+mysql實現(xiàn)簡易文件上傳器的代碼
最近在做一個小項目,項目主要需求是實現(xiàn)一個文件上傳器,通過客戶端的登陸,把本地文件上傳到服務器的數(shù)據(jù)庫(本地的)。下面通過本文給大家分享下實現(xiàn)代碼,感興趣的朋友一起看看吧2016-10-10
spring boot線上日志級別動態(tài)調整的配置步驟
這篇文章主要為大家介紹了spring boot線上日志級別動態(tài)調整的配置步驟,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2022-03-03

