Parcourir la source

生成器代码优化.

nieqiurong il y a 4 ans
Parent
commit
17ad9e458b

+ 0 - 1
build.gradle

@@ -185,7 +185,6 @@ subprojects {
     test {
         dependsOn("cleanTest", "generatePomFileForMavenJavaPublication")
         useJUnitPlatform()
-        exclude("**/generator/**")
         exclude("**/mysql/**")
         exclude("**/phoenix/**")
     }

+ 10 - 0
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/PackageConfig.java

@@ -80,4 +80,14 @@ public class PackageConfig {
         }
         return parent;
     }
+
+
+    /**
+     * 连接父子包名
+     *
+     * @return 连接后的包名
+     */
+    public String joinPackage(String subPackage) {
+        return StringUtils.isBlank(parent) ? subPackage : (parent + StringPool.DOT + subPackage);
+    }
 }

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

@@ -26,6 +26,11 @@ import com.baomidou.mybatisplus.generator.config.po.TableInfo;
 import com.baomidou.mybatisplus.generator.config.querys.DecoratorDbQuery;
 import com.baomidou.mybatisplus.generator.config.querys.H2Query;
 import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
+import lombok.AccessLevel;
+import lombok.Data;
+import lombok.Getter;
+import lombok.Setter;
+import lombok.experimental.Accessors;
 
 import java.io.File;
 import java.sql.Connection;
@@ -42,19 +47,26 @@ import java.util.stream.Collectors;
  * @author YangHu, tangguo, hubin, Juzi
  * @since 2016-08-30
  */
+@Data
+@Accessors(chain = true)
 public class ConfigBuilder {
 
     /**
      * 模板路径配置信息
      */
+    @Setter(value = AccessLevel.NONE)
     private final TemplateConfig template;
     /**
      * 数据库配置
      */
+    @Getter(value = AccessLevel.NONE)
+    @Setter(value = AccessLevel.NONE)
     private final DataSourceConfig dataSourceConfig;
     /**
      * SQL连接
      */
+    @Getter(value = AccessLevel.NONE)
+    @Setter(value = AccessLevel.NONE)
     private final Connection connection;
     /**
      * 数据库表信息
@@ -63,10 +75,12 @@ public class ConfigBuilder {
     /**
      * 包配置详情
      */
+    @Setter(value = AccessLevel.NONE)
     private Map<String, String> packageInfo;
     /**
      * 路径配置信息
      */
+    @Setter(value = AccessLevel.NONE)
     private Map<String, String> pathInfo;
     /**
      * 策略配置
@@ -112,52 +126,6 @@ public class ConfigBuilder {
         this.tableInfoList = getTablesInfo(this.strategyConfig);
     }
 
-    // ************************ 曝露方法 BEGIN*****************************
-
-    /**
-     * 所有包配置信息
-     *
-     * @return 包配置
-     */
-    public Map<String, String> getPackageInfo() {
-        return packageInfo;
-    }
-
-
-    /**
-     * 所有路径配置
-     *
-     * @return 路径配置
-     */
-    public Map<String, String> getPathInfo() {
-        return pathInfo;
-    }
-
-    /**
-     * 表信息
-     *
-     * @return 所有表信息
-     */
-    public List<TableInfo> getTableInfoList() {
-        return tableInfoList;
-    }
-
-    public ConfigBuilder setTableInfoList(List<TableInfo> tableInfoList) {
-        this.tableInfoList = tableInfoList;
-        return this;
-    }
-
-    /**
-     * 模板路径配置信息
-     *
-     * @return 所以模板路径配置信息
-     */
-    public TemplateConfig getTemplate() {
-        return this.template;
-    }
-
-    // ****************************** 曝露方法 END**********************************
-
     /**
      * 处理包配置
      *
@@ -169,12 +137,12 @@ public class ConfigBuilder {
         // 包信息
         packageInfo = CollectionUtils.newHashMapWithExpectedSize(7);
         packageInfo.put(ConstVal.MODULE_NAME, config.getModuleName());
-        packageInfo.put(ConstVal.ENTITY, joinPackage(config.getParent(), config.getEntity()));
-        packageInfo.put(ConstVal.MAPPER, joinPackage(config.getParent(), config.getMapper()));
-        packageInfo.put(ConstVal.XML, joinPackage(config.getParent(), config.getXml()));
-        packageInfo.put(ConstVal.SERVICE, joinPackage(config.getParent(), config.getService()));
-        packageInfo.put(ConstVal.SERVICE_IMPL, joinPackage(config.getParent(), config.getServiceImpl()));
-        packageInfo.put(ConstVal.CONTROLLER, joinPackage(config.getParent(), config.getController()));
+        packageInfo.put(ConstVal.ENTITY, config.joinPackage(config.getEntity()));
+        packageInfo.put(ConstVal.MAPPER, config.joinPackage(config.getMapper()));
+        packageInfo.put(ConstVal.XML, config.joinPackage(config.getXml()));
+        packageInfo.put(ConstVal.SERVICE, config.joinPackage(config.getService()));
+        packageInfo.put(ConstVal.SERVICE_IMPL, config.joinPackage(config.getServiceImpl()));
+        packageInfo.put(ConstVal.CONTROLLER, config.joinPackage(config.getController()));
 
         // 自定义路径
         Map<String, String> configPathInfo = config.getPathInfo();
@@ -330,16 +298,12 @@ public class ConfigBuilder {
                     }
                     tableInfo = new TableInfo();
                     tableInfo.setName(tableName);
-                    String commentColumn = dbQuery.tableComment();
-                    if (StringUtils.isNotBlank(commentColumn)) {
-                        String tableComment = results.getString(commentColumn);
-                        if (config.isSkipView() && "VIEW".equals(tableComment)) {
-                            // 跳过视图
-                            continue;
-                        }
-                        tableInfo.setComment(formatComment(tableComment));
+                    String tableComment = dbQuery.getTableComment(results);
+                    if (config.isSkipView() && "VIEW".equals(tableComment)) {
+                        // 跳过视图
+                        continue;
                     }
-
+                    tableInfo.setComment(tableComment);
                     if (isInclude) {
                         for (String includeTable : config.getInclude()) {
                             // 忽略大小写等于 或 正则 true
@@ -413,7 +377,6 @@ public class ConfigBuilder {
      * @return ignore
      */
     private TableInfo convertTableFields(TableInfo tableInfo, StrategyConfig config) {
-        boolean haveId = false;
         List<TableField> fieldList = new ArrayList<>();
         List<TableField> commonFieldList = new ArrayList<>();
         DbType dbType = this.dataSourceConfig.getDbType();
@@ -452,8 +415,7 @@ public class ConfigBuilder {
                     }
 
                     // 处理ID
-                    if (isId && !haveId) {
-                        haveId = true;
+                    if (isId) {
                         field.setKeyFlag(true);
                         tableInfo.setHavePrimaryKey(true);
                         field.setKeyIdentityFlag(dbQuery.isKeyIdentity(results));
@@ -484,13 +446,10 @@ public class ConfigBuilder {
                     if (null != nameConvert) {
                         field.setPropertyName(nameConvert.propertyNameConvert(field));
                     } else {
-                        field.setPropertyName(strategyConfig, processName(field.getName(), config.getColumnNaming()));
+                        field.setPropertyName(strategyConfig, processName(field.getName(), config.getColumnNaming(), strategyConfig.getFieldPrefix()));
                     }
                     field.setColumnType(dataSourceConfig.getTypeConvert().processTypeConvert(globalConfig, field));
-                    String fieldCommentColumn = dbQuery.fieldComment();
-                    if (StringUtils.isNotBlank(fieldCommentColumn)) {
-                        field.setComment(formatComment(results.getString(fieldCommentColumn)));
-                    }
+                    field.setComment(dbQuery.getFiledComment(results));
                     // 填充逻辑判断
                     getStrategyConfig().getTableFillList()
                         .stream()
@@ -532,29 +491,6 @@ public class ConfigBuilder {
         return parentDir + packageName;
     }
 
-
-    /**
-     * 连接父子包名
-     *
-     * @param parent     父包名
-     * @param subPackage 子包名
-     * @return 连接后的包名
-     */
-    private String joinPackage(String parent, String subPackage) {
-        return StringUtils.isBlank(parent) ? subPackage : (parent + StringPool.DOT + subPackage);
-    }
-
-
-    /**
-     * 处理字段名称
-     *
-     * @return 根据策略返回处理后的名称
-     */
-    private String processName(String name, NamingStrategy strategy) {
-        return processName(name, strategy, strategyConfig.getFieldPrefix());
-    }
-
-
     /**
      * 处理表/字段名称
      *
@@ -583,48 +519,17 @@ public class ConfigBuilder {
         return propertyName;
     }
 
-
-    public StrategyConfig getStrategyConfig() {
-        return strategyConfig;
-    }
-
-
-    public ConfigBuilder setStrategyConfig(StrategyConfig strategyConfig) {
-        this.strategyConfig = strategyConfig;
-        return this;
-    }
-
-
-    public GlobalConfig getGlobalConfig() {
-        return globalConfig;
-    }
-
-
-    public ConfigBuilder setGlobalConfig(GlobalConfig globalConfig) {
-        this.globalConfig = globalConfig;
-        return this;
-    }
-
-
-    public InjectionConfig getInjectionConfig() {
-        return injectionConfig;
-    }
-
-
-    public ConfigBuilder setInjectionConfig(InjectionConfig injectionConfig) {
-        this.injectionConfig = injectionConfig;
-        return this;
-    }
-
     /**
      * 格式化数据库注释内容
      *
      * @param comment 注释
      * @return 注释
+     * @see DecoratorDbQuery#formatComment(java.lang.String)
      * @since 3.4.0
+     * @deprecated 3.4.1
      */
     public String formatComment(String comment) {
-        return StringUtils.isBlank(comment) ? StringPool.EMPTY : comment.replaceAll("\r\n", "\t");
+        return dbQuery.formatComment(comment);
     }
 
     public void close() {

+ 19 - 1
mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/querys/DecoratorDbQuery.java

@@ -16,6 +16,8 @@
 package com.baomidou.mybatisplus.generator.config.querys;
 
 import com.baomidou.mybatisplus.annotation.DbType;
+import com.baomidou.mybatisplus.core.toolkit.StringPool;
+import com.baomidou.mybatisplus.core.toolkit.StringUtils;
 import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
 import com.baomidou.mybatisplus.generator.config.IDbQuery;
 
@@ -83,7 +85,7 @@ public class DecoratorDbQuery extends AbstractDbQuery {
     }
 
     /**
-     * 扩展{@link #tableFieldsSql()}接口
+     * 扩展{@link #tableFieldsSql()}方法
      *
      * @param tableName 表名
      * @return 查询表字段语句
@@ -120,6 +122,10 @@ public class DecoratorDbQuery extends AbstractDbQuery {
         return dbQuery.tableComment();
     }
 
+    public String getTableComment(ResultSet resultSet) throws SQLException {
+        return getResultStringValue(resultSet, this.tableComment());
+    }
+
     @Override
     public String fieldName() {
         return dbQuery.fieldName();
@@ -135,6 +141,18 @@ public class DecoratorDbQuery extends AbstractDbQuery {
         return dbQuery.fieldComment();
     }
 
+    public String getFiledComment(ResultSet resultSet) throws SQLException {
+        return getResultStringValue(resultSet, this.fieldComment());
+    }
+
+    private String getResultStringValue(ResultSet resultSet, String columnLabel) throws SQLException {
+        return StringUtils.isNotBlank(columnLabel) ? StringPool.EMPTY : formatComment(resultSet.getString(columnLabel));
+    }
+
+    public String formatComment(String comment) {
+        return StringUtils.isBlank(comment) ? StringPool.EMPTY : comment.replaceAll("\r\n", "\t");
+    }
+
     @Override
     public String fieldKey() {
         return dbQuery.fieldKey();

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

@@ -24,6 +24,7 @@ import com.baomidou.mybatisplus.generator.config.PackageConfig;
 import com.baomidou.mybatisplus.generator.config.StrategyConfig;
 import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
 import com.mysql.cj.jdbc.Driver;
+import org.junit.jupiter.api.Disabled;
 import org.junit.jupiter.api.Test;
 
 /**
@@ -32,6 +33,7 @@ import org.junit.jupiter.api.Test;
  * @author K神
  * @since 2017/12/29
  */
+@Disabled
 class CodeGeneratorTest {
 
     /**

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

@@ -22,6 +22,7 @@ import com.baomidou.mybatisplus.generator.InjectionConfig;
 import com.baomidou.mybatisplus.generator.config.*;
 import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
 import com.mysql.cj.jdbc.Driver;
+import org.junit.jupiter.api.Disabled;
 import org.junit.jupiter.api.Test;
 
 import java.util.HashMap;
@@ -33,6 +34,7 @@ import java.util.Map;
  * @author K神
  * @since 2017/12/29
  */
+@Disabled
 class CodeGeneratorWithTemplateTest {
 
     /**