MyBatisPlus批量添加的優(yōu)化與報錯解決
現(xiàn)狀
一般來說,批量插入可以使用 MyBatisPlus 中 ServiceImpl 自帶的方法 saveBatch

打開 sql 日志,application.yml 添加配置,mapper-locations 配置 mapper 路徑
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #開啟sql日志
mapper-locations: classpath*:mapper/**/*Mapper.xml可以發(fā)現(xiàn)插入是在同一個 SqlSession,但并不是理想中的批量插入

它的插入算法我沒有細究,但從日志觀察可以看出它的插入條數(shù)是無序的,如果可以一次插入全部,效率應該更高
優(yōu)化
MyBatisPlus 預留了 insertBatchSomeColumn 方法,可以實現(xiàn)批量插入,下面介紹一下如何配置
1.MyBatisPlus 依賴
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>2.新建 Sql 注射器 BatchSqlInjector
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 BatchSqlInjector extends DefaultSqlInjector {
@Override
public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {
List<AbstractMethod> methodList = super.getMethodList(mapperClass, tableInfo);
methodList.add(new InsertBatchSomeColumn(i -> i.getFieldFill() != FieldFill.UPDATE));
return methodList;
}
}3.MybatisPlusConfig 配置 BatchSqlInjector Bean,可忽略這里配置的分頁插件
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@EnableTransactionManagement
@Configuration
public class MybatisPlusConfig {
/**
* 分頁插件
*
* @return
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
PaginationInnerInterceptor pageInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);
pageInterceptor.setMaxLimit(500L);
pageInterceptor.setOptimizeJoin(true);
interceptor.addInnerInterceptor(pageInterceptor);
return interceptor;
}
/**
* 批量插入
*
* @return
*/
@Bean
public BatchSqlInjector easySqlInjector() {
return new BatchSqlInjector();
}
}
4.配置 BatchBaseMapper 繼承 BaseMapper
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import java.util.Collection;
public interface BatchBaseMapper<T> extends BaseMapper<T> {
/**
* 批量插入 僅適用于mysql
*
* @param entityList 實體列表
* @return 影響行數(shù)
*/
Integer insertBatchSomeColumn(Collection<T> entityList);
}
5.業(yè)務 Mapper 繼承 BatchBaseMapper
@Repository
public interface ISapCustomerMapper extends BatchBaseMapper<SapCustomerPO> {
}6.service 創(chuàng)建 createBatch 作為新的批量插入方法
public class SapCustomerServiceImpl extends ServiceImpl<ISapCustomerMapper, SapCustomerPO> {
void createBatch(List<SapCustomerPO> entityList) {
if(!entityList.isEmpty()){
baseMapper.insertBatchSomeColumn(entityList);
}
}
}注意:不建議直接用 mapper 的 insertBatchSomeColumn 方法,因為當 entityList 為空時會報錯
其實就是 INSERT INTO 表名(字段1,字段2,字段3) VALUES 后面為空
NestedRuntimeException:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘’ at line 1
效果
3600 條數(shù)據(jù)
優(yōu)化前:2058 毫秒
優(yōu)化后:1293 毫秒
15000 條數(shù)據(jù)
優(yōu)化前:8958 毫秒
優(yōu)化后:2037 毫秒
可以看出,數(shù)據(jù)越多,優(yōu)化效果越明細
通過這次測試發(fā)現(xiàn),打開 sql 日志后,會明細拖慢 sql 執(zhí)行效率,數(shù)據(jù)越多越明細
報錯
Packet for query is too large (82,807,536 > 67,108,864). You can change this value on the server by setting the max_allowed_packet’ variable
原因: 一次插入數(shù)量太多的數(shù)據(jù),超出了 mysql 默認設置
解決辦法: 在 service 層限制插入數(shù)量
static int batchSize = 10000;
public void createBatch(List<TestPO> entityList) {
if (!entityList.isEmpty()) {
int size = entityList.size();
int idxLimit = Math.min(batchSize, size);
int i = 1;
List<TestPO> oneBatchList = new ArrayList<>();
for (Iterator<TestPO> var7 = entityList.iterator(); var7.hasNext(); ++i) {
TestPOelement = var7.next();
oneBatchList.add(element);
if (i == idxLimit) {
baseMapper.insertBatchSomeColumn(oneBatchList);
oneBatchList.clear();
idxLimit = Math.min(idxLimit + batchSize, size);
}
}
}
}
總結
到此這篇關于MyBatisPlus批量添加的優(yōu)化與報錯解決的文章就介紹到這了,更多相關MyBatisPlus批量添加內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Spring攔截器HandlerInterceptor接口代碼解析
這篇文章主要介紹了Spring攔截器HandlerInterceptor接口代碼解析,具有一定借鑒價值,需要的朋友可以參考下2017-12-12
System.getProperty(“l(fā)ine.separator“)含義及意義詳解
這篇文章主要介紹了System.getProperty(“l(fā)ine.separator“)含義,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-05-05

