Quellcode durchsuchen

调整Mapper方法生成(默认单索引生成).

nieqiurong vor 3 Monaten
Ursprung
Commit
d36160b0a6

+ 40 - 7
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/index/DefaultGenerateMapperLambdaMethodHandler.java

@@ -16,10 +16,13 @@
 package com.baomidou.mybatisplus.generator.index;
 
 import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
+import com.baomidou.mybatisplus.core.toolkit.StringUtils;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper;
 import com.baomidou.mybatisplus.extension.kotlin.KtUpdateWrapper;
 import com.baomidou.mybatisplus.generator.config.GlobalConfig;
+import com.baomidou.mybatisplus.generator.config.StrategyConfig;
+import com.baomidou.mybatisplus.generator.config.builder.Entity;
 import com.baomidou.mybatisplus.generator.config.po.TableField;
 import com.baomidou.mybatisplus.generator.config.po.TableInfo;
 import com.baomidou.mybatisplus.generator.jdbc.DatabaseMetaDataWrapper;
@@ -35,28 +38,47 @@ import java.util.stream.Collectors;
 
 /**
  * 使用Lambda方式生成索引方法
+ * <p>复合索引下,第一个字段不会判空,后续字段会进行判空处理,也就是只能保证第一个字段不传递空,无法解决掉索引中间项传递为空的情况</p>
+ * <p>由于需求不一样,默认只处理单字段索引,如果默认复合索引的方案符合你的要求,你可以考虑{@link #singleIndex}设置成false</p>
  *
  * @author nieqiurong
  * @since 3.5.10
  */
 public class DefaultGenerateMapperLambdaMethodHandler extends AbstractMapperMethodHandler {
 
+    /**
+     * 只生成单索引字段方法(默认true)
+     * <p>当设置为true时,代表会过滤掉复合索引</p>
+     */
+    private final boolean singleIndex;
+
+    public DefaultGenerateMapperLambdaMethodHandler() {
+        this(true);
+    }
+
+    public DefaultGenerateMapperLambdaMethodHandler(boolean singleIndex) {
+        this.singleIndex = singleIndex;
+    }
+
     @Override
     public List<MapperMethod> getMethodList(TableInfo tableInfo) {
         Map<String, List<DatabaseMetaDataWrapper.Index>> indexlistMap = tableInfo.getIndexList().stream()
             .collect(Collectors.groupingBy(DatabaseMetaDataWrapper.Index::getName));
         String entityName = tableInfo.getEntityName();
         GlobalConfig globalConfig = tableInfo.getGlobalConfig();
+        StrategyConfig strategyConfig = tableInfo.getStrategyConfig();
+        Entity entity = strategyConfig.entity();
         Set<Map.Entry<String, List<DatabaseMetaDataWrapper.Index>>> entrySet = indexlistMap.entrySet();
         List<MapperMethod> methodList = new ArrayList<>();
         for (Map.Entry<String, List<DatabaseMetaDataWrapper.Index>> entry : entrySet) {
             String indexName = entry.getKey();
             List<DatabaseMetaDataWrapper.Index> indexList = entry.getValue();
             int indexSize = indexList.size();
-            if ("PRIMARY".equals(indexName)) {
-                if (indexSize == 1) {
-                    continue;
-                }
+            if (this.singleIndex && indexSize > 1) {
+                continue;
+            }
+            if ("PRIMARY".equals(indexName) && indexSize == 1) {
+                continue;
             }
             Map<String, TableField> tableFieldMap = tableInfo.getTableFieldMap();
             StringBuilder baseMethodNameBuilder = new StringBuilder();
@@ -70,6 +92,10 @@ public class DefaultGenerateMapperLambdaMethodHandler extends AbstractMapperMeth
                     uniqueKey = true;
                 }
                 TableField tableField = tableFieldMap.get(index.getColumnName());
+                if (index.getColumnName().equals(entity.getLogicDeleteColumnName())
+                    || tableField.getPropertyName().equals(entity.getLogicDeletePropertyName())) {
+                    continue;
+                }
                 tableFieldList.add(tableField);
                 baseMethodNameBuilder.append(tableField.getCapitalName());
                 if (indexSize > 1) {
@@ -96,8 +122,11 @@ public class DefaultGenerateMapperLambdaMethodHandler extends AbstractMapperMeth
                     argsBuilder.append(", ");
                 }
             }
-            boolean returnList = (indexSize > 1 || !uniqueKey);
             String baseMethodName = baseMethodNameBuilder.toString();
+            if (StringUtils.isBlank(baseMethodName)) {
+                continue;
+            }
+            boolean returnList = (indexSize > 1 || !uniqueKey);
             String args = argsBuilder.toString();
             String baseWrapper = baseWrapperBuilder.toString();
             if (globalConfig.isKotlin()) {
@@ -142,8 +171,12 @@ public class DefaultGenerateMapperLambdaMethodHandler extends AbstractMapperMeth
     public Set<String> getImportPackages(TableInfo tableInfo) {
         GlobalConfig globalConfig = tableInfo.getGlobalConfig();
         Set<String> imports = new HashSet<>();
-        imports.add(ObjectUtils.class.getName());
-        imports.add(List.class.getName());
+        if (!singleIndex) {
+            imports.add(ObjectUtils.class.getName());
+        }
+        if (!globalConfig.isKotlin()) {
+            imports.add(List.class.getName());
+        }
         if (globalConfig.isKotlin()) {
             imports.add(KtQueryWrapper.class.getName());
             imports.add(KtUpdateWrapper.class.getName());

+ 36 - 6
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/index/DefaultGenerateMapperMethodHandler.java

@@ -16,6 +16,7 @@
 package com.baomidou.mybatisplus.generator.index;
 
 import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
+import com.baomidou.mybatisplus.core.toolkit.StringUtils;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.generator.config.GlobalConfig;
 import com.baomidou.mybatisplus.generator.config.builder.Entity;
@@ -34,6 +35,8 @@ import java.util.stream.Collectors;
 
 /**
  * 按字符串或者字符串常量方法生成查询条件
+ * <p>复合索引下,第一个字段不会判空,后续字段会进行判空处理,也就是只能保证第一个字段不传递空,无法解决掉索引中间项传递为空的情况</p>
+ * <p>由于需求不一样,默认只处理单字段索引,如果默认复合索引的方案符合你的要求,你可以考虑{@link #singleIndex}设置成false</p>
  *
  * @author nieqiurong
  * @see Entity.Builder#enableColumnConstant()
@@ -41,6 +44,20 @@ import java.util.stream.Collectors;
  */
 public class DefaultGenerateMapperMethodHandler extends AbstractMapperMethodHandler {
 
+    /**
+     * 只生成单索引字段方法(默认true)
+     * <p>当设置为true时,代表会过滤掉复合索引</p>
+     */
+    private final boolean singleIndex;
+
+    public DefaultGenerateMapperMethodHandler() {
+        this(true);
+    }
+
+    public DefaultGenerateMapperMethodHandler(boolean singleIndex) {
+        this.singleIndex = singleIndex;
+    }
+
     @Override
     public List<MapperMethod> getMethodList(TableInfo tableInfo) {
         Map<String, List<DatabaseMetaDataWrapper.Index>> indexlistMap = tableInfo.getIndexList().stream()
@@ -55,10 +72,11 @@ public class DefaultGenerateMapperMethodHandler extends AbstractMapperMethodHand
             String indexName = entry.getKey();
             List<DatabaseMetaDataWrapper.Index> indexList = entry.getValue();
             int indexSize = indexList.size();
-            if ("PRIMARY".equals(indexName)) {
-                if (indexSize == 1) {
-                    continue;
-                }
+            if (this.singleIndex && indexSize > 1) {
+                continue;
+            }
+            if ("PRIMARY".equals(indexName) && indexSize == 1) {
+                continue;
             }
             List<TableField> tableFieldList = new ArrayList<>();
             Map<String, TableField> tableFieldMap = tableInfo.getTableFieldMap();
@@ -72,6 +90,10 @@ public class DefaultGenerateMapperMethodHandler extends AbstractMapperMethodHand
                     uniqueKey = true;
                 }
                 TableField tableField = tableFieldMap.get(index.getColumnName());
+                if (index.getColumnName().equals(entity.getLogicDeleteColumnName())
+                    || tableField.getPropertyName().equals(entity.getLogicDeletePropertyName())) {
+                    continue;
+                }
                 tableFieldList.add(tableField);
                 baseMethodNameBuilder.append(tableField.getCapitalName());
                 if (indexSize > 1) {
@@ -101,6 +123,9 @@ public class DefaultGenerateMapperMethodHandler extends AbstractMapperMethodHand
                 }
             }
             String baseMethodName = baseMethodNameBuilder.toString();
+            if (StringUtils.isBlank(baseMethodNameBuilder)) {
+                continue;
+            }
             String args = argsBuilder.toString();
             String baseWrapper = baseWrapperBuilder.toString();
             boolean returnList = (indexSize > 1 || !uniqueKey);
@@ -145,9 +170,14 @@ public class DefaultGenerateMapperMethodHandler extends AbstractMapperMethodHand
 
     @Override
     public Set<String> getImportPackages(TableInfo tableInfo) {
+        GlobalConfig globalConfig = tableInfo.getGlobalConfig();
         Set<String> imports = new HashSet<>();
-        imports.add(ObjectUtils.class.getName());
-        imports.add(List.class.getName());
+        if (!singleIndex) {
+            imports.add(ObjectUtils.class.getName());
+        }
+        if (!globalConfig.isKotlin()) {
+            imports.add(List.class.getName());
+        }
         imports.add(Wrappers.class.getName());
         return imports;
     }