mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 17:18:24 +08:00
!186 修改代码生成器中ColumnConfig类,propertyType改为String,使其支持原始类型,添加propertyDefaultValue,支持为实体类的属性添加默认值
Merge pull request !186 from pengpeng/main
This commit is contained in:
commit
03579122f3
@ -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 驱动的数据类型为自定义的
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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<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 +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<String> importClasses, String importClass) {
|
||||
// 不包含“.”则认为是原始类型,不需要import
|
||||
// lang包不需要显式导入
|
||||
if (importClass.contains(".") && !importClass.startsWith("java.lang.")) {
|
||||
importClasses.add(importClass);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isDefaultColumn() {
|
||||
if (columnConfig == null) {
|
||||
return true;
|
||||
|
||||
@ -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}
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user