From 06fdc17eae17fff9081562d015fc77e6d9da5c75 Mon Sep 17 00:00:00 2001 From: limyshkalapala Date: Mon, 3 Apr 2023 23:37:19 -0300 Subject: [PATCH 1/6] Decompose conditional refactoring in TableField.java. --- .../generator/config/po/TableField.java | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/po/TableField.java b/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/po/TableField.java index 1a0b75149..4063f9e73 100644 --- a/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/po/TableField.java +++ b/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/po/TableField.java @@ -141,6 +141,22 @@ public TableField(@NotNull ConfigBuilder configBuilder, @NotNull String name) { this.globalConfig = configBuilder.getGlobalConfig(); } + + private boolean shouldConvertBooleanProperty(String propertyName) { + return entity.isBooleanColumnRemoveIsPrefix() + && "boolean".equalsIgnoreCase(this.getPropertyType()) + && propertyName.startsWith("is"); + } + + private void convertBooleanProperty() { + this.convert = true; + } + + private void updatePropertyName(String propertyName) { + this.propertyName = StringUtils.removePrefixAfterPrefixToLower(propertyName, 2); + } + + /** * 设置属性名称 * @@ -151,12 +167,12 @@ public TableField(@NotNull ConfigBuilder configBuilder, @NotNull String name) { */ public TableField setPropertyName(@NotNull String propertyName, @NotNull IColumnType columnType) { this.columnType = columnType; - if (entity.isBooleanColumnRemoveIsPrefix() - && "boolean".equalsIgnoreCase(this.getPropertyType()) && propertyName.startsWith("is")) { - this.convert = true; - this.propertyName = StringUtils.removePrefixAfterPrefixToLower(propertyName, 2); + if (shouldConvertBooleanProperty(propertyName)) { + convertBooleanProperty(); + updatePropertyName(propertyName); return this; } + // 下划线转驼峰策略 if (NamingStrategy.underline_to_camel.equals(this.entity.getColumnNaming())) { this.convert = !propertyName.equalsIgnoreCase(NamingStrategy.underlineToCamel(this.columnName)); From 76f60547501331ab0635bab1a6856907199cb456 Mon Sep 17 00:00:00 2001 From: limyshkalapala Date: Mon, 3 Apr 2023 23:47:45 -0300 Subject: [PATCH 2/6] Extract method refactoring in TemplateConfig.java --- .../generator/config/TemplateConfig.java | 52 ++++++++++--------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/TemplateConfig.java b/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/TemplateConfig.java index e2ae14991..006b9d915 100644 --- a/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/TemplateConfig.java +++ b/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/TemplateConfig.java @@ -121,35 +121,39 @@ public String getEntity(boolean kotlin) { public TemplateConfig disable(@NotNull TemplateType... templateTypes) { if (templateTypes != null && templateTypes.length > 0) { for (TemplateType templateType : templateTypes) { - switch (templateType) { - case ENTITY: - this.entity = null; - this.entityKt = null; - //暂时没其他多的需求,使用一个单独的boolean变量进行支持一下. - this.disableEntity = true; - break; - case CONTROLLER: - this.controller = null; - break; - case MAPPER: - this.mapper = null; - break; - case XML: - this.xml = null; - break; - case SERVICE: - this.service = null; - break; - case SERVICE_IMPL: - this.serviceImpl = null; - break; - default: - } + disableTemplateType(templateType); } } return this; } + private void disableTemplateType(TemplateType templateType) { + switch (templateType) { + case ENTITY: + this.entity = null; + this.entityKt = null; + //暂时没其他多的需求,使用一个单独的boolean变量进行支持一下. + this.disableEntity = true; + break; + case CONTROLLER: + this.controller = null; + break; + case MAPPER: + this.mapper = null; + break; + case XML: + this.xml = null; + break; + case SERVICE: + this.service = null; + break; + case SERVICE_IMPL: + this.serviceImpl = null; + break; + default: + } + } + /** * 禁用全部模板 * From fd2c73e62176b3cf9b4c8e0c6b03c2ebf2c65db2 Mon Sep 17 00:00:00 2001 From: limyshkalapala Date: Tue, 4 Apr 2023 00:18:06 -0300 Subject: [PATCH 3/6] Introduce explaining variable refactoring in TableField.java --- .../baomidou/mybatisplus/generator/config/po/TableField.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/po/TableField.java b/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/po/TableField.java index 4063f9e73..974a78d9d 100644 --- a/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/po/TableField.java +++ b/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/po/TableField.java @@ -173,8 +173,9 @@ public TableField setPropertyName(@NotNull String propertyName, @NotNull IColumn return this; } + Boolean eligibleForCamelCase = NamingStrategy.underline_to_camel.equals(this.entity.getColumnNaming()); // 下划线转驼峰策略 - if (NamingStrategy.underline_to_camel.equals(this.entity.getColumnNaming())) { + if (eligibleForCamelCase) { this.convert = !propertyName.equalsIgnoreCase(NamingStrategy.underlineToCamel(this.columnName)); } // 原样输出策略 From f3c7ba2dad47646be7c2920233e8ea39705ab3ec Mon Sep 17 00:00:00 2001 From: limyshkalapala Date: Tue, 4 Apr 2023 00:38:13 -0300 Subject: [PATCH 4/6] Replace conditional with polymorphism in TableField.java --- .../config/po/ColumnNameStrategy.java | 5 +++++ .../generator/config/po/NoChangeStrategy.java | 8 +++++++ .../generator/config/po/TableField.java | 22 +++++++++++-------- .../po/UnderlineToCamelCaseStrategy.java | 10 +++++++++ 4 files changed, 36 insertions(+), 9 deletions(-) create mode 100644 mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/po/ColumnNameStrategy.java create mode 100644 mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/po/NoChangeStrategy.java create mode 100644 mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/po/UnderlineToCamelCaseStrategy.java diff --git a/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/po/ColumnNameStrategy.java b/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/po/ColumnNameStrategy.java new file mode 100644 index 000000000..24de70f22 --- /dev/null +++ b/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/po/ColumnNameStrategy.java @@ -0,0 +1,5 @@ +package com.baomidou.mybatisplus.generator.config.po; + +public interface ColumnNameStrategy { + String convert(String columnName); +} diff --git a/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/po/NoChangeStrategy.java b/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/po/NoChangeStrategy.java new file mode 100644 index 000000000..0fc15a6d1 --- /dev/null +++ b/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/po/NoChangeStrategy.java @@ -0,0 +1,8 @@ +package com.baomidou.mybatisplus.generator.config.po; + +public class NoChangeStrategy implements ColumnNameStrategy { + @Override + public String convert(String columnName) { + return columnName; + } +} diff --git a/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/po/TableField.java b/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/po/TableField.java index 974a78d9d..2ab6803de 100644 --- a/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/po/TableField.java +++ b/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/po/TableField.java @@ -173,15 +173,9 @@ public TableField setPropertyName(@NotNull String propertyName, @NotNull IColumn return this; } - Boolean eligibleForCamelCase = NamingStrategy.underline_to_camel.equals(this.entity.getColumnNaming()); - // 下划线转驼峰策略 - if (eligibleForCamelCase) { - this.convert = !propertyName.equalsIgnoreCase(NamingStrategy.underlineToCamel(this.columnName)); - } - // 原样输出策略 - if (NamingStrategy.no_change.equals(this.entity.getColumnNaming())) { - this.convert = !propertyName.equalsIgnoreCase(this.columnName); - } + ColumnNameStrategy columnNameStrategy = getColumnNameStrategy(); + + this.convert = !propertyName.equalsIgnoreCase(columnNameStrategy.convert(this.columnName)); if (entity.isTableFieldAnnotationEnable()) { this.convert = true; } @@ -189,6 +183,16 @@ public TableField setPropertyName(@NotNull String propertyName, @NotNull IColumn return this; } + private ColumnNameStrategy getColumnNameStrategy() { + switch (this.entity.getColumnNaming()) { + case underline_to_camel: + return new UnderlineToCamelCaseStrategy(); + case no_change: + default: + return new NoChangeStrategy(); + } + } + public String getPropertyType() { if (null != columnType) { return columnType.getType(); diff --git a/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/po/UnderlineToCamelCaseStrategy.java b/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/po/UnderlineToCamelCaseStrategy.java new file mode 100644 index 000000000..f6aa26730 --- /dev/null +++ b/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/po/UnderlineToCamelCaseStrategy.java @@ -0,0 +1,10 @@ +package com.baomidou.mybatisplus.generator.config.po; + +import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; + +public class UnderlineToCamelCaseStrategy implements ColumnNameStrategy { + @Override + public String convert(String columnName) { + return NamingStrategy.underlineToCamel(columnName); + } +} From beb20cd9a248ce82a7f87b527275378f3842e707 Mon Sep 17 00:00:00 2001 From: limyshkalapala Date: Wed, 5 Apr 2023 13:46:52 -0300 Subject: [PATCH 5/6] Extract class Refactoring method in DatabaseMetaDataWrapper.java --- .../generator/jdbc/CommentFormatter.java | 28 +++++++++++++++++++ .../jdbc/DatabaseMetaDataWrapper.java | 13 +++++---- 2 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/jdbc/CommentFormatter.java diff --git a/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/jdbc/CommentFormatter.java b/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/jdbc/CommentFormatter.java new file mode 100644 index 000000000..6afb6563f --- /dev/null +++ b/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/jdbc/CommentFormatter.java @@ -0,0 +1,28 @@ +package com.baomidou.mybatisplus.generator.jdbc; + +public class CommentFormatter { + public String formatComment(String comment) { + // Remove leading and trailing whitespaces + comment = comment.trim(); + + // Split the comment into lines + String[] lines = comment.split("\\r?\\n"); + + // Remove leading and trailing whitespaces from each line + for (int i = 0; i < lines.length; i++) { + lines[i] = lines[i].trim(); + } + + // Reconstruct the formatted comment + StringBuilder formattedComment = new StringBuilder(); + formattedComment.append("/**\n"); + for (String line : lines) { + formattedComment.append(" * "); + formattedComment.append(line); + formattedComment.append("\n"); + } + formattedComment.append(" */\n"); + + return formattedComment.toString(); + } +} diff --git a/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/jdbc/DatabaseMetaDataWrapper.java b/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/jdbc/DatabaseMetaDataWrapper.java index 609235260..ffb139cb4 100644 --- a/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/jdbc/DatabaseMetaDataWrapper.java +++ b/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/jdbc/DatabaseMetaDataWrapper.java @@ -108,7 +108,8 @@ public Map getColumnsInfo(String catalog, String schema, String column.jdbcType = JdbcType.forCode(resultSet.getInt("DATA_TYPE")); column.length = resultSet.getInt("COLUMN_SIZE"); column.scale = resultSet.getInt("DECIMAL_DIGITS"); - column.remarks = formatComment(resultSet.getString("REMARKS")); + CommentFormatter formatter = new CommentFormatter(); + column.remarks = formatter.formatComment(resultSet.getString("REMARKS")); column.defaultValue = resultSet.getString("COLUMN_DEF"); column.nullable = resultSet.getInt("NULLABLE") == DatabaseMetaData.columnNullable; try { @@ -125,9 +126,7 @@ public Map getColumnsInfo(String catalog, String schema, String } } - public String formatComment(String comment) { - return StringUtils.isBlank(comment) ? StringPool.EMPTY : comment.replaceAll("\r\n", "\t"); - } + public Table getTableInfo(String tableName) { return getTableInfo(this.catalog, this.schema, tableName); @@ -144,7 +143,8 @@ public List getTables(String catalog, String schemaPattern, String tableN while (resultSet.next()) { table = new Table(); table.name = resultSet.getString("TABLE_NAME"); - table.remarks = formatComment(resultSet.getString("REMARKS")); + CommentFormatter formatter = new CommentFormatter(); + table.remarks = formatter.formatComment(resultSet.getString("REMARKS")); table.tableType = resultSet.getString("TABLE_TYPE"); tables.add(table); } @@ -160,7 +160,8 @@ public Table getTableInfo(String catalog, String schema, String tableName) { try (ResultSet resultSet = databaseMetaData.getTables(catalog, schema, tableName, new String[]{"TABLE", "VIEW"})) { table.name = tableName; while (resultSet.next()) { - table.remarks = formatComment(resultSet.getString("REMARKS")); + CommentFormatter formatter = new CommentFormatter(); + table.remarks = formatter.formatComment(resultSet.getString("REMARKS")); table.tableType = resultSet.getString("TABLE_TYPE"); } } catch (SQLException e) { From 08af9131b411a438957d0ae562d3b95b31cc3e2e Mon Sep 17 00:00:00 2001 From: limyshkalapala Date: Wed, 5 Apr 2023 14:24:39 -0300 Subject: [PATCH 6/6] Move method Refactoring method in DatabaseMetaDataWrapper.java --- .../config/rules/NamingStrategy.java | 2 +- .../config/rules/NamingStrategyUtils.java | 36 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/rules/NamingStrategyUtils.java diff --git a/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/rules/NamingStrategy.java b/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/rules/NamingStrategy.java index d6a381c95..f4c8f763c 100644 --- a/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/rules/NamingStrategy.java +++ b/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/rules/NamingStrategy.java @@ -124,7 +124,7 @@ public static String removeSuffix(String name, Set suffix) { * @return 转换后的字符串 */ public static String removeSuffixAndCamel(String name, Set suffix) { - return underlineToCamel(removeSuffix(name, suffix)); + return NamingStrategyUtils.underlineToCamel(removeSuffix(name, suffix)); } /** diff --git a/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/rules/NamingStrategyUtils.java b/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/rules/NamingStrategyUtils.java new file mode 100644 index 000000000..976bba9a4 --- /dev/null +++ b/mybatis-plus-generator/src/main/java/com/baomidou/mybatisplus/generator/config/rules/NamingStrategyUtils.java @@ -0,0 +1,36 @@ +package com.baomidou.mybatisplus.generator.config.rules; + +import com.baomidou.mybatisplus.core.toolkit.StringPool; +import com.baomidou.mybatisplus.core.toolkit.StringUtils; +import com.baomidou.mybatisplus.generator.config.ConstVal; + +import java.util.Arrays; + +public class NamingStrategyUtils { + public static String underlineToCamel(String name) { + if (StringUtils.isBlank(name)) { + return StringPool.EMPTY; + } + String tempName = name; + if (StringUtils.isCapitalMode(name) || StringUtils.isMixedMode(name)) { + tempName = name.toLowerCase(); + } + StringBuilder result = new StringBuilder(); + String[] camels = tempName.split(ConstVal.UNDERLINE); + Arrays.stream(camels).filter(camel -> !StringUtils.isBlank(camel)).forEach(camel -> { + if (result.length() == 0) { + result.append(StringUtils.firstToLowerCase(camel)); + } else { + result.append(capitalFirst(camel)); + } + }); + return result.toString(); + } + + private static String capitalFirst(String name) { + if (StringUtils.isNotBlank(name)) { + return name.substring(0, 1).toUpperCase() + name.substring(1); + } + return StringPool.EMPTY; + } +}