From 0c1b59e6625df47df78d170a3732f68e9b98121f Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Fri, 29 Mar 2024 11:54:12 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=20=E2=80=9C=E6=80=BB?= =?UTF-8?q?=E6=98=AF=E7=94=9F=E6=88=90=20@Column=20=E6=B3=A8=E8=A7=A3?= =?UTF-8?q?=E2=80=9D=20=E9=80=89=E9=A1=B9=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../codegen/config/EntityConfig.java | 13 ++ .../mybatisflex/codegen/entity/Column.java | 132 ++++++++++-------- 2 files changed, 89 insertions(+), 56 deletions(-) diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/EntityConfig.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/EntityConfig.java index 93110d22..bcead66c 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/EntityConfig.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/EntityConfig.java @@ -116,6 +116,10 @@ public class EntityConfig implements Serializable { */ private boolean columnCommentEnable; + /** + * 是否总是生成 @Column 注解。 + */ + private boolean alwaysGenColumnAnnotation; public String getSourceDir() { return sourceDir; @@ -341,6 +345,15 @@ public class EntityConfig implements Serializable { this.columnCommentEnable = columnCommentEnable; } + public boolean isAlwaysGenColumnAnnotation() { + return alwaysGenColumnAnnotation; + } + + public EntityConfig setAlwaysGenColumnAnnotation(boolean alwaysGenColumnAnnotation) { + this.alwaysGenColumnAnnotation = alwaysGenColumnAnnotation; + return this; + } + public enum SwaggerVersion { FOX("FOX"), diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Column.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Column.java index 05a37502..7a67856c 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Column.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/entity/Column.java @@ -65,11 +65,6 @@ public class Column { */ private boolean isAutoIncrement; -// /** -// * 是否需要生成 @Column 注解。 -// */ -// private boolean needGenColumnAnnotation = false; - /** * 数据库的字段类型,比如 varchar/tinyint 等 */ @@ -210,6 +205,30 @@ public class Column { return StringUtil.firstCharToLowerCase(StringUtil.underlineToCamel(entityJavaFileName)); } + /** + * importClass为类的全限定名 + */ + private static void addImportClass(Set importClasses, String importClass) { + importClass = importClass.trim(); + + // java.util.List >>>>> java.util.List + if (importClass.contains("<") && importClass.endsWith(">")) { + importClass = importClass.substring(0, importClass.indexOf("<")); + } + + // 不包含“.”则认为是原始类型,不需要import + // lang 包不需要显式导入 + if (importClass.contains(".") && !importClass.startsWith("java.lang.")) { + importClasses.add(importClass); + } + } + + private void addComma(StringBuilder annotations, boolean needComma) { + if (needComma) { + annotations.append(", "); + } + } + public String buildAnnotations() { StringBuilder annotations = new StringBuilder(); @@ -241,8 +260,11 @@ public class Column { if (entityConfig != null && entityConfig.isColumnCommentEnable() && StringUtil.isNotBlank(comment)) { addComma(annotations, needComma); - String comment = this.comment.replace("\n", "").replace("\"", "\\\"").trim(); - annotations.append("comment = \"" + comment + "\""); + annotations.append("comment = \"") + .append(this.comment.replace("\n", "") + .replace("\"", "\\\"") + .trim()) + .append("\""); } if (annotations.length() == 4) { @@ -250,11 +272,18 @@ public class Column { } else { annotations.append(")"); } + + if (entityConfig != null && entityConfig.isAlwaysGenColumnAnnotation()) { + annotations.append("\n\t"); + } } - boolean needGenColumnAnnotation = !name.equalsIgnoreCase(StringUtil.camelToUnderline(property)) + boolean needGenColumnAnnotation = (entityConfig != null && entityConfig.isAlwaysGenColumnAnnotation()) + || !name.equalsIgnoreCase(StringUtil.camelToUnderline(property)) || (entityConfig != null && entityConfig.isColumnCommentEnable() && StringUtil.isNotBlank(this.comment) && annotations.length() == 0); + StringBuilder columnAnnotation = new StringBuilder("@Column("); + //@Column 注解 if (columnConfig.getOnInsertValue() != null || columnConfig.getOnUpdateValue() != null @@ -266,59 +295,73 @@ public class Column { || columnConfig.getTenantId() != null || needGenColumnAnnotation ) { - annotations.append("@Column("); boolean needComma = false; - if (!name.equalsIgnoreCase(StringUtil.camelToUnderline(property))) { - annotations.append("value = \"").append(name).append("\""); + if (entityConfig != null && entityConfig.isAlwaysGenColumnAnnotation() + || !name.equalsIgnoreCase(StringUtil.camelToUnderline(property))) { + columnAnnotation.append("value = \"").append(name).append("\""); needComma = true; } if (columnConfig.getOnInsertValue() != null) { - addComma(annotations, needComma); - annotations.append("onInsertValue = \"").append(columnConfig.getOnInsertValue()).append("\""); + addComma(columnAnnotation, needComma); + columnAnnotation.append("onInsertValue = \"").append(columnConfig.getOnInsertValue()).append("\""); needComma = true; } if (columnConfig.getOnUpdateValue() != null) { - addComma(annotations, needComma); - annotations.append("onUpdateValue = \"").append(columnConfig.getOnUpdateValue()).append("\""); + addComma(columnAnnotation, needComma); + columnAnnotation.append("onUpdateValue = \"").append(columnConfig.getOnUpdateValue()).append("\""); needComma = true; } if (columnConfig.getLarge() != null) { - addComma(annotations, needComma); - annotations.append("isLarge = ").append(columnConfig.getLarge()); + addComma(columnAnnotation, needComma); + columnAnnotation.append("isLarge = ").append(columnConfig.getLarge()); needComma = true; } if (columnConfig.getLogicDelete() != null) { - addComma(annotations, needComma); - annotations.append("isLogicDelete = ").append(columnConfig.getLogicDelete()); + addComma(columnAnnotation, needComma); + columnAnnotation.append("isLogicDelete = ").append(columnConfig.getLogicDelete()); needComma = true; } if (columnConfig.getVersion() != null) { - addComma(annotations, needComma); - annotations.append("version = ").append(columnConfig.getVersion()); + addComma(columnAnnotation, needComma); + columnAnnotation.append("version = ").append(columnConfig.getVersion()); needComma = true; } if (columnConfig.getJdbcType() != null) { - addComma(annotations, needComma); - annotations.append("jdbcType = JdbcType.").append(columnConfig.getJdbcType().name()); + addComma(columnAnnotation, needComma); + columnAnnotation.append("jdbcType = JdbcType.").append(columnConfig.getJdbcType().name()); needComma = true; } if (columnConfig.getTypeHandler() != null) { - addComma(annotations, needComma); - annotations.append("typeHandler = ").append(columnConfig.getTypeHandler().getSimpleName()).append(".class"); + addComma(columnAnnotation, needComma); + columnAnnotation.append("typeHandler = ").append(columnConfig.getTypeHandler().getSimpleName()).append(".class"); needComma = true; } if (Boolean.TRUE.equals(columnConfig.getTenantId())) { - addComma(annotations, needComma); - annotations.append("tenantId = true"); + addComma(columnAnnotation, needComma); + columnAnnotation.append("tenantId = true"); needComma = true; } if (entityConfig != null && entityConfig.isColumnCommentEnable() && StringUtil.isNotBlank(comment)) { - addComma(annotations, needComma); - String comment = this.comment.replace("\n", "").replace("\"", "\\\"").trim(); - annotations.append("comment = \"" + comment + "\""); + addComma(columnAnnotation, needComma); + columnAnnotation.append("comment = \"") + .append(this.comment.replace("\n", "") + .replace("\"", "\\\"") + .trim()) + .append("\""); } - annotations.append(")"); + columnAnnotation.append(")"); + + // @Column(value = "user_name") -> @Column("user_name") + int index = columnAnnotation.indexOf(","); + if (index == -1) { + int start = columnAnnotation.indexOf("value"); + if (start != -1) { + columnAnnotation.delete(start, start + 8); + } + } + + annotations.append(columnAnnotation); } //@ColumnMask 注解 @@ -329,12 +372,6 @@ public class Column { return annotations.toString(); } - private void addComma(StringBuilder annotations, boolean needComma) { - if (needComma) { - annotations.append(", "); - } - } - public Set getImportClasses() { Set importClasses = new LinkedHashSet<>(); @@ -363,7 +400,8 @@ public class Column { addImportClass(importClasses, columnConfig.getTypeHandler().getName()); } - boolean needGenColumnAnnotation = !name.equalsIgnoreCase(StringUtil.camelToUnderline(property)) + boolean needGenColumnAnnotation = (entityConfig != null && entityConfig.isAlwaysGenColumnAnnotation()) + || !name.equalsIgnoreCase(StringUtil.camelToUnderline(property)) || (entityConfig != null && entityConfig.isColumnCommentEnable() && StringUtil.isNotBlank(this.comment)); if (columnConfig.getOnInsertValue() != null @@ -383,24 +421,6 @@ public class Column { return importClasses; } - /** - * importClass为类的全限定名 - */ - private static void addImportClass(Set importClasses, String importClass) { - importClass = importClass.trim(); - - //java.util.List >>>>> java.util.List - if (importClass.contains("<") && importClass.endsWith(">")) { - importClass = importClass.substring(0, importClass.indexOf("<")); - } - - // 不包含“.”则认为是原始类型,不需要import - // lang 包不需要显式导入 - if (importClass.contains(".") && !importClass.startsWith("java.lang.")) { - importClasses.add(importClass); - } - } - public boolean isDefaultColumn() { if (columnConfig == null) { return true;