MyBatisPlus批量添加的優(yōu)化與報(bào)錯(cuò)解決
現(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)插入是在同一個(gè) SqlSession,但并不是理想中的批量插入
它的插入算法我沒有細(xì)究,但從日志觀察可以看出它的插入條數(shù)是無序的,如果可以一次插入全部,效率應(yīng)該更高
優(yōu)化
MyBatisPlus 預(yù)留了 insertBatchSomeColumn 方法,可以實(shí)現(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 實(shí)體列表 * @return 影響行數(shù) */ Integer insertBatchSomeColumn(Collection<T> entityList); }
5.業(yè)務(wù) 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 方法,因?yàn)楫?dāng) entityList 為空時(shí)會(huì)報(bào)錯(cuò)
其實(shí)就是 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)化效果越明細(xì)
通過這次測(cè)試發(fā)現(xiàn),打開 sql 日志后,會(huì)明細(xì)拖慢 sql 執(zhí)行效率,數(shù)據(jù)越多越明細(xì)
報(bào)錯(cuò)
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 默認(rèn)設(shè)置
解決辦法: 在 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); } } } }
總結(jié)
到此這篇關(guān)于MyBatisPlus批量添加的優(yōu)化與報(bào)錯(cuò)解決的文章就介紹到這了,更多相關(guān)MyBatisPlus批量添加內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot 在ftl頁面上使用shiro標(biāo)簽的實(shí)例代碼
這篇文章主要介紹了springboot 在ftl頁面上使用shiro標(biāo)簽的實(shí)例代碼,通過文字說明結(jié)合實(shí)例的形式給大家介紹的非常詳細(xì),需要的朋友參考下吧2018-05-05Spring MVC實(shí)現(xiàn)文件上傳及優(yōu)化案例解析
本文介紹如何在SpringMVC框架中實(shí)現(xiàn)文件上傳和異步登錄功能,通過添加必要的依賴和配置,創(chuàng)建文件上傳頁面和控制器,實(shí)現(xiàn)文件上傳到指定文件夾,同時(shí),展示了如何使用AJAX實(shí)現(xiàn)局部刷新的異步登錄,優(yōu)化用戶體驗(yàn),詳細(xì)步驟包括配置springmvc.xml、編寫前端頁面和控制器等2024-10-10java的jdk基礎(chǔ)知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家整理的是一篇關(guān)于java的jdk基礎(chǔ)知識(shí)點(diǎn)總結(jié)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)參考下。2021-01-01ArrayList和LinkedList的區(qū)別、擴(kuò)容機(jī)制以及底層的實(shí)現(xiàn)方式
這篇文章主要介紹了ArrayList和LinkedList的區(qū)別、擴(kuò)容機(jī)制以及底層的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03利用EasyPOI實(shí)現(xiàn)多sheet和列數(shù)的動(dòng)態(tài)生成
EasyPoi功能如同名字,主打的功能就是容易,讓一個(gè)沒見接觸過poi的人員就可以方便的寫出Excel導(dǎo)出,Excel導(dǎo)入等功能,本文主要來講講如何利用EasyPOI實(shí)現(xiàn)多sheet和列數(shù)的動(dòng)態(tài)生成,需要的可以了解下2025-03-03