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

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

那我們就拿這個(gè)方法來測(cè)試一下
@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();
//添加分頁(yè)插件
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)測(cè)試類測(cè)試
@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);
}
}測(cè)試結(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)文章
淺析javax.servlet.Servlet,ServletContext接口
本篇文章是對(duì)javax.servlet.Servlet,ServletContext接口進(jìn)行了纖細(xì)的分析介紹,需要的朋友參考下2013-07-07
java?String到底有多長(zhǎng)?String超出長(zhǎng)度該如何解決
在Java中,由于字符串常量池的存在,String常量長(zhǎng)度限制取決于String常量在常量池中的存儲(chǔ)大小,下面這篇文章主要給大家介紹了關(guān)于java?String到底有多長(zhǎng)?String超出長(zhǎng)度該如何解決的相關(guān)資料,需要的朋友可以參考下2023-01-01
Java Socket+mysql實(shí)現(xiàn)簡(jiǎn)易文件上傳器的代碼
最近在做一個(gè)小項(xiàng)目,項(xiàng)目主要需求是實(shí)現(xiàn)一個(gè)文件上傳器,通過客戶端的登陸,把本地文件上傳到服務(wù)器的數(shù)據(jù)庫(kù)(本地的)。下面通過本文給大家分享下實(shí)現(xiàn)代碼,感興趣的朋友一起看看吧2016-10-10
spring boot線上日志級(jí)別動(dòng)態(tài)調(diào)整的配置步驟
這篇文章主要為大家介紹了spring boot線上日志級(jí)別動(dòng)態(tài)調(diào)整的配置步驟,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03

