日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Mybatis-Plus批量新增

發(fā)布時間:2023/12/20 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Mybatis-Plus批量新增 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

背景:提供的insertBatch是假批量,重復(fù)的IO連接與斷開效率極低,提供了insertBatchSomeColumn真批量需要自己手動配置

直接上代碼:

public interface MyMapper<T> extends BaseMapper<T> {/*** 默認(rèn)批次提交數(shù)量*/int DEFAULT_BATCH_SIZE = 1000;/*** 批量新增數(shù)據(jù),自選字段 insert. 自動按每批1000插入數(shù)據(jù)庫* 此填充不會填充 FieldFill.UPDATE 的字段。* 注意數(shù)據(jù)庫默認(rèn)更新的字段也需要手工設(shè)置** @param entityList 數(shù)據(jù)* @return 插入條數(shù)*/@Transactional(rollbackFor = Exception.class)default int insertBatch(List<T> entityList) {return this.insertBatchSomeColumn(entityList, DEFAULT_BATCH_SIZE);}/*** 批量新增數(shù)據(jù),自選字段 insert* 不會分批插入,需要分批請調(diào)用方法insertBatch或者 insertBatchSomeColumn(List<T> entityList, int size)* 此填充不會填充 FieldFill.UPDATE 的字段。* 注意數(shù)據(jù)庫默認(rèn)更新的字段也需要手工設(shè)置** @param entityList 數(shù)據(jù)* @return 插入條數(shù)*/int insertBatchSomeColumn(List<T> entityList);/*** 分批插入。每次插入* @param entityList 原實體對象* @param size 分批大小* @return 總插入記錄*/@Transactional(rollbackFor = Exception.class)default int insertBatchSomeColumn(List<T> entityList, int size) {if (CollUtil.isEmpty(entityList)) {return 0;}List<List<T>> split = CollUtil.split(entityList, size);return split.stream().mapToInt(this::insertBatchSomeColumn).sum();} }

有使用工具類,可以引入以下依賴

<dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.8.3</version> </dependency>

以下的操作也需要,要不可能會有其他bug:

public class MySqlInjector extends DefaultSqlInjector {@Overridepublic List<AbstractMethod> getMethodList(Class<?> mapperClass) {List<AbstractMethod> methodList = super.getMethodList(mapperClass);// 例: 不要指定了 update 填充的字段methodList.add(new InsertBatchSomeColumn(i -> i.getFieldFill() != FieldFill.UPDATE));return methodList;} } @Configuration public class MybatisPlusConfig {/*** 自定義內(nèi)置選裝件* @return*/@Beanpublic MySqlInjector sqlInjector() {return new MySqlInjector();} }

總結(jié)

以上是生活随笔為你收集整理的Mybatis-Plus批量新增的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。