mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
feat(Column): 支持带有范型的类型
- 例如`java.util.Optional<java.time.LocalDateTime>`这种类型 Signed-off-by: benshi <807629978@qq.com>
This commit is contained in:
parent
82ed8229e2
commit
dcc9dfb187
@ -110,12 +110,58 @@ public class Column {
|
|||||||
if (!columnConfig.getPropertyType().contains(".")) {
|
if (!columnConfig.getPropertyType().contains(".")) {
|
||||||
return columnConfig.getPropertyType();
|
return columnConfig.getPropertyType();
|
||||||
}
|
}
|
||||||
return StringUtil.substringAfterLast(columnConfig.getPropertyType(), ".");
|
return convertToSimpleGenericType(columnConfig.getPropertyType());
|
||||||
} else {
|
} else {
|
||||||
return StringUtil.substringAfterLast(propertyType, ".");
|
return convertToSimpleGenericType(propertyType);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String convertToSimpleGenericType(String fullType) {
|
||||||
|
if (fullType == null || fullType.isEmpty()) {
|
||||||
|
return fullType;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果不包含泛型,直接处理
|
||||||
|
if (!fullType.contains("<") && !fullType.endsWith(">")) {
|
||||||
|
return StringUtil.substringAfterLast(fullType, ".");
|
||||||
|
}
|
||||||
|
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
|
int i = 0;
|
||||||
|
|
||||||
|
while (i < fullType.length()) {
|
||||||
|
char c = fullType.charAt(i);
|
||||||
|
|
||||||
|
if (c == '<' || c == ',' || c == '>') {
|
||||||
|
result.append(c);
|
||||||
|
if (c == ',' || c == '<') {
|
||||||
|
// 跳过空格
|
||||||
|
while (i + 1 < fullType.length() && fullType.charAt(i + 1) == ' ') {
|
||||||
|
i++;
|
||||||
|
result.append(' ');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
} else {
|
||||||
|
// 提取类型名称
|
||||||
|
int start = i;
|
||||||
|
while (i < fullType.length() && fullType.charAt(i) != '<' &&
|
||||||
|
fullType.charAt(i) != ',' && fullType.charAt(i) != '>') {
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
String typeName = fullType.substring(start, i).trim();
|
||||||
|
if (!typeName.isEmpty()) {
|
||||||
|
// 转换为简单类名
|
||||||
|
String simpleType = StringUtil.substringAfterLast(typeName, ".");
|
||||||
|
result.append(simpleType);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.toString();
|
||||||
|
}
|
||||||
|
|
||||||
public void setPropertyType(String propertyType) {
|
public void setPropertyType(String propertyType) {
|
||||||
this.propertyType = propertyType;
|
this.propertyType = propertyType;
|
||||||
}
|
}
|
||||||
@ -197,8 +243,8 @@ public class Column {
|
|||||||
return "";
|
return "";
|
||||||
} else {
|
} else {
|
||||||
return "/**\n" +
|
return "/**\n" +
|
||||||
" * " + comment + "\n" +
|
" * " + comment + "\n" +
|
||||||
" */";
|
" */";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -212,16 +258,41 @@ public class Column {
|
|||||||
*/
|
*/
|
||||||
private static void addImportClass(Set<String> importClasses, String importClass) {
|
private static void addImportClass(Set<String> importClasses, String importClass) {
|
||||||
importClass = importClass.trim();
|
importClass = importClass.trim();
|
||||||
|
extractAllTypes(importClasses, importClass);
|
||||||
|
}
|
||||||
|
|
||||||
// java.util.List<String> >>>>> java.util.List
|
private static void extractAllTypes(Set<String> importClasses, String typeString) {
|
||||||
if (importClass.contains("<") && importClass.endsWith(">")) {
|
if (typeString == null || typeString.isEmpty()) {
|
||||||
importClass = importClass.substring(0, importClass.indexOf("<"));
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 不包含“.”则认为是原始类型,不需要import
|
int i = 0;
|
||||||
// lang 包不需要显式导入
|
while (i < typeString.length()) {
|
||||||
if (importClass.contains(".") && !importClass.startsWith("java.lang.")) {
|
// 跳过非字母字符
|
||||||
importClasses.add(importClass);
|
while (i < typeString.length() && !Character.isLetter(typeString.charAt(i))
|
||||||
|
&& typeString.charAt(i) != '_') {
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (i >= typeString.length()) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 提取类名
|
||||||
|
int start = i;
|
||||||
|
while (i < typeString.length() &&
|
||||||
|
(Character.isLetterOrDigit(typeString.charAt(i)) ||
|
||||||
|
typeString.charAt(i) == '_' ||
|
||||||
|
typeString.charAt(i) == '.')) {
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
String className = typeString.substring(start, i);
|
||||||
|
|
||||||
|
// 检查是否包含包路径且不是java.lang包
|
||||||
|
if (className.contains(".") && !className.startsWith("java.lang.")) {
|
||||||
|
importClasses.add(className);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -234,7 +305,7 @@ public class Column {
|
|||||||
public String buildAnnotations() {
|
public String buildAnnotations() {
|
||||||
StringBuilder annotations = new StringBuilder();
|
StringBuilder annotations = new StringBuilder();
|
||||||
|
|
||||||
//@Id 的注解
|
// @Id 的注解
|
||||||
if (isPrimaryKey || columnConfig.isPrimaryKey()) {
|
if (isPrimaryKey || columnConfig.isPrimaryKey()) {
|
||||||
annotations.append("@Id(");
|
annotations.append("@Id(");
|
||||||
|
|
||||||
@ -259,14 +330,13 @@ public class Column {
|
|||||||
needComma = true;
|
needComma = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (entityConfig != null && entityConfig.isColumnCommentEnable() && StringUtil.hasText(comment)) {
|
if (entityConfig != null && entityConfig.isColumnCommentEnable() && StringUtil.hasText(comment)) {
|
||||||
addComma(annotations, needComma);
|
addComma(annotations, needComma);
|
||||||
annotations.append("comment = \"")
|
annotations.append("comment = \"")
|
||||||
.append(this.comment.replace("\n", "")
|
.append(this.comment.replace("\n", "")
|
||||||
.replace("\"", "\\\"")
|
.replace("\"", "\\\"")
|
||||||
.trim())
|
.trim())
|
||||||
.append("\"");
|
.append("\"");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (annotations.length() == 4) {
|
if (annotations.length() == 4) {
|
||||||
@ -281,25 +351,25 @@ public class Column {
|
|||||||
}
|
}
|
||||||
|
|
||||||
boolean needGenColumnAnnotation = (entityConfig != null && entityConfig.isAlwaysGenColumnAnnotation())
|
boolean needGenColumnAnnotation = (entityConfig != null && entityConfig.isAlwaysGenColumnAnnotation())
|
||||||
|| !name.equalsIgnoreCase(StringUtil.camelToUnderline(property))
|
|| !name.equalsIgnoreCase(StringUtil.camelToUnderline(property))
|
||||||
|| (entityConfig != null && entityConfig.isColumnCommentEnable() && StringUtil.hasText(this.comment) && annotations.length() == 0);
|
|| (entityConfig != null && entityConfig.isColumnCommentEnable() && StringUtil.hasText(this.comment)
|
||||||
|
&& annotations.length() == 0);
|
||||||
|
|
||||||
StringBuilder columnAnnotation = new StringBuilder("@Column(");
|
StringBuilder columnAnnotation = new StringBuilder("@Column(");
|
||||||
|
|
||||||
//@Column 注解
|
// @Column 注解
|
||||||
if (columnConfig.getOnInsertValue() != null
|
if (columnConfig.getOnInsertValue() != null
|
||||||
|| columnConfig.getOnUpdateValue() != null
|
|| columnConfig.getOnUpdateValue() != null
|
||||||
|| columnConfig.getLarge() != null
|
|| columnConfig.getLarge() != null
|
||||||
|| columnConfig.getLogicDelete() != null
|
|| columnConfig.getLogicDelete() != null
|
||||||
|| columnConfig.getVersion() != null
|
|| columnConfig.getVersion() != null
|
||||||
|| columnConfig.getJdbcType() != null
|
|| columnConfig.getJdbcType() != null
|
||||||
|| columnConfig.getTypeHandler() != null
|
|| columnConfig.getTypeHandler() != null
|
||||||
|| columnConfig.getTenantId() != null
|
|| columnConfig.getTenantId() != null
|
||||||
|| needGenColumnAnnotation
|
|| needGenColumnAnnotation) {
|
||||||
) {
|
|
||||||
boolean needComma = false;
|
boolean needComma = false;
|
||||||
if (entityConfig != null && entityConfig.isAlwaysGenColumnAnnotation()
|
if (entityConfig != null && entityConfig.isAlwaysGenColumnAnnotation()
|
||||||
|| !name.equalsIgnoreCase(StringUtil.camelToUnderline(property))) {
|
|| !name.equalsIgnoreCase(StringUtil.camelToUnderline(property))) {
|
||||||
columnAnnotation.append("value = \"").append(name).append("\"");
|
columnAnnotation.append("value = \"").append(name).append("\"");
|
||||||
needComma = true;
|
needComma = true;
|
||||||
}
|
}
|
||||||
@ -336,7 +406,8 @@ public class Column {
|
|||||||
}
|
}
|
||||||
if (columnConfig.getTypeHandler() != null) {
|
if (columnConfig.getTypeHandler() != null) {
|
||||||
addComma(columnAnnotation, needComma);
|
addComma(columnAnnotation, needComma);
|
||||||
columnAnnotation.append("typeHandler = ").append(columnConfig.getTypeHandler().getSimpleName()).append(".class");
|
columnAnnotation.append("typeHandler = ").append(columnConfig.getTypeHandler().getSimpleName())
|
||||||
|
.append(".class");
|
||||||
needComma = true;
|
needComma = true;
|
||||||
}
|
}
|
||||||
if (Boolean.TRUE.equals(columnConfig.getTenantId())) {
|
if (Boolean.TRUE.equals(columnConfig.getTenantId())) {
|
||||||
@ -347,10 +418,10 @@ public class Column {
|
|||||||
if (entityConfig != null && entityConfig.isColumnCommentEnable() && StringUtil.hasText(comment)) {
|
if (entityConfig != null && entityConfig.isColumnCommentEnable() && StringUtil.hasText(comment)) {
|
||||||
addComma(columnAnnotation, needComma);
|
addComma(columnAnnotation, needComma);
|
||||||
columnAnnotation.append("comment = \"")
|
columnAnnotation.append("comment = \"")
|
||||||
.append(this.comment.replace("\n", "")
|
.append(this.comment.replace("\n", "")
|
||||||
.replace("\"", "\\\"")
|
.replace("\"", "\\\"")
|
||||||
.trim())
|
.trim())
|
||||||
.append("\"");
|
.append("\"");
|
||||||
}
|
}
|
||||||
columnAnnotation.append(")");
|
columnAnnotation.append(")");
|
||||||
|
|
||||||
@ -418,19 +489,19 @@ public class Column {
|
|||||||
}
|
}
|
||||||
|
|
||||||
boolean needGenColumnAnnotation = (entityConfig != null && entityConfig.isAlwaysGenColumnAnnotation())
|
boolean needGenColumnAnnotation = (entityConfig != null && entityConfig.isAlwaysGenColumnAnnotation())
|
||||||
|| !name.equalsIgnoreCase(StringUtil.camelToUnderline(property))
|
|| !name.equalsIgnoreCase(StringUtil.camelToUnderline(property))
|
||||||
|| (entityConfig != null && entityConfig.isColumnCommentEnable() && StringUtil.hasText(this.comment));
|
|| (entityConfig != null && entityConfig.isColumnCommentEnable()
|
||||||
|
&& StringUtil.hasText(this.comment));
|
||||||
|
|
||||||
if (columnConfig.getOnInsertValue() != null
|
if (columnConfig.getOnInsertValue() != null
|
||||||
|| columnConfig.getOnUpdateValue() != null
|
|| columnConfig.getOnUpdateValue() != null
|
||||||
|| columnConfig.getLarge() != null
|
|| columnConfig.getLarge() != null
|
||||||
|| columnConfig.getLogicDelete() != null
|
|| columnConfig.getLogicDelete() != null
|
||||||
|| columnConfig.getVersion() != null
|
|| columnConfig.getVersion() != null
|
||||||
|| columnConfig.getJdbcType() != null
|
|| columnConfig.getJdbcType() != null
|
||||||
|| columnConfig.getTypeHandler() != null
|
|| columnConfig.getTypeHandler() != null
|
||||||
|| Boolean.TRUE.equals(columnConfig.getTenantId())
|
|| Boolean.TRUE.equals(columnConfig.getTenantId())
|
||||||
|| needGenColumnAnnotation
|
|| needGenColumnAnnotation) {
|
||||||
) {
|
|
||||||
addImportClass(importClasses, com.mybatisflex.annotation.Column.class.getName());
|
addImportClass(importClasses, com.mybatisflex.annotation.Column.class.getName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -450,11 +521,11 @@ public class Column {
|
|||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Column{" +
|
return "Column{" +
|
||||||
"name='" + name + '\'' +
|
"name='" + name + '\'' +
|
||||||
", className='" + propertyType + '\'' +
|
", className='" + propertyType + '\'' +
|
||||||
", remarks='" + comment + '\'' +
|
", remarks='" + comment + '\'' +
|
||||||
", isAutoIncrement=" + isAutoIncrement +
|
", isAutoIncrement=" + isAutoIncrement +
|
||||||
'}';
|
'}';
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user