欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

mybatis-plus 擴展批量新增的實現(xiàn)

 更新時間:2023年01月09日 14:40:58   作者:JOEY P  
本文主要介紹了mybatis-plus 擴展批量新增的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

前言

最近發(fā)現(xiàn)公司的微服務(wù)項目中沒有統(tǒng)一的批量新增方法,公司用的是MP插件,遇到批量新增都是單獨去去編寫xml實現(xiàn),費時費力,而MP自帶的批插方法只是實現(xiàn)了分批條sql,跟真正意義上一條sql實現(xiàn)批插還是有很大的性能差異,所以決定實現(xiàn)一個統(tǒng)一的批插方法。

一、MP如何擴展批量新增方法?

MP給我們預(yù)留了一個可以真正實現(xiàn)批插的插件InsertBatchSomeColumn,但是只是針對mysql數(shù)據(jù)庫的,我們可以參照它稍加改造擴展一個針對oracle或者其他數(shù)據(jù)庫的批插插件類,然后通過sql注入器的方式注入并初始化,就可以實現(xiàn)真正的批插了。

二、實現(xiàn)步驟

1.擴展批插類

代碼如下(示例):

public class InsertBatchSomeColumnOracle extends AbstractMethod {

? ? /**
? ? ?* 字段篩選條件
? ? ?*/
? ? @Setter
? ? @Accessors(chain = true)
? ? private Predicate<TableFieldInfo> predicate;

? ? @SuppressWarnings("Duplicates")
? ? @Override
? ? public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
? ? ? ? KeyGenerator keyGenerator = new NoKeyGenerator();
? ? ? ? SqlMethod sqlMethod = SqlMethod.INSERT_ALL;
? ? ? ? List<TableFieldInfo> fieldList = tableInfo.getFieldList();
? ? ? ? String insertSqlColumn = tableInfo.getKeyInsertSqlColumn(false) +
? ? ? ? ? ? ? ? this.filterTableFieldInfo(fieldList, predicate, TableFieldInfo::getInsertSqlColumn, EMPTY);
? ? ? ? String tableName = tableInfo.getTableName();
? ? ? ? String columnScript = LEFT_BRACKET + insertSqlColumn.substring(0, insertSqlColumn.length() - 1) + RIGHT_BRACKET;
? ? ? ? String insertSqlProperty = tableInfo.getKeyInsertSqlProperty(ENTITY_DOT, false) +
? ? ? ? ? ? ? ? this.filterTableFieldInfo(fieldList, predicate, i -> i.getInsertSqlProperty(ENTITY_DOT), EMPTY);
? ? ? ? insertSqlProperty = " into " + tableName + columnScript + " values " + LEFT_BRACKET + insertSqlProperty.substring(0, insertSqlProperty.length() - 1) + RIGHT_BRACKET;
? ? ? ? String batchScript = SqlScriptUtils.convertForeach(insertSqlProperty, "list", null, ENTITY, EMPTY);
? ? ? ? String keyProperty = null;
? ? ? ? String keyColumn = null;
? ? ? ? // 表包含主鍵處理邏輯,如果不包含主鍵當(dāng)普通字段處理
? ? ? ? if (tableInfo.havePK()) {
? ? ? ? ? ? if (tableInfo.getIdType() == IdType.AUTO) {
? ? ? ? ? ? ? ? /* 自增主鍵 */
? ? ? ? ? ? ? ? keyGenerator = new Jdbc3KeyGenerator();
? ? ? ? ? ? ? ? keyProperty = tableInfo.getKeyProperty();
? ? ? ? ? ? ? ? keyColumn = tableInfo.getKeyColumn();
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? if (null != tableInfo.getKeySequence()) {
? ? ? ? ? ? ? ? ? ? keyGenerator = TableInfoHelper.genKeyGenerator(getMethod(), tableInfo, builderAssistant);
? ? ? ? ? ? ? ? ? ? keyProperty = tableInfo.getKeyProperty();
? ? ? ? ? ? ? ? ? ? keyColumn = tableInfo.getKeyColumn();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? String sql = String.format(sqlMethod.getSql(), batchScript);
? ? ? ? SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
? ? ? ? return this.addInsertMappedStatement(mapperClass, modelClass, getMethod(), sqlSource, keyGenerator, keyProperty, keyColumn);
? ? }

? ? public String getMethod() {
? ? ? ? // 自定義 mapper 方法名
? ? ? ? return "insertBatchSomeColumnOracle";
? ? }
}

2.編寫sql注入器

代碼如下(示例):

public class MpSqlInjector extends DefaultSqlInjector {

? ? @Override
? ? public List<AbstractMethod> getMethodList(Class<?> mapperClass) {
? ? ? ? // 防止父類的方法不可使用
? ? ? ? List<AbstractMethod> methodList = super.getMethodList(mapperClass);
? ? ? ? // 添加批量插入的方法
? ? ? ? methodList.add(new InsertBatchSomeColumn());
? ? ? ? methodList.add(new InsertBatchSomeColumnOracle());
? ? ? ? return methodList;
? ? }
}

3.spring容器中實例化sql注入器

代碼如下(示例):

@Bean
public MpSqlInjector mpSqlInjector() {
? ? return new MpSqlInjector();
}

4.業(yè)務(wù)代碼實現(xiàn)

代碼如下(示例):

public interface MyBaseMapper<T> extends BaseMapper<T> {

? ? /**
? ? ?* 批量插入 (mysql)
? ? ?* @param entityList 實體列表
? ? ?* @return 影響行數(shù)
? ? ?*/
? ? int insertBatchSomeColumn(Collection<T> entityList);

? ? /**
? ? ?* 批量插入 (oracle)
? ? ?* @param entityList 實體列表
? ? ?* @return 影響行數(shù)
? ? ?*/
? ? int insertBatchSomeColumnOracle(Collection<T> entityList);
}


/**
? ? ?* 批量插入記錄(選擇字段,策略插入)
? ? ?*
? ? ?* @param entityList 實體對象集合
? ? ?*/
? ? public void saveBatch(Collection<T> entityList) {
? ? ? ? if (DEFAULT_MUST_SAVE_BATCH) {
? ? ? ? ? ? ServiceAssert.isTrue(CollectionUtil.isNotEmpty(entityList), "批量插入傳入空集合, 插入失敗");
? ? ? ? }
? ? ? ? if (CollectionUtil.isEmpty(entityList)) {
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? int res;
? ? ? ? if (DATABASE_TYPE_ORACLE.equalsIgnoreCase(dbType)) {
? ? ? ? ? ? res = getBaseMapper().insertBatchSomeColumnOracle(entityList);
? ? ? ? } else {
? ? ? ? ? ? res = getBaseMapper().insertBatchSomeColumn(entityList);
? ? ? ? }
? ? ? ? ServiceAssert.isTrue(res == entityList.size(), "插入失敗,請聯(lián)系管理員");
? ? }
? ? /**
? ? ?* 批量插入記錄(選擇字段,策略插入)
? ? ?*
? ? ?* @param entityList 實體對象集合
? ? ?* @param batchSize 每批次插入數(shù)
? ? ?*/
? ? public void saveBatch(Collection<T> entityList, int batchSize) {
? ? ? ? if (DEFAULT_MUST_SAVE_BATCH) {
? ? ? ? ? ? ServiceAssert.isTrue(CollectionUtil.isNotEmpty(entityList), "批量插入傳入空集合, 插入失敗");
? ? ? ? }
? ? ? ? if (CollectionUtil.isEmpty(entityList)) {
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? int size = entityList.size();
? ? ? ? int idxLimit = Math.min(batchSize, size);
? ? ? ? int i = 1;
? ? ? ? //保存單批提交的數(shù)據(jù)集合
? ? ? ? List<T> batchList = new ArrayList<>();
? ? ? ? for(Iterator<T> it = entityList.iterator(); it.hasNext(); ++i) {
? ? ? ? ? ? T element = it.next();
? ? ? ? ? ? batchList.add(element);
? ? ? ? ? ? int res;
? ? ? ? ? ? if (i == idxLimit) {
? ? ? ? ? ? ? ? if (DATABASE_TYPE_ORACLE.equalsIgnoreCase(dbType)) {
? ? ? ? ? ? ? ? ? ? res = getBaseMapper().insertBatchSomeColumnOracle(batchList);
? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? res = getBaseMapper().insertBatchSomeColumn(batchList);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? //每次提交后需要清空集合數(shù)據(jù)
? ? ? ? ? ? ? ? batchList.clear();
? ? ? ? ? ? ? ? idxLimit = Math.min(idxLimit + batchSize, size);
? ? ? ? ? ? ? ? ServiceAssert.isTrue(res == entityList.size(), "插入失敗,請聯(lián)系管理員");
? ? ? ? ? ? }
? ? ? ? }
? ? }

總結(jié)

遇到問題可以看看源碼,說不定作者已經(jīng)給你留好路了。

到此這篇關(guān)于mybatis-plus 擴展批量新增的實現(xiàn)的文章就介紹到這了,更多相關(guān)mybatis-plus 批量新增內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論