Quellcode durchsuchen

代码生成器允许正则表达式匹配表名

hubin vor 6 Jahren
Ursprung
Commit
69d6367aa0

+ 18 - 24
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/toolkit/StringUtils.java

@@ -15,18 +15,17 @@
  */
 package com.baomidou.mybatisplus.core.toolkit;
 
-import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
-import com.baomidou.mybatisplus.core.toolkit.sql.StringEscape;
-
 import java.sql.Blob;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
-import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 import java.util.stream.Collectors;
 
+import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
+import com.baomidou.mybatisplus.core.toolkit.sql.StringEscape;
+
 /**
  * <p>
  * String 工具类
@@ -60,16 +59,11 @@ public class StringUtils {
      * 占位符
      */
     public static final String PLACE_HOLDER = "{%s}";
-    private static final Pattern PATTERN_ONE = Pattern.compile(".*[A-Z]+.*");
-    private static final Pattern PATTERN_TWO = Pattern.compile(".*[/_]+.*");
-    private static boolean separatorBeforeDigit = false;
-    private static boolean separatorAfterDigit = true;
-
 
     private StringUtils() {
+        // to do nothing
     }
 
-
     /**
      * <p>
      * Blob 转为 String 格式
@@ -220,7 +214,7 @@ public class StringUtils {
      * @return
      */
     public static boolean isUpperCase(String str) {
-        return match("^[A-Z]+$", str);
+        return matches("^[A-Z]+$", str);
     }
 
     /**
@@ -229,13 +223,14 @@ public class StringUtils {
      * </p>
      *
      * @param regex 正则表达式字符串
-     * @param str   要匹配的字符串
-     * @return 如果str 符合 regex的正则表达式格式,返回true, 否则返回 false;
+     * @param input 要匹配的字符串
+     * @return 如果 input 符合 regex 正则表达式格式, 返回true, 否则返回 false;
      */
-    public static boolean match(String regex, String str) {
-        Pattern pattern = Pattern.compile(regex);
-        Matcher matcher = pattern.matcher(str);
-        return matcher.matches();
+    public static boolean matches(String regex, String input) {
+        if (null == regex || null == input) {
+            return false;
+        }
+        return Pattern.matches(regex, input);
     }
 
     /**
@@ -418,7 +413,7 @@ public class StringUtils {
      * @return
      */
     public static boolean isMixedMode(String word) {
-        return PATTERN_ONE.matcher(word).matches() && PATTERN_TWO.matcher(word).matches();
+        return matches(".*[A-Z]+.*", word) && matches(".*[/_]+.*", word);
     }
 
     /**
@@ -776,17 +771,16 @@ public class StringUtils {
             boolean previousIsWhitespace = Character.isWhitespace(previousChar);
             boolean lastOneIsNotUnderscore = (buf.length() > 0) && (buf.charAt(buf.length() - 1) != '_');
             boolean isNotUnderscore = c != '_';
-            if ((lastOneIsNotUnderscore) && ((isUpperCaseAndPreviousIsLowerCase) || (previousIsWhitespace) || ((betweenUpperCases)
-                && (containsLowerCase) && (isUpperCaseAndPreviousIsUpperCase)))) {
+            if (lastOneIsNotUnderscore && (isUpperCaseAndPreviousIsLowerCase || previousIsWhitespace
+                || (betweenUpperCases && containsLowerCase && isUpperCaseAndPreviousIsUpperCase))) {
                 buf.append("_");
-            } else if (((separatorAfterDigit) && (Character.isDigit(previousChar))
-                && (Character.isLetter(c))) || ((separatorBeforeDigit) && (Character
-                .isDigit(c)) && (Character.isLetter(previousChar)))) {
+            } else if ((Character.isDigit(previousChar) && Character.isLetter(c))
+                || (false && Character.isDigit(c) && Character.isLetter(previousChar))) {
                 buf.append('_');
             }
             if ((shouldReplace(c)) && (lastOneIsNotUnderscore)) {
                 buf.append('_');
-            } else if ((!Character.isWhitespace(c)) && ((isNotUnderscore) || (lastOneIsNotUnderscore))) {
+            } else if (!Character.isWhitespace(c) && (isNotUnderscore || lastOneIsNotUnderscore)) {
                 buf.append(Character.toUpperCase(c));
             }
             previousChar = c;

+ 2 - 2
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/StrategyConfig.java

@@ -97,12 +97,12 @@ public class StrategyConfig {
     private String superControllerClass;
 
     /**
-     * 需要包含的表名(与exclude二选一配置)
+     * 需要包含的表名,允许正则表达式(与exclude二选一配置)
      */
     private String[] include = null;
 
     /**
-     * 需要排除的表名
+     * 需要排除的表名,允许正则表达式
      */
     private String[] exclude = null;
     /**

+ 23 - 7
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/builder/ConfigBuilder.java

@@ -460,19 +460,21 @@ public class ConfigBuilder {
                     tableInfo.setName(tableName);
                     tableInfo.setComment(tableComment);
                     if (isInclude) {
-                        for (String includeTab : config.getInclude()) {
-                            if (includeTab.equalsIgnoreCase(tableName)) {
+                        for (String includeTable : config.getInclude()) {
+                            // 忽略大小写等于 或 正则 true
+                            if (tableNameMatches(includeTable, tableName)) {
                                 includeTableList.add(tableInfo);
                             } else {
-                                notExistTables.add(includeTab);
+                                notExistTables.add(includeTable);
                             }
                         }
                     } else if (isExclude) {
-                        for (String excludeTab : config.getExclude()) {
-                            if (excludeTab.equalsIgnoreCase(tableName)) {
+                        for (String excludeTable : config.getExclude()) {
+                            // 忽略大小写等于 或 正则 true
+                            if (tableNameMatches(excludeTable, tableName)) {
                                 excludeTableList.add(tableInfo);
                             } else {
-                                notExistTables.add(excludeTab);
+                                notExistTables.add(excludeTable);
                             }
                         }
                     }
@@ -481,11 +483,11 @@ public class ConfigBuilder {
                     System.err.println("当前数据库为空!!!");
                 }
             }
+
             // 将已经存在的表移除,获取配置中数据库不存在的表
             for (TableInfo tabInfo : tableList) {
                 notExistTables.remove(tabInfo.getName());
             }
-
             if (notExistTables.size() > 0) {
                 System.err.println("表 " + notExistTables + " 在数据库中不存在!!!");
             }
@@ -521,6 +523,20 @@ public class ConfigBuilder {
     }
 
 
+    /**
+     * <p>
+     * 表名匹配
+     * </p>
+     *
+     * @param setTableName 设置表名
+     * @param dbTableName  数据库表单
+     * @return
+     */
+    private boolean tableNameMatches(String setTableName, String dbTableName) {
+        return setTableName.equalsIgnoreCase(dbTableName)
+            || StringUtils.matches(setTableName, dbTableName);
+    }
+
     /**
      * <p>
      * 将字段信息与表信息关联

+ 1 - 1
mybatis-plus-generator/src/main/resources/templates/controller.java.ftl

@@ -14,7 +14,7 @@ import ${superControllerClassPackage};
 
 /**
  * <p>
- * ${table.comment} 前端控制器
+ * ${table.comment!} 前端控制器
  * </p>
  *
  * @author ${author}

+ 1 - 1
mybatis-plus-generator/src/main/resources/templates/entity.java.ftl

@@ -12,7 +12,7 @@ import lombok.experimental.Accessors;
 
 /**
  * <p>
- * ${table.comment}
+ * ${table.comment!}
  * </p>
  *
  * @author ${author}

+ 1 - 1
mybatis-plus-generator/src/main/resources/templates/mapper.java.ftl

@@ -5,7 +5,7 @@ import ${superMapperClassPackage};
 
 /**
  * <p>
- * ${table.comment} Mapper 接口
+ * ${table.comment!} Mapper 接口
  * </p>
  *
  * @author ${author}

+ 1 - 1
mybatis-plus-generator/src/main/resources/templates/service.java.ftl

@@ -5,7 +5,7 @@ import ${superServiceClassPackage};
 
 /**
  * <p>
- * ${table.comment} 服务类
+ * ${table.comment!} 服务类
  * </p>
  *
  * @author ${author}

+ 1 - 1
mybatis-plus-generator/src/main/resources/templates/serviceImpl.java.ftl

@@ -8,7 +8,7 @@ import org.springframework.stereotype.Service;
 
 /**
  * <p>
- * ${table.comment} 服务实现类
+ * ${table.comment!} 服务实现类
  * </p>
  *
  * @author ${author}

+ 2 - 2
mybatis-plus-generator/src/test/java/com/baomidou/mybatisplus/test/generator/PostgreSQLGenerator.java

@@ -81,8 +81,8 @@ public class PostgreSQLGenerator extends GeneratorTest {
         strategy.setFieldPrefix(new String[]{"A_"});
         strategy.setNaming(NamingStrategy.underline_to_camel);// 表名生成策略
         strategy.setColumnNaming(NamingStrategy.underline_to_camel);// 允许字段策略独立设置,默认为 naming 策略
-        // strategy.setInclude(new String[] { "user" }); // 需要生成的表
-        // strategy.setExclude(new String[]{"test"}); // 排除生成的表
+        strategy.setInclude("sys_user", "^mp.*", "ok"); // 需要生成的表,支持正则表达式
+        // strategy.setExclude("test"); // 排除生成的表,支持正则表达式
         // 自定义实体父类
         // strategy.setSuperEntityClass("com.baomidou.demo.TestEntity");
         // 自定义实体,公共字段

+ 2 - 2
mybatis-plus-generator/src/test/resources/templates/dto.java.ftl

@@ -3,8 +3,8 @@ package ${package.Entity};
 
 /**
  * <p>
-    * 测试自定义DTO模板 ${table.comment}
-    * </p>
+ * 测试自定义DTO模板 ${table.comment!}
+ * </p>
  *
  * 测试注入 ${cfg.abc}
  */