mybatis-plus saveOrUpdateBatch踩坑記錄
mybatis-plus saveOrUpdateBatch踩坑
mybatis-plus版本: 3.5.1
調(diào)用方法
@Transactional(rollbackFor = Exception.class) default boolean saveOrUpdateBatch(Collection<T> entityList) { return saveOrUpdateBatch(entityList, DEFAULT_BATCH_SIZE); }
問(wèn)題說(shuō)明
當(dāng)對(duì)entityList進(jìn)行批量更新操作時(shí),方法內(nèi)部會(huì)根據(jù)主鍵查詢(xún)?cè)撚涗?,?dǎo)致批量更新操作十分緩慢
具體代碼如下:
@Transactional(rollbackFor = Exception.class) @Override public boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize) { TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass); Assert.notNull(tableInfo, "error: can not execute. because can not find cache of TableInfo for entity!"); String keyProperty = tableInfo.getKeyProperty(); Assert.notEmpty(keyProperty, "error: can not execute. because can not find column for id from entity!"); return SqlHelper.saveOrUpdateBatch(this.entityClass, this.mapperClass, this.log, entityList, batchSize, (sqlSession, entity) -> { Object idVal = tableInfo.getPropertyValue(entity, keyProperty); // 當(dāng)主鍵存在時(shí),會(huì)執(zhí)行sqlSession.selectList(getSqlStatement(SqlMethod.SELECT_BY_ID), entity)查詢(xún)?cè)摋l記錄 return StringUtils.checkValNull(idVal) || CollectionUtils.isEmpty(sqlSession.selectList(getSqlStatement(SqlMethod.SELECT_BY_ID), entity)); }, (sqlSession, entity) -> { MapperMethod.ParamMap<T> param = new MapperMethod.ParamMap<>(); param.put(Constants.ENTITY, entity); sqlSession.update(getSqlStatement(SqlMethod.UPDATE_BY_ID), param); }); }
public static <E> boolean saveOrUpdateBatch(Class<?> entityClass, Class<?> mapper, Log log, Collection<E> list, int batchSize, BiPredicate<SqlSession, E> predicate, BiConsumer<SqlSession, E> consumer) { String sqlStatement = getSqlStatement(mapper, SqlMethod.INSERT_ONE); return executeBatch(entityClass, log, list, batchSize, (sqlSession, entity) -> { // 執(zhí)行predicate if (predicate.test(sqlSession, entity)) { sqlSession.insert(sqlStatement, entity); } else { consumer.accept(sqlSession, entity); } }); }
public static <E> boolean executeBatch(Class<?> entityClass, Log log, Collection<E> list, int batchSize, BiConsumer<SqlSession, E> consumer) { Assert.isFalse(batchSize < 1, "batchSize must not be less than one"); return !CollectionUtils.isEmpty(list) && executeBatch(entityClass, log, sqlSession -> { int size = list.size(); int idxLimit = Math.min(batchSize, size); int i = 1; for (E element : list) { // 循環(huán)每條記錄,執(zhí)行consumer,在此次調(diào)用中,consumer中會(huì)執(zhí)行predicate,當(dāng)該條記錄主鍵不為空時(shí),會(huì)通過(guò)主鍵查詢(xún)?cè)撚涗? consumer.accept(sqlSession, element); if (i == idxLimit) { sqlSession.flushStatements(); idxLimit = Math.min(idxLimit + batchSize, size); } i++; } }); }
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
slf4j?jcl?jul?log4j1?log4j2?logback各組件系統(tǒng)日志切換
這篇文章主要介紹了slf4j、jcl、jul、log4j1、log4j2、logback的大總結(jié),各個(gè)組件的jar包以及目前系統(tǒng)日志需要切換實(shí)現(xiàn)方式的方法,有需要的朋友可以借鑒參考下2022-03-03java.Net.UnknownHostException異常處理問(wèn)題解決
這篇文章主要介紹了java.Net.UnknownHostException異常處理方法,問(wèn)題原因是在系統(tǒng)的?/etc/Hostname中配置了主機(jī)名,而在/etc/hosts文件中沒(méi)有相應(yīng)的配置,本文給大家詳細(xì)講解,需要的朋友可以參考下2023-03-03如何手動(dòng)安裝Gradle并配置IDEA使用Gradle構(gòu)建
本文給大家分享手動(dòng)安裝Gradle并配置IDEA使用Gradle構(gòu)建的步驟,本文給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2025-04-04淺析Java進(jìn)制轉(zhuǎn)換、輸入、命名問(wèn)題
這篇文章主要介紹了Java進(jìn)制轉(zhuǎn)換、輸入、命名問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07基于A(yíng)rrayList源碼解析(基于JDK1.8)
這篇文章主要介紹了關(guān)于A(yíng)rrayList源碼解析(基于JDK1.8),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03詳解spring boot集成ehcache 2.x 用于hibernate二級(jí)緩存
本篇文章主要介紹了詳解spring boot集成ehcache 2.x 用于hibernate二級(jí)緩存,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05基于SpringBoot實(shí)現(xiàn)代碼在線(xiàn)運(yùn)行工具
這篇文章主要介紹了如何利用SpringBoot實(shí)現(xiàn)簡(jiǎn)單的代碼在線(xiàn)運(yùn)行工具(類(lèi)似于菜鳥(niǎo)工具),文中的示例代碼講解詳細(xì),需要的可以參考一下2022-06-06JAVA 獲取系統(tǒng)當(dāng)前時(shí)間實(shí)例代碼
這篇文章主要介紹了JAVA 獲取系統(tǒng)當(dāng)前時(shí)間實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-10-10