修改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;
/**
* 属性的类型
* 属性的类型原始类型直接写类型名称int/long/float/double/boolean对象类型请写对应类的全限定名
*/
private Class<?> propertyType;
private String propertyType;
/**
* 属性的默认值
*/
private String propertyDefaultValue;
/**
* 自定义 TypeHandler
@ -212,11 +217,19 @@ public class ColumnConfig implements Serializable {
this.tenantId = tenantId;
}
public Class<?> getPropertyType() {
public String getPropertyType() {
return propertyType;
}
public void setPropertyType(Class<?> propertyType) {
public void setPropertyType(String 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 java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
/**
* 数据库表里面的列信息
@ -92,13 +94,23 @@ public class Column {
return propertyType;
}
public String getPropertySimpleType() {
if (columnConfig.getPropertyType()!=null){
return columnConfig.getPropertyType().getSimpleName();
public String getPropertyDefaultValue() {
return columnConfig.getPropertyDefaultValue();
}
else {
public String getPropertySimpleType() {
try {
if (columnConfig.getPropertyType() != null) {
if (!columnConfig.getPropertyType().contains(".")) {
return columnConfig.getPropertyType();
}
return Class.forName(columnConfig.getPropertyType()).getSimpleName();
} else {
return propertyType.substring(propertyType.lastIndexOf(".") + 1);
}
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
public void setPropertyType(String propertyType) {
@ -276,39 +288,32 @@ public class Column {
}
}
public List<String> getImportClasses() {
List<String> importClasses = new ArrayList<>();
//lang 包不需要显式导入
if (!propertyType.startsWith("java.lang.")
&& !"byte[]".equals(propertyType)
&& !"Byte[]".equals(propertyType)
) {
importClasses.add(propertyType);
}
public Set<String> getImportClasses() {
Set<String> 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 +326,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<String> importClasses, String importClass) {
// 不包含.则认为是原始类型不需要import
// lang包不需要显式导入
if (importClass.contains(".") && !importClass.startsWith("java.lang.")) {
importClasses.add(importClass);
}
}
public boolean isDefaultColumn() {
if (columnConfig == null) {
return true;

View File

@ -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}