!186 修改代码生成器中ColumnConfig类,propertyType改为String,使其支持原始类型,添加propertyDefaultValue,支持为实体类的属性添加默认值

Merge pull request !186 from pengpeng/main
This commit is contained in:
Michael Yang 2023-07-26 01:41:57 +00:00 committed by Gitee
commit 03579122f3
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
5 changed files with 83 additions and 29 deletions

View File

@ -449,9 +449,22 @@ public class ColumnConfig implements Serializable {
// 是否是租户列 // 是否是租户列
private Boolean tenantId; 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 驱动的数据类型为自定义的 MyBatis-Flex 内置了一个名为:`JdbcTypeMapping` 的 java 类,我们可以用其配置映射 Jdbc 驱动的数据类型为自定义的

View File

@ -60,10 +60,16 @@ public class ColumnConfig implements Serializable {
* 配置的 jdbcType * 配置的 jdbcType
*/ */
private JdbcType jdbcType; private JdbcType jdbcType;
/** /**
* 属性的类型 * 属性的类型
*/ */
private Class<?> propertyType; private String propertyType;
/**
* 属性的默认值
*/
private String propertyDefaultValue;
/** /**
* 自定义 TypeHandler * 自定义 TypeHandler
@ -211,11 +217,23 @@ public class ColumnConfig implements Serializable {
public void setTenantId(Boolean tenantId) { public void setTenantId(Boolean tenantId) {
this.tenantId = tenantId; this.tenantId = tenantId;
} }
public Class<?> getPropertyType() {
public String getPropertyType() {
return propertyType; return propertyType;
} }
public void setPropertyType(Class<?> propertyType) { /**
* 原始类型直接写类型名称int/long/float/double/boolean对象类型请写对应类的全限定名java.lang.String
*/
public void setPropertyType(String propertyType) {
this.propertyType = propertyType; this.propertyType = propertyType;
} }
public String getPropertyDefaultValue() {
return propertyDefaultValue;
}
public void setPropertyDefaultValue(String propertyDefaultValue) {
this.propertyDefaultValue = propertyDefaultValue;
}
} }

View File

@ -22,7 +22,9 @@ import com.mybatisflex.codegen.config.ColumnConfig;
import com.mybatisflex.core.util.StringUtil; import com.mybatisflex.core.util.StringUtil;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Set;
/** /**
* 数据库表里面的列信息 * 数据库表里面的列信息
@ -92,12 +94,18 @@ public class Column {
return propertyType; return propertyType;
} }
public String getPropertyDefaultValue() {
return columnConfig.getPropertyDefaultValue();
}
public String getPropertySimpleType() { public String getPropertySimpleType() {
if (columnConfig.getPropertyType()!=null){ if (columnConfig.getPropertyType() != null) {
return columnConfig.getPropertyType().getSimpleName(); if (!columnConfig.getPropertyType().contains(".")) {
} return columnConfig.getPropertyType();
else { }
return propertyType.substring(propertyType.lastIndexOf(".") + 1); return StringUtil.substringAfterLast(columnConfig.getPropertyType(), ".");
} else {
return StringUtil.substringAfterLast(propertyType, ".");
} }
} }
@ -276,39 +284,32 @@ public class Column {
} }
} }
public List<String> getImportClasses() { public Set<String> getImportClasses() {
List<String> importClasses = new ArrayList<>(); Set<String> importClasses = new LinkedHashSet<>();
//lang 包不需要显式导入
if (!propertyType.startsWith("java.lang.")
&& !"byte[]".equals(propertyType)
&& !"Byte[]".equals(propertyType)
) {
importClasses.add(propertyType);
}
addImportClass(importClasses, propertyType);
if (isPrimaryKey || (columnConfig != null && columnConfig.isPrimaryKey())) { if (isPrimaryKey || (columnConfig != null && columnConfig.isPrimaryKey())) {
importClasses.add(Id.class.getName()); addImportClass(importClasses, Id.class.getName());
if (isAutoIncrement || (columnConfig != null && columnConfig.getKeyType() != null)) { if (isAutoIncrement || (columnConfig != null && columnConfig.getKeyType() != null)) {
importClasses.add(KeyType.class.getName()); addImportClass(importClasses, KeyType.class.getName());
} }
} }
if (columnConfig != null) { if (columnConfig != null) {
if (columnConfig.getPropertyType() !=null){ if (columnConfig.getPropertyType() != null) {
importClasses.add(columnConfig.getPropertyType().getName()); addImportClass(importClasses, columnConfig.getPropertyType());
} }
if (columnConfig.getMask() != null) { if (columnConfig.getMask() != null) {
importClasses.add(ColumnMask.class.getName()); addImportClass(importClasses, ColumnMask.class.getName());
} }
if (columnConfig.getJdbcType() != null) { if (columnConfig.getJdbcType() != null) {
importClasses.add("org.apache.ibatis.type.JdbcType"); addImportClass(importClasses, "org.apache.ibatis.type.JdbcType");
} }
if (columnConfig.getTypeHandler() != null) { if (columnConfig.getTypeHandler() != null) {
importClasses.add(columnConfig.getTypeHandler().getName()); addImportClass(importClasses, columnConfig.getTypeHandler().getName());
} }
if (columnConfig.getOnInsertValue() != null if (columnConfig.getOnInsertValue() != null
@ -321,13 +322,24 @@ public class Column {
|| Boolean.TRUE.equals(columnConfig.getTenantId()) || Boolean.TRUE.equals(columnConfig.getTenantId())
|| needGenColumnAnnotation || needGenColumnAnnotation
) { ) {
importClasses.add(com.mybatisflex.annotation.Column.class.getName()); addImportClass(importClasses, com.mybatisflex.annotation.Column.class.getName());
} }
} }
return importClasses; return importClasses;
} }
/**
* importClass为类的全限定名
*/
private static void addImportClass(Set<String> importClasses, String importClass) {
// 不包含.则认为是原始类型不需要import
// lang包不需要显式导入
if (importClass.contains(".") && !importClass.startsWith("java.lang.")) {
importClasses.add(importClass);
}
}
public boolean isDefaultColumn() { public boolean isDefaultColumn() {
if (columnConfig == null) { if (columnConfig == null) {
return true; return true;

View File

@ -40,6 +40,7 @@ import lombok.NoArgsConstructor;
#end #end
#(table.buildTableAnnotation()) #(table.buildTableAnnotation())
public class #(table.buildEntityClassName())#(table.buildExtends())#(table.buildImplements()) { public class #(table.buildEntityClassName())#(table.buildExtends())#(table.buildImplements()) {
#for(column : table.columns) #for(column : table.columns)
#set(comment = javadocConfig.formatColumnComment(column.comment)) #set(comment = javadocConfig.formatColumnComment(column.comment))
#if(isNotBlank(comment)) #if(isNotBlank(comment))
@ -57,7 +58,8 @@ public class #(table.buildEntityClassName())#(table.buildExtends())#(table.build
#if(withSwagger && swaggerVersion.getName() == "DOC") #if(withSwagger && swaggerVersion.getName() == "DOC")
@Schema(description = "#(column.comment)") @Schema(description = "#(column.comment)")
#end #end
private #(column.propertySimpleType) #(column.property); private #(column.propertySimpleType) #(column.property)#if(isNotBlank(column.propertyDefaultValue)) = #(column.propertyDefaultValue)#end;
#end #end
#if(!withLombok) #if(!withLombok)
@ -71,5 +73,4 @@ public class #(table.buildEntityClassName())#(table.buildExtends())#(table.build
} }
#end #end
#end #end}
}

View File

@ -287,5 +287,15 @@ public class StringUtil {
return string != null ? string.trim() : null; 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);
}
} }