From 66c6bfa0fb8a06a644795eec8cbbea2e925f8676 Mon Sep 17 00:00:00 2001 From: misworga831 Date: Mon, 6 Nov 2023 15:19:53 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BC=98=E5=8C=96=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E5=88=86=E8=AF=8D=E6=8F=92=E5=85=A5=EF=BC=88=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E4=B8=BA=E6=8F=92=E5=85=A5=E6=97=B6=EF=BC=8C=E9=81=87?= =?UTF-8?q?=E5=88=B0=E5=94=AF=E4=B8=80=E7=B4=A2=E5=BC=95=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E8=B7=B3=E8=BF=87=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../serviceimpl/CustomWordsServiceImpl.java | 2 +- .../serviceimpl/EsGoodsIndexServiceImpl.java | 1 - .../external/InsertIgnoreBatchAllColumn.java | 68 +++++++++++++++++++ .../mybatisplus/external/SpiceBaseMapper.java | 3 + .../external/SpiceSqlInjector.java | 1 + 5 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 framework/src/main/java/cn/lili/mybatis/mybatisplus/external/InsertIgnoreBatchAllColumn.java diff --git a/framework/src/main/java/cn/lili/modules/search/serviceimpl/CustomWordsServiceImpl.java b/framework/src/main/java/cn/lili/modules/search/serviceimpl/CustomWordsServiceImpl.java index 2b8c9e1c5..74070d021 100644 --- a/framework/src/main/java/cn/lili/modules/search/serviceimpl/CustomWordsServiceImpl.java +++ b/framework/src/main/java/cn/lili/modules/search/serviceimpl/CustomWordsServiceImpl.java @@ -102,7 +102,7 @@ public class CustomWordsServiceImpl extends ServiceImpl customWordsList) { - return this.baseMapper.insertBatchSomeColumn(customWordsList); + return this.baseMapper.insertIgnoreBatchAllColumn(customWordsList); } /** diff --git a/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsIndexServiceImpl.java b/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsIndexServiceImpl.java index 0e44fd144..a5a95d062 100644 --- a/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsIndexServiceImpl.java +++ b/framework/src/main/java/cn/lili/modules/search/serviceimpl/EsGoodsIndexServiceImpl.java @@ -368,7 +368,6 @@ public class EsGoodsIndexServiceImpl extends BaseElasticsearchService implements keywordsList.forEach(item -> customWordsArrayList.add(new CustomWords(item))); //这里采用先批量删除再插入的方法,故意这么做。否则需要挨个匹配是否存在,性能消耗更大 if (CollUtil.isNotEmpty(customWordsArrayList)) { - customWordsService.deleteBathByName(keywordsList); customWordsService.insertBatchCustomWords(customWordsArrayList); } } catch (Exception e) { diff --git a/framework/src/main/java/cn/lili/mybatis/mybatisplus/external/InsertIgnoreBatchAllColumn.java b/framework/src/main/java/cn/lili/mybatis/mybatisplus/external/InsertIgnoreBatchAllColumn.java new file mode 100644 index 000000000..9b3bf815e --- /dev/null +++ b/framework/src/main/java/cn/lili/mybatis/mybatisplus/external/InsertIgnoreBatchAllColumn.java @@ -0,0 +1,68 @@ +package cn.lili.mybatis.mybatisplus.external; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.core.enums.SqlMethod; +import com.baomidou.mybatisplus.core.injector.AbstractMethod; +import com.baomidou.mybatisplus.core.metadata.TableFieldInfo; +import com.baomidou.mybatisplus.core.metadata.TableInfo; +import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; +import com.baomidou.mybatisplus.core.toolkit.StringUtils; +import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils; +import lombok.Setter; +import lombok.experimental.Accessors; +import org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator; +import org.apache.ibatis.executor.keygen.KeyGenerator; +import org.apache.ibatis.executor.keygen.NoKeyGenerator; +import org.apache.ibatis.mapping.MappedStatement; +import org.apache.ibatis.mapping.SqlSource; + +import java.util.List; +import java.util.function.Predicate; + +public class InsertIgnoreBatchAllColumn extends AbstractMethod { + + private static final String MAPPER_METHOD = "insertIgnoreBatchAllColumn"; + private static final long serialVersionUID = 9014932405041929700L; + + @Setter + @Accessors(chain = true) + private Predicate predicate; + + @SuppressWarnings("Duplicates") + @Override + public MappedStatement injectMappedStatement(Class mapperClass, Class modelClass, TableInfo tableInfo) { + KeyGenerator keyGenerator = new NoKeyGenerator(); + SqlMethod sqlMethod = SqlMethod.INSERT_ONE; + String sqlTemplate = ""; + + List fieldList = tableInfo.getFieldList(); + String insertSqlColumn = tableInfo.getKeyInsertSqlColumn(true, false) + + this.filterTableFieldInfo(fieldList, predicate, TableFieldInfo::getInsertSqlColumn, EMPTY); + String columnScript = LEFT_BRACKET + insertSqlColumn.substring(0, insertSqlColumn.length() - 1) + RIGHT_BRACKET; + String insertSqlProperty = tableInfo.getKeyInsertSqlProperty(true, ENTITY_DOT, false) + + this.filterTableFieldInfo(fieldList, predicate, i -> i.getInsertSqlProperty(ENTITY_DOT), EMPTY); + insertSqlProperty = LEFT_BRACKET + insertSqlProperty.substring(0, insertSqlProperty.length() - 1) + RIGHT_BRACKET; + String valuesScript = SqlScriptUtils.convertForeach(insertSqlProperty, "list", null, ENTITY, COMMA); + String keyProperty = null; + String keyColumn = null; + // 表包含主键处理逻辑,如果不包含主键当普通字段处理 + if (StringUtils.isNotEmpty(tableInfo.getKeyProperty())) { + if (tableInfo.getIdType() == IdType.AUTO) { + /* 自增主键 */ + keyGenerator = new Jdbc3KeyGenerator(); + keyProperty = tableInfo.getKeyProperty(); + keyColumn = tableInfo.getKeyColumn(); + } else { + if (null != tableInfo.getKeySequence()) { + keyGenerator = TableInfoHelper.genKeyGenerator(sqlMethod.getMethod(), tableInfo, builderAssistant); + keyProperty = tableInfo.getKeyProperty(); + keyColumn = tableInfo.getKeyColumn(); + } + } + } + String sql = String.format(sqlTemplate, tableInfo.getTableName(), columnScript, valuesScript); + SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass); + return this.addInsertMappedStatement(mapperClass, modelClass, MAPPER_METHOD, sqlSource, keyGenerator, keyProperty, keyColumn); + } +} + diff --git a/framework/src/main/java/cn/lili/mybatis/mybatisplus/external/SpiceBaseMapper.java b/framework/src/main/java/cn/lili/mybatis/mybatisplus/external/SpiceBaseMapper.java index 8d56ccecb..23a76fe68 100644 --- a/framework/src/main/java/cn/lili/mybatis/mybatisplus/external/SpiceBaseMapper.java +++ b/framework/src/main/java/cn/lili/mybatis/mybatisplus/external/SpiceBaseMapper.java @@ -1,5 +1,6 @@ package cn.lili.mybatis.mybatisplus.external; + import java.util.List; /** @@ -16,4 +17,6 @@ public interface SpiceBaseMapper { * @return 成功插入的数据条数 */ long insertBatchSomeColumn(List entityList); + + int insertIgnoreBatchAllColumn(List list); } diff --git a/framework/src/main/java/cn/lili/mybatis/mybatisplus/external/SpiceSqlInjector.java b/framework/src/main/java/cn/lili/mybatis/mybatisplus/external/SpiceSqlInjector.java index 5481579ae..78f98eeb5 100644 --- a/framework/src/main/java/cn/lili/mybatis/mybatisplus/external/SpiceSqlInjector.java +++ b/framework/src/main/java/cn/lili/mybatis/mybatisplus/external/SpiceSqlInjector.java @@ -28,6 +28,7 @@ public class SpiceSqlInjector extends DefaultSqlInjector { // methodList.add(new InsertBatchSomeColumn(t -> !t.isLogicDelete() && !"update_time".equals(t.getColumn()))); // 要逻辑删除 t.isLogicDelete() 默认不要 methodList.add(new InsertBatchSomeColumn(t -> !t.isLogicDelete())); + methodList.add(new InsertIgnoreBatchAllColumn()); return methodList; } }