feat: 支持配置多个别名。

This commit is contained in:
Suomm 2023-06-30 22:12:17 +08:00
parent fa3249c5b9
commit 3b298651b9
4 changed files with 49 additions and 23 deletions

View File

@ -25,6 +25,7 @@ import java.lang.annotation.*;
* @since 2023-06-30
*/
@Inherited
@Repeatable(As.AsList.class)
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
public @interface As {
@ -36,4 +37,21 @@ public @interface As {
*/
String value();
/**
* 支持多个别名
*/
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
@interface AsList {
/**
* 全部别名
*
* @return 别名
*/
As[] value();
}
}

View File

@ -31,7 +31,7 @@ public class ColumnInfo {
/**
* 列的别名
*/
protected String alias;
protected String[] alias;
/**
* java entity 定义的属性名称
@ -67,11 +67,11 @@ public class ColumnInfo {
this.column = column;
}
public String getAlias() {
public String[] getAlias() {
return alias;
}
public void setAlias(String alias) {
public void setAlias(String[] alias) {
this.alias = alias;
}

View File

@ -808,14 +808,16 @@ public class TableInfo {
.build();
resultMappings.add(mapping);
// add alias mapping
if (columnInfo.alias != null) {
ResultMapping aliasMapping = new ResultMapping.Builder(configuration, columnInfo.property,
columnInfo.alias, columnInfo.propertyType)
.jdbcType(columnInfo.getJdbcType())
.typeHandler(columnInfo.buildTypeHandler())
.build();
resultMappings.add(aliasMapping);
// add alias mapping
for (String alias : columnInfo.alias) {
ResultMapping aliasMapping = new ResultMapping.Builder(configuration, columnInfo.property,
alias, columnInfo.propertyType)
.jdbcType(columnInfo.getJdbcType())
.typeHandler(columnInfo.buildTypeHandler())
.build();
resultMappings.add(aliasMapping);
}
}
// add property mapper for sql: select xxx as property ...
@ -839,15 +841,17 @@ public class TableInfo {
.build();
resultMappings.add(mapping);
// add alias mapping
if (idInfo.alias != null) {
ResultMapping aliasMapping = new ResultMapping.Builder(configuration, idInfo.property,
idInfo.alias, idInfo.propertyType)
.flags(CollectionUtil.newArrayList(ResultFlag.ID))
.jdbcType(idInfo.getJdbcType())
.typeHandler(idInfo.buildTypeHandler())
.build();
resultMappings.add(aliasMapping);
// add alias mapping
for (String alias : idInfo.alias) {
ResultMapping aliasMapping = new ResultMapping.Builder(configuration, idInfo.property,
alias, idInfo.propertyType)
.flags(CollectionUtil.newArrayList(ResultFlag.ID))
.jdbcType(idInfo.getJdbcType())
.typeHandler(idInfo.buildTypeHandler())
.build();
resultMappings.add(aliasMapping);
}
}
}

View File

@ -288,19 +288,23 @@ public class TableInfoFactory {
}
// 优先使用属性上的别名
As asType = field.getAnnotation(As.class);
As[] asType = field.getAnnotationsByType(As.class);
// 属性上没有别名查找 getter 方法上有没有别名
if (asType == null) {
if (asType.length == 0) {
String setterMethodName = "set" + StringUtil.firstCharToUpperCase(field.getName());
Method setterMethod = ClassUtil.getFirstMethod(entityClass, m -> m.getName().equals(setterMethodName));
if (setterMethod != null) {
asType = setterMethod.getAnnotation(As.class);
asType = setterMethod.getAnnotationsByType(As.class);
}
}
if (asType != null) {
columnInfo.setAlias(asType.value());
if (asType.length > 0) {
columnInfo.setAlias(
Arrays.stream(asType)
.map(As::value)
.toArray(String[]::new)
);
}
columnInfo.setColumn(columnName);