Browse Source

生成器废弃正则表名匹配(暂时完成mysql)

https://gitee.com/baomidou/mybatis-plus/issues/I14OTP
nieqiuqiu 5 năm trước cách đây
mục cha
commit
d185c1b6e8

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

@@ -95,12 +95,14 @@ public class StrategyConfig {
      */
     private String superControllerClass;
     /**
-     * 需要包含的表名,允许正则表达式(与exclude二选一配置)
+     * 需要包含的表名(与exclude二选一配置)
+     * @since 3.2.x 正则匹配不再支持,请使用{@link #setLikeTable(String)}
      */
     @Setter(AccessLevel.NONE)
     private String[] include = null;
     /**
-     * 需要排除的表名,允许正则表达式
+     * 需要排除的表名
+     * @since 3.2.x 正则匹配不再支持,请使用{@link #setNotLikeTable(String)}
      */
     @Setter(AccessLevel.NONE)
     private String[] exclude = null;
@@ -160,6 +162,18 @@ public class StrategyConfig {
      * 表填充字段
      */
     private List<TableFill> tableFillList = null;
+    /**
+     * 包含表名
+     *
+     * @since 3.2.x
+     */
+    private String likeTable;
+    /**
+     * 不包含表名
+     *
+     * @since 3.2.x
+     */
+    private String notLikeTable;
 
     /**
      * 大写命名、字段符合大写字母数字下划线命名

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

@@ -40,6 +40,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.regex.Pattern;
+import java.util.stream.Collectors;
 
 /**
  * 配置汇总 传递给文件生成工具
@@ -409,6 +410,9 @@ public class ConfigBuilder {
         if (isInclude && isExclude) {
             throw new RuntimeException("<strategy> 标签中 <include> 与 <exclude> 只能配置一项!");
         }
+        if (StringUtils.isNotBlank(config.getNotLikeTable()) && StringUtils.isNotBlank(config.getLikeTable())) {
+            throw new RuntimeException("<strategy> 标签中 <likeTable> 与 <notLikeTable> 只能配置一项!");
+        }
         //所有的表信息
         List<TableInfo> tableList = new ArrayList<>();
 
@@ -420,7 +424,22 @@ public class ConfigBuilder {
         Set<String> notExistTables = new HashSet<>();
         try {
             String tablesSql = dbQuery.tablesSql();
-            if (DbType.POSTGRE_SQL == dbQuery.dbType()) {
+            if(DbType.MYSQL == dbQuery.dbType()){
+                StringBuilder sql = new StringBuilder(tablesSql).append(" where 1 = 1 ");
+                if (StringUtils.isNotBlank(config.getLikeTable())) {
+                    sql.append(" and ").append(dbQuery.tableName()).append(" like '").append(config.getLikeTable()).append("'");
+                } else if (StringUtils.isNotBlank(config.getNotLikeTable())) {
+                    sql.append(" and ").append(dbQuery.tableName()).append(" not like '").append(config.getLikeTable()).append("'");
+                }
+                if (isInclude) {
+                    sql.append(" and ").append(dbQuery.tableName()).append(" in (")
+                        .append(Arrays.stream(config.getInclude()).map(tb -> "'" + tb + "'").collect(Collectors.joining(","))).append(")");
+                } else if (isExclude) {
+                    sql.append(" and ").append(dbQuery.tableName()).append(" not in (")
+                        .append(Arrays.stream(config.getInclude()).map(tb -> "'" + tb + "'").collect(Collectors.joining(","))).append(")");
+                }
+                tablesSql = sql.toString();
+            } else if (DbType.POSTGRE_SQL == dbQuery.dbType()) {
                 String schema = dataSourceConfig.getSchemaName();
                 if (schema == null) {
                     //pg 默认 schema=public
@@ -428,8 +447,7 @@ public class ConfigBuilder {
                     dataSourceConfig.setSchemaName(schema);
                 }
                 tablesSql = String.format(tablesSql, schema);
-            }
-            if (DbType.KINGBASE_ES == dbQuery.dbType()) {
+            }else if (DbType.KINGBASE_ES == dbQuery.dbType()) {
                 String schema = dataSourceConfig.getSchemaName();
                 if (schema == null) {
                     //kingbase 默认 schema=PUBLIC
@@ -437,8 +455,7 @@ public class ConfigBuilder {
                     dataSourceConfig.setSchemaName(schema);
                 }
                 tablesSql = String.format(tablesSql, schema);
-            }
-            if (DbType.DB2 == dbQuery.dbType()) {
+            } else if (DbType.DB2 == dbQuery.dbType()) {
                 String schema = dataSourceConfig.getSchemaName();
                 if (schema == null) {
                     //db2 默认 schema=current schema