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

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

那我們就拿這個(gè)方法來測試一下
@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)這個(gè)方法我們代碼中只是一行代碼,但是底層其實(shí)還是單條插入的。
但是這樣批量插入的速度有時(shí)其實(shí)是很慢的,那么有沒有真正的批量插入方法呢?
其實(shí)mybatis-plus給我們預(yù)留了一個(gè)真正批量插入的擴(kuò)展插件InsertBatchSomeColumn
搭建工程
1)創(chuàng)建springboot項(xiàng)目
引入如下相關(guān)依賴
<!--mybatis-plus啟動(dòng)器-->
<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(默認(rèn)注入器),調(diào)用了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 實(shí)體列表
* @return 影響行數(shù)
*/
Integer insertBatchSomeColumn(Collection<T> entityList);
}5)實(shí)體類的mapper繼承自己編寫的mapper
@Repository
public interface UserMapper<T> extends EasyBaseMapper<User> {
}6)主啟動(dòng)類
@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);
}
}測試結(jié)果:
==> 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語句。
采坑點(diǎn):希望自己注意自己的目錄結(jié)構(gòu)(圖左)


我之前采用的是右邊的目錄結(jié)構(gòu)導(dǎo)致一直報(bào)錯(cuò):
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文件掃描時(shí)出現(xiàn)了問題,于是進(jìn)行了修改,就OK了。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot實(shí)現(xiàn)XSS攻擊防御的幾種方式
隨著Web應(yīng)用的普及,網(wǎng)絡(luò)安全問題也日益凸顯,跨站腳本攻擊(Cross-Site Scripting,簡稱XSS)是一種常見的Web安全漏洞,本文旨在探討如何在Spring Boot應(yīng)用程序中有效地防御XSS攻擊,我們將介紹兩種主要的防御手段:注解和過濾器,需要的朋友可以參考下2024-07-07
淺談StringEntity 和 UrlEncodedFormEntity之間的區(qū)別
這篇文章主要介紹了StringEntity 和 UrlEncodedFormEntity之間的區(qū)別,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
JAVA實(shí)現(xiàn)數(shù)字大寫金額轉(zhuǎn)換的方法
這篇文章主要介紹了JAVA實(shí)現(xiàn)數(shù)字大寫金額轉(zhuǎn)換的方法,涉及java針對字符串與數(shù)組的遍歷與轉(zhuǎn)換相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
SpringBoot配置Redis自定義過期時(shí)間操作
這篇文章主要介紹了SpringBoot配置Redis自定義過期時(shí)間操作,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
Jmeter跨線程組傳值調(diào)用實(shí)現(xiàn)圖解
這篇文章主要介紹了Jmeter跨線程組傳值調(diào)用實(shí)現(xiàn)圖解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
idea 2023.1字體設(shè)置及自動(dòng)調(diào)整大小的圖文教程
這篇文章主要介紹了idea 2023.1字體設(shè)置及自動(dòng)調(diào)整大小的教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-07-07

