mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-08 09:38:26 +08:00
feat: 支持配置多个别名。
This commit is contained in:
parent
fa3249c5b9
commit
3b298651b9
@ -25,6 +25,7 @@ import java.lang.annotation.*;
|
|||||||
* @since 2023-06-30
|
* @since 2023-06-30
|
||||||
*/
|
*/
|
||||||
@Inherited
|
@Inherited
|
||||||
|
@Repeatable(As.AsList.class)
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
@Target({ElementType.FIELD, ElementType.METHOD})
|
@Target({ElementType.FIELD, ElementType.METHOD})
|
||||||
public @interface As {
|
public @interface As {
|
||||||
@ -36,4 +37,21 @@ public @interface As {
|
|||||||
*/
|
*/
|
||||||
String value();
|
String value();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 支持多个别名。
|
||||||
|
*/
|
||||||
|
@Inherited
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target({ElementType.FIELD, ElementType.METHOD})
|
||||||
|
@interface AsList {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 全部别名。
|
||||||
|
*
|
||||||
|
* @return 别名
|
||||||
|
*/
|
||||||
|
As[] value();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -31,7 +31,7 @@ public class ColumnInfo {
|
|||||||
/**
|
/**
|
||||||
* 列的别名。
|
* 列的别名。
|
||||||
*/
|
*/
|
||||||
protected String alias;
|
protected String[] alias;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* java entity 定义的属性名称。
|
* java entity 定义的属性名称。
|
||||||
@ -67,11 +67,11 @@ public class ColumnInfo {
|
|||||||
this.column = column;
|
this.column = column;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getAlias() {
|
public String[] getAlias() {
|
||||||
return alias;
|
return alias;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAlias(String alias) {
|
public void setAlias(String[] alias) {
|
||||||
this.alias = alias;
|
this.alias = alias;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -808,14 +808,16 @@ public class TableInfo {
|
|||||||
.build();
|
.build();
|
||||||
resultMappings.add(mapping);
|
resultMappings.add(mapping);
|
||||||
|
|
||||||
// add alias mapping
|
|
||||||
if (columnInfo.alias != null) {
|
if (columnInfo.alias != null) {
|
||||||
ResultMapping aliasMapping = new ResultMapping.Builder(configuration, columnInfo.property,
|
// add alias mapping
|
||||||
columnInfo.alias, columnInfo.propertyType)
|
for (String alias : columnInfo.alias) {
|
||||||
.jdbcType(columnInfo.getJdbcType())
|
ResultMapping aliasMapping = new ResultMapping.Builder(configuration, columnInfo.property,
|
||||||
.typeHandler(columnInfo.buildTypeHandler())
|
alias, columnInfo.propertyType)
|
||||||
.build();
|
.jdbcType(columnInfo.getJdbcType())
|
||||||
resultMappings.add(aliasMapping);
|
.typeHandler(columnInfo.buildTypeHandler())
|
||||||
|
.build();
|
||||||
|
resultMappings.add(aliasMapping);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// add property mapper for sql: select xxx as property ...
|
// add property mapper for sql: select xxx as property ...
|
||||||
@ -839,15 +841,17 @@ public class TableInfo {
|
|||||||
.build();
|
.build();
|
||||||
resultMappings.add(mapping);
|
resultMappings.add(mapping);
|
||||||
|
|
||||||
// add alias mapping
|
|
||||||
if (idInfo.alias != null) {
|
if (idInfo.alias != null) {
|
||||||
ResultMapping aliasMapping = new ResultMapping.Builder(configuration, idInfo.property,
|
// add alias mapping
|
||||||
idInfo.alias, idInfo.propertyType)
|
for (String alias : idInfo.alias) {
|
||||||
.flags(CollectionUtil.newArrayList(ResultFlag.ID))
|
ResultMapping aliasMapping = new ResultMapping.Builder(configuration, idInfo.property,
|
||||||
.jdbcType(idInfo.getJdbcType())
|
alias, idInfo.propertyType)
|
||||||
.typeHandler(idInfo.buildTypeHandler())
|
.flags(CollectionUtil.newArrayList(ResultFlag.ID))
|
||||||
.build();
|
.jdbcType(idInfo.getJdbcType())
|
||||||
resultMappings.add(aliasMapping);
|
.typeHandler(idInfo.buildTypeHandler())
|
||||||
|
.build();
|
||||||
|
resultMappings.add(aliasMapping);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -288,19 +288,23 @@ public class TableInfoFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 优先使用属性上的别名
|
// 优先使用属性上的别名
|
||||||
As asType = field.getAnnotation(As.class);
|
As[] asType = field.getAnnotationsByType(As.class);
|
||||||
|
|
||||||
// 属性上没有别名,查找 getter 方法上有没有别名
|
// 属性上没有别名,查找 getter 方法上有没有别名
|
||||||
if (asType == null) {
|
if (asType.length == 0) {
|
||||||
String setterMethodName = "set" + StringUtil.firstCharToUpperCase(field.getName());
|
String setterMethodName = "set" + StringUtil.firstCharToUpperCase(field.getName());
|
||||||
Method setterMethod = ClassUtil.getFirstMethod(entityClass, m -> m.getName().equals(setterMethodName));
|
Method setterMethod = ClassUtil.getFirstMethod(entityClass, m -> m.getName().equals(setterMethodName));
|
||||||
if (setterMethod != null) {
|
if (setterMethod != null) {
|
||||||
asType = setterMethod.getAnnotation(As.class);
|
asType = setterMethod.getAnnotationsByType(As.class);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (asType != null) {
|
if (asType.length > 0) {
|
||||||
columnInfo.setAlias(asType.value());
|
columnInfo.setAlias(
|
||||||
|
Arrays.stream(asType)
|
||||||
|
.map(As::value)
|
||||||
|
.toArray(String[]::new)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
columnInfo.setColumn(columnName);
|
columnInfo.setColumn(columnName);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user