فهرست منبع

调整表名匹配.

聂秋秋 5 سال پیش
والد
کامیت
43e80fc74a

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

@@ -199,12 +199,12 @@ public class DataSourceConfig {
      * @return Connection
      */
     public Connection getConn() {
-        Connection conn = null;
+        Connection conn;
         try {
             Class.forName(driverName);
             conn = DriverManager.getConnection(url, username, password);
         } catch (ClassNotFoundException | SQLException e) {
-            e.printStackTrace();
+            throw new RuntimeException(e);
         }
         return conn;
     }

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

@@ -17,6 +17,7 @@ package com.baomidou.mybatisplus.generator.config;
 
 import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
 import com.baomidou.mybatisplus.core.toolkit.StringUtils;
+import com.baomidou.mybatisplus.generator.config.po.LikeTable;
 import com.baomidou.mybatisplus.generator.config.po.TableFill;
 import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
 import lombok.AccessLevel;
@@ -96,13 +97,13 @@ public class StrategyConfig {
     private String superControllerClass;
     /**
      * 需要包含的表名(与exclude二选一配置)
-     * @since 3.2.x 正则匹配不再支持,请使用{@link #setLikeTable(String)}
+     * @since 3.2.1 正则匹配不再支持,请使用{@link #setLikeTable(LikeTable)}}
      */
     @Setter(AccessLevel.NONE)
     private String[] include = null;
     /**
      * 需要排除的表名
-     * @since 3.2.x 正则匹配不再支持,请使用{@link #setNotLikeTable(String)}
+     * @since 3.2.1 正则匹配不再支持,请使用{@link #setNotLikeTable(LikeTable)}}
      */
     @Setter(AccessLevel.NONE)
     private String[] exclude = null;
@@ -165,15 +166,15 @@ public class StrategyConfig {
     /**
      * 包含表名
      *
-     * @since 3.2.x
+     * @since 3.2.1
      */
-    private String likeTable;
+    private LikeTable likeTable;
     /**
      * 不包含表名
      *
-     * @since 3.2.x
+     * @since 3.2.1
      */
-    private String notLikeTable;
+    private LikeTable notLikeTable;
 
     /**
      * 大写命名、字段符合大写字母数字下划线命名

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

@@ -410,7 +410,7 @@ public class ConfigBuilder {
         if (isInclude && isExclude) {
             throw new RuntimeException("<strategy> 标签中 <include> 与 <exclude> 只能配置一项!");
         }
-        if (StringUtils.isNotBlank(config.getNotLikeTable()) && StringUtils.isNotBlank(config.getLikeTable())) {
+        if (config.getNotLikeTable() != null && config.getLikeTable() != null) {
             throw new RuntimeException("<strategy> 标签中 <likeTable> 与 <notLikeTable> 只能配置一项!");
         }
         //所有的表信息
@@ -460,10 +460,10 @@ public class ConfigBuilder {
                 tablesSql = String.format(tablesSql, schema);
             }
             StringBuilder sql = new StringBuilder(tablesSql);
-            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 (config.getLikeTable() != null) {
+                sql.append(" AND ").append(dbQuery.tableName()).append(" LIKE '").append(config.getLikeTable().getValue()).append("'");
+            } else if (config.getNotLikeTable() != null) {
+                sql.append(" AND ").append(dbQuery.tableName()).append(" NOT LIKE '").append(config.getNotLikeTable().getValue()).append("'");
             }
             if (isInclude) {
                 sql.append(" AND ").append(dbQuery.tableName()).append(" IN (")

+ 52 - 0
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/po/LikeTable.java

@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2011-2020, baomidou (jobob@qq.com).
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * <p>
+ * https://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.baomidou.mybatisplus.generator.config.po;
+
+import com.baomidou.mybatisplus.core.enums.SqlLike;
+import com.baomidou.mybatisplus.core.toolkit.sql.SqlUtils;
+
+/**
+ * 表名拼接
+ *
+ * @author nieqiuqiu
+ * @date 2019-11-26
+ * @since 3.2.1
+ */
+public class LikeTable {
+
+    private String value;
+
+    private SqlLike like = SqlLike.DEFAULT;
+
+    public LikeTable(String value) {
+        this.value = value;
+    }
+
+    public LikeTable(String value, SqlLike like) {
+        this.value = value;
+        this.like = like;
+    }
+
+    @Override
+    public String toString() {
+        return getValue();
+    }
+
+    public String getValue() {
+        return SqlUtils.concatLike(this.value, like);
+    }
+
+}

+ 20 - 0
mybatis-plus-generator/src/test/java/com/baomidou/mybatisplus/test/generator/LikeTableTest.java

@@ -0,0 +1,20 @@
+package com.baomidou.mybatisplus.test.generator;
+
+import com.baomidou.mybatisplus.core.enums.SqlLike;
+import com.baomidou.mybatisplus.generator.config.po.LikeTable;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+class LikeTableTest {
+
+    @Test
+    void test() {
+        Assertions.assertEquals("%test%", new LikeTable("test").toString());
+        Assertions.assertEquals("%test%", new LikeTable("test").getValue());
+        Assertions.assertEquals("%test", new LikeTable("test", SqlLike.LEFT).toString());
+        Assertions.assertEquals("%test", new LikeTable("test", SqlLike.LEFT).getValue());
+        Assertions.assertEquals("test%", new LikeTable("test", SqlLike.RIGHT).toString());
+        Assertions.assertEquals("test%", new LikeTable("test", SqlLike.RIGHT).getValue());
+    }
+
+}