修改ColumnConfig的propertyType字段类型为String,使其支持原始类型

ColumnConfig添加propertyDefaultValue字段,支持为实体类的属性添加默认值
This commit is contained in:
zack 2023-07-24 23:17:00 +08:00
parent 7fbec39530
commit 275968d39b
3 changed files with 60 additions and 30 deletions

View File

@ -61,9 +61,14 @@ public class ColumnConfig implements Serializable {
*/ */
private JdbcType jdbcType; private JdbcType jdbcType;
/** /**
* 属性的类型 * 属性的类型原始类型直接写类型名称int/long/float/double/boolean对象类型请写对应类的全限定名
*/ */
private Class<?> propertyType; private String propertyType;
/**
* 属性的默认值
*/
private String propertyDefaultValue;
/** /**
* 自定义 TypeHandler * 自定义 TypeHandler
@ -212,11 +217,19 @@ public class ColumnConfig implements Serializable {
this.tenantId = tenantId; this.tenantId = tenantId;
} }
public Class<?> getPropertyType() { public String getPropertyType() {
return propertyType; return propertyType;
} }
public void setPropertyType(Class<?> propertyType) { 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,22 @@ public class Column {
return propertyType; return propertyType;
} }
public String getPropertyDefaultValue() {
return columnConfig.getPropertyDefaultValue();
}
public String getPropertySimpleType() { public String getPropertySimpleType() {
if (columnConfig.getPropertyType()!=null){ try {
return columnConfig.getPropertyType().getSimpleName(); if (columnConfig.getPropertyType() != null) {
} if (!columnConfig.getPropertyType().contains(".")) {
else { return columnConfig.getPropertyType();
return propertyType.substring(propertyType.lastIndexOf(".") + 1); }
return Class.forName(columnConfig.getPropertyType()).getSimpleName();
} else {
return propertyType.substring(propertyType.lastIndexOf(".") + 1);
}
} catch (Throwable e) {
throw new RuntimeException(e);
} }
} }
@ -276,39 +288,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 +326,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}
}