Browse Source

优化ConfigBuilder

nieqiuqiu 5 năm trước cách đây
mục cha
commit
d0c8e2abe9

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

@@ -16,6 +16,7 @@
 package com.baomidou.mybatisplus.generator.config.builder;
 
 import com.baomidou.mybatisplus.annotation.DbType;
+import com.baomidou.mybatisplus.core.toolkit.ArrayUtils;
 import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.core.toolkit.Maps;
 import com.baomidou.mybatisplus.core.toolkit.StringPool;
@@ -38,6 +39,7 @@ import java.util.Arrays;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 import java.util.Set;
 import java.util.regex.Pattern;
 import java.util.stream.Collectors;
@@ -65,7 +67,9 @@ public class ConfigBuilder {
     /**
      * SQL语句类型
      */
+    @Deprecated
     private IDbQuery dbQuery;
+    @Deprecated
     private DbType dbType;
     private String superEntityClass;
     private String superMapperClass;
@@ -102,6 +106,7 @@ public class ConfigBuilder {
     /**
      * 是否支持注释
      */
+    @Deprecated
     private boolean commentSupported;
     /**
      * 过滤正则
@@ -120,17 +125,9 @@ public class ConfigBuilder {
     public ConfigBuilder(PackageConfig packageConfig, DataSourceConfig dataSourceConfig, StrategyConfig strategyConfig,
                          TemplateConfig template, GlobalConfig globalConfig) {
         // 全局配置
-        if (null == globalConfig) {
-            this.globalConfig = new GlobalConfig();
-        } else {
-            this.globalConfig = globalConfig;
-        }
+        this.globalConfig = Optional.ofNullable(globalConfig).orElseGet(GlobalConfig::new);
         // 模板配置
-        if (null == template) {
-            this.template = new TemplateConfig();
-        } else {
-            this.template = template;
-        }
+        this.template = Optional.ofNullable(template).orElseGet(TemplateConfig::new);
         // 包配置
         if (null == packageConfig) {
             handlerPackage(this.template, this.globalConfig.getOutputDir(), new PackageConfig());
@@ -140,11 +137,7 @@ public class ConfigBuilder {
         this.dataSourceConfig = dataSourceConfig;
         handlerDataSource(dataSourceConfig);
         // 策略配置
-        if (null == strategyConfig) {
-            this.strategyConfig = new StrategyConfig();
-        } else {
-            this.strategyConfig = strategyConfig;
-        }
+        this.strategyConfig = Optional.ofNullable(strategyConfig).orElseGet(StrategyConfig::new);
         //SQLITE 数据库不支持注释获取
         commentSupported = !dataSourceConfig.getDbType().equals(DbType.SQLITE);
 
@@ -224,7 +217,7 @@ public class ConfigBuilder {
      * @return 所以模板路径配置信息
      */
     public TemplateConfig getTemplate() {
-        return template == null ? new TemplateConfig() : template;
+        return this.template;
     }
 
     // ****************************** 曝露方法 END**********************************
@@ -298,25 +291,17 @@ public class ConfigBuilder {
      * @param config 策略配置
      */
     private void processTypes(StrategyConfig config) {
-        if (StringUtils.isBlank(config.getSuperServiceClass())) {
-            superServiceClass = ConstVal.SUPER_SERVICE_CLASS;
-        } else {
-            superServiceClass = config.getSuperServiceClass();
-        }
-        if (StringUtils.isBlank(config.getSuperServiceImplClass())) {
-            superServiceImplClass = ConstVal.SUPER_SERVICE_IMPL_CLASS;
-        } else {
-            superServiceImplClass = config.getSuperServiceImplClass();
-        }
-        if (StringUtils.isBlank(config.getSuperMapperClass())) {
-            superMapperClass = ConstVal.SUPER_MAPPER_CLASS;
-        } else {
-            superMapperClass = config.getSuperMapperClass();
-        }
+        this.superServiceClass = getValueOrDefault(config.getSuperServiceClass(), ConstVal.SUPER_SERVICE_CLASS);
+        this.superServiceImplClass = getValueOrDefault(config.getSuperServiceImplClass(), ConstVal.SUPER_SERVICE_IMPL_CLASS);
+        this.superMapperClass = getValueOrDefault(config.getSuperMapperClass(), ConstVal.SUPER_MAPPER_CLASS);
         superEntityClass = config.getSuperEntityClass();
         superControllerClass = config.getSuperControllerClass();
     }
 
+    private static String getValueOrDefault(String value, String defaultValue) {
+        return StringUtils.isBlank(value) ? defaultValue : value;
+    }
+
     /**
      * 处理表对应的类名称
      *
@@ -435,9 +420,11 @@ public class ConfigBuilder {
 
         //不存在的表名
         Set<String> notExistTables = new HashSet<>();
+        DbType dbType = this.dataSourceConfig.getDbType();
+        IDbQuery dbQuery = this.dataSourceConfig.getDbQuery();
         try {
-            String tablesSql = dbQuery.tablesSql();
-            if (DbType.POSTGRE_SQL == this.dbType) {
+            String tablesSql = dataSourceConfig.getDbQuery().tablesSql();
+            if (DbType.POSTGRE_SQL == dbType) {
                 String schema = dataSourceConfig.getSchemaName();
                 if (schema == null) {
                     //pg 默认 schema=public
@@ -445,7 +432,7 @@ public class ConfigBuilder {
                     dataSourceConfig.setSchemaName(schema);
                 }
                 tablesSql = String.format(tablesSql, schema);
-            } else if (DbType.KINGBASE_ES == this.dbType) {
+            } else if (DbType.KINGBASE_ES == dbType) {
                 String schema = dataSourceConfig.getSchemaName();
                 if (schema == null) {
                     //kingbase 默认 schema=PUBLIC
@@ -453,7 +440,7 @@ public class ConfigBuilder {
                     dataSourceConfig.setSchemaName(schema);
                 }
                 tablesSql = String.format(tablesSql, schema);
-            } else if (DbType.DB2 == this.dbType) {
+            } else if (DbType.DB2 == dbType) {
                 String schema = dataSourceConfig.getSchemaName();
                 if (schema == null) {
                     //db2 默认 schema=current schema
@@ -463,7 +450,7 @@ public class ConfigBuilder {
                 tablesSql = String.format(tablesSql, schema);
             }
             //oracle数据库表太多,出现最大游标错误
-            else if (DbType.ORACLE == this.dbType) {
+            else if (DbType.ORACLE == dbType) {
                 String schema = dataSourceConfig.getSchemaName();
                 //oracle 默认 schema=username
                 if (schema == null) {
@@ -495,9 +482,9 @@ public class ConfigBuilder {
                     if (StringUtils.isNotBlank(tableName)) {
                         tableInfo = new TableInfo();
                         tableInfo.setName(tableName);
-
-                        if (commentSupported) {
-                            String tableComment = results.getString(dbQuery.tableComment());
+                        String commentColumn = dbQuery.tableComment();
+                        if (StringUtils.isNotBlank(commentColumn)) {
+                            String tableComment = results.getString(commentColumn);
                             if (config.isSkipView() && "VIEW".equals(tableComment)) {
                                 // 跳过视图
                                 continue;
@@ -584,7 +571,8 @@ public class ConfigBuilder {
         boolean haveId = false;
         List<TableField> fieldList = new ArrayList<>();
         List<TableField> commonFieldList = new ArrayList<>();
-        DbType dbType = this.dbType;
+        DbType dbType = this.dataSourceConfig.getDbType();
+        IDbQuery dbQuery = dataSourceConfig.getDbQuery();
         String tableName = tableInfo.getName();
         try {
             String tableFieldsSql = dbQuery.tableFieldsSql();
@@ -672,8 +660,9 @@ public class ConfigBuilder {
                         field.setPropertyName(strategyConfig, processName(field.getName(), config.getColumnNaming()));
                     }
                     field.setColumnType(dataSourceConfig.getTypeConvert().processTypeConvert(globalConfig, field));
-                    if (commentSupported) {
-                        field.setComment(formatComment(results.getString(dbQuery.fieldComment())));
+                    String fieldCommentColumn = dbQuery.fieldComment();
+                    if (StringUtils.isNotBlank(fieldCommentColumn)) {
+                        field.setComment(formatComment(results.getString(fieldCommentColumn)));
                     }
                     // 填充逻辑判断
                     List<TableFill> tableFillList = getStrategyConfig().getTableFillList();
@@ -726,10 +715,7 @@ public class ConfigBuilder {
      * @return 连接后的包名
      */
     private String joinPackage(String parent, String subPackage) {
-        if (StringUtils.isBlank(parent)) {
-            return subPackage;
-        }
-        return parent + StringPool.DOT + subPackage;
+        return StringUtils.isBlank(parent) ? subPackage : parent + StringPool.DOT + subPackage;
     }
 
 
@@ -752,12 +738,8 @@ public class ConfigBuilder {
      * @return 根据策略返回处理后的名称
      */
     private String processName(String name, NamingStrategy strategy, String[] prefix) {
-        boolean removePrefix = false;
-        if (prefix != null && prefix.length != 0) {
-            removePrefix = true;
-        }
         String propertyName;
-        if (removePrefix) {
+        if (ArrayUtils.isNotEmpty(prefix)) {
             if (strategy == NamingStrategy.underline_to_camel) {
                 // 删除前缀、下划线转驼峰
                 propertyName = NamingStrategy.removePrefixAndCamel(name, prefix);