mybatis-plus 擴展批量新增的實現
前言
最近發(fā)現公司的微服務項目中沒有統一的批量新增方法,公司用的是MP插件,遇到批量新增都是單獨去去編寫xml實現,費時費力,而MP自帶的批插方法只是實現了分批條sql,跟真正意義上一條sql實現批插還是有很大的性能差異,所以決定實現一個統一的批插方法。
一、MP如何擴展批量新增方法?
MP給我們預留了一個可以真正實現批插的插件InsertBatchSomeColumn,但是只是針對mysql數據庫的,我們可以參照它稍加改造擴展一個針對oracle或者其他數據庫的批插插件類,然后通過sql注入器的方式注入并初始化,就可以實現真正的批插了。
二、實現步驟
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;
? ? ? ? // 表包含主鍵處理邏輯,如果不包含主鍵當普通字段處理
? ? ? ? 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è)務代碼實現
代碼如下(示例):
public interface MyBaseMapper<T> extends BaseMapper<T> {
? ? /**
? ? ?* 批量插入 (mysql)
? ? ?* @param entityList 實體列表
? ? ?* @return 影響行數
? ? ?*/
? ? int insertBatchSomeColumn(Collection<T> entityList);
? ? /**
? ? ?* 批量插入 (oracle)
? ? ?* @param entityList 實體列表
? ? ?* @return 影響行數
? ? ?*/
? ? 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(), "插入失敗,請聯系管理員");
? ? }? ? /**
? ? ?* 批量插入記錄(選擇字段,策略插入)
? ? ?*
? ? ?* @param entityList 實體對象集合
? ? ?* @param batchSize 每批次插入數
? ? ?*/
? ? 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;
? ? ? ? //保存單批提交的數據集合
? ? ? ? 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);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? //每次提交后需要清空集合數據
? ? ? ? ? ? ? ? batchList.clear();
? ? ? ? ? ? ? ? idxLimit = Math.min(idxLimit + batchSize, size);
? ? ? ? ? ? ? ? ServiceAssert.isTrue(res == entityList.size(), "插入失敗,請聯系管理員");
? ? ? ? ? ? }
? ? ? ? }
? ? }總結
遇到問題可以看看源碼,說不定作者已經給你留好路了。
到此這篇關于mybatis-plus 擴展批量新增的實現的文章就介紹到這了,更多相關mybatis-plus 批量新增內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
PrintStream和PrintWriter的區(qū)別簡介
這篇文章主要介紹了PrintStream和PrintWriter的區(qū)別簡介,具有一定借鑒價值,需要的朋友可以參考下2018-01-01

