diff --git a/docs/zh/others/codegen.md b/docs/zh/others/codegen.md index 5115848e..a222813d 100644 --- a/docs/zh/others/codegen.md +++ b/docs/zh/others/codegen.md @@ -449,9 +449,22 @@ public class ColumnConfig implements Serializable { // 是否是租户列 private Boolean tenantId; + + /** + * 属性的类型。 + * 原始类型直接写类型名称,例:int/long/float/double/boolean + * 对象类型请写对应类的全限定名,例:java.lang.String/com.abc.def.enums.Gender + */ + private String propertyType; + + /** + * 属性的默认值, 例:long类型默认值:0L,枚举类型默认值:Gender.MALE + */ + private String propertyDefaultValue; } ``` + ## 自定义属性类型 MyBatis-Flex 内置了一个名为:`JdbcTypeMapping` 的 java 类,我们可以用其配置映射 Jdbc 驱动的数据类型为自定义的 diff --git a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/ColumnConfig.java b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/ColumnConfig.java index 7be95683..2352fc86 100644 --- a/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/ColumnConfig.java +++ b/mybatis-flex-codegen/src/main/java/com/mybatisflex/codegen/config/ColumnConfig.java @@ -60,10 +60,16 @@ public class ColumnConfig implements Serializable { * 配置的 jdbcType。 */ private JdbcType jdbcType; + /** * 属性的类型。 */ - private Class propertyType; + private String propertyType; + + /** + * 属性的默认值 + */ + private String propertyDefaultValue; /** * 自定义 TypeHandler。 @@ -211,11 +217,23 @@ public class ColumnConfig implements Serializable { public void setTenantId(Boolean tenantId) { this.tenantId = tenantId; } - public Class getPropertyType() { + + public String getPropertyType() { return propertyType; } - public void setPropertyType(Class propertyType) { + /** + * 原始类型直接写类型名称,例:int/long/float/double/boolean,对象类型请写对应类的全限定名,例:java.lang.String + */ + public void setPropertyType(String propertyType) { this.propertyType = propertyType; } + + public String getPropertyDefaultValue() { + return propertyDefaultValue; + } + + public void setPropertyDefaultValue(String propertyDefaultValue) { + this.propertyDefaultValue = propertyDefaultValue; + } } 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 d1efb73f..fc1ace11 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 @@ -22,7 +22,9 @@ import com.mybatisflex.codegen.config.ColumnConfig; import com.mybatisflex.core.util.StringUtil; import java.util.ArrayList; +import java.util.LinkedHashSet; import java.util.List; +import java.util.Set; /** * 数据库表里面的列信息。 @@ -92,12 +94,18 @@ public class Column { return propertyType; } + public String getPropertyDefaultValue() { + return columnConfig.getPropertyDefaultValue(); + } + public String getPropertySimpleType() { - if (columnConfig.getPropertyType()!=null){ - return columnConfig.getPropertyType().getSimpleName(); - } - else { - return propertyType.substring(propertyType.lastIndexOf(".") + 1); + if (columnConfig.getPropertyType() != null) { + if (!columnConfig.getPropertyType().contains(".")) { + return columnConfig.getPropertyType(); + } + return StringUtil.substringAfterLast(columnConfig.getPropertyType(), "."); + } else { + return StringUtil.substringAfterLast(propertyType, "."); } } @@ -276,39 +284,32 @@ public class Column { } } - public List getImportClasses() { - List importClasses = new ArrayList<>(); - - //lang 包不需要显式导入 - if (!propertyType.startsWith("java.lang.") - && !"byte[]".equals(propertyType) - && !"Byte[]".equals(propertyType) - ) { - importClasses.add(propertyType); - } + public Set getImportClasses() { + Set importClasses = new LinkedHashSet<>(); + addImportClass(importClasses, propertyType); if (isPrimaryKey || (columnConfig != null && columnConfig.isPrimaryKey())) { - importClasses.add(Id.class.getName()); + addImportClass(importClasses, Id.class.getName()); if (isAutoIncrement || (columnConfig != null && columnConfig.getKeyType() != null)) { - importClasses.add(KeyType.class.getName()); + addImportClass(importClasses, KeyType.class.getName()); } } if (columnConfig != null) { - if (columnConfig.getPropertyType() !=null){ - importClasses.add(columnConfig.getPropertyType().getName()); + if (columnConfig.getPropertyType() != null) { + addImportClass(importClasses, columnConfig.getPropertyType()); } if (columnConfig.getMask() != null) { - importClasses.add(ColumnMask.class.getName()); + addImportClass(importClasses, ColumnMask.class.getName()); } if (columnConfig.getJdbcType() != null) { - importClasses.add("org.apache.ibatis.type.JdbcType"); + addImportClass(importClasses, "org.apache.ibatis.type.JdbcType"); } if (columnConfig.getTypeHandler() != null) { - importClasses.add(columnConfig.getTypeHandler().getName()); + addImportClass(importClasses, columnConfig.getTypeHandler().getName()); } if (columnConfig.getOnInsertValue() != null @@ -321,13 +322,24 @@ public class Column { || Boolean.TRUE.equals(columnConfig.getTenantId()) || needGenColumnAnnotation ) { - importClasses.add(com.mybatisflex.annotation.Column.class.getName()); + addImportClass(importClasses, com.mybatisflex.annotation.Column.class.getName()); } } return importClasses; } + /** + * importClass为类的全限定名 + */ + private static void addImportClass(Set importClasses, String importClass) { + // 不包含“.”则认为是原始类型,不需要import + // lang包不需要显式导入 + if (importClass.contains(".") && !importClass.startsWith("java.lang.")) { + importClasses.add(importClass); + } + } + public boolean isDefaultColumn() { if (columnConfig == null) { return true; diff --git a/mybatis-flex-codegen/src/main/resources/templates/enjoy/entity.tpl b/mybatis-flex-codegen/src/main/resources/templates/enjoy/entity.tpl index 12966c13..cb79e4e5 100644 --- a/mybatis-flex-codegen/src/main/resources/templates/enjoy/entity.tpl +++ b/mybatis-flex-codegen/src/main/resources/templates/enjoy/entity.tpl @@ -40,6 +40,7 @@ import lombok.NoArgsConstructor; #end #(table.buildTableAnnotation()) public class #(table.buildEntityClassName())#(table.buildExtends())#(table.buildImplements()) { + #for(column : table.columns) #set(comment = javadocConfig.formatColumnComment(column.comment)) #if(isNotBlank(comment)) @@ -57,7 +58,8 @@ public class #(table.buildEntityClassName())#(table.buildExtends())#(table.build #if(withSwagger && swaggerVersion.getName() == "DOC") @Schema(description = "#(column.comment)") #end - private #(column.propertySimpleType) #(column.property); + private #(column.propertySimpleType) #(column.property)#if(isNotBlank(column.propertyDefaultValue)) = #(column.propertyDefaultValue)#end; + #end #if(!withLombok) @@ -71,5 +73,4 @@ public class #(table.buildEntityClassName())#(table.buildExtends())#(table.build } #end -#end -} +#end} diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/StringUtil.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/StringUtil.java index 7cbfc3c6..25bfe22c 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/StringUtil.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/StringUtil.java @@ -287,5 +287,15 @@ public class StringUtil { return string != null ? string.trim() : null; } + public static String substringAfterLast(String text, String str) { + if (text == null) { + return null; + } + if (str == null) { + return text; + } + return text.substring(text.lastIndexOf(str) + 1); + } + }