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
914248c89e
commit
6288556076
@ -989,10 +989,10 @@ public class TableInfo {
|
|||||||
|
|
||||||
|
|
||||||
public ResultMap buildResultMap(Configuration configuration) {
|
public ResultMap buildResultMap(Configuration configuration) {
|
||||||
return doBuildResultMap(configuration, new HashSet<>(), new HashSet<>(), false, getTableNameWithSchema());
|
return doBuildResultMap(configuration, new HashSet<>(), false, getTableNameWithSchema());
|
||||||
}
|
}
|
||||||
|
|
||||||
private ResultMap doBuildResultMap(Configuration configuration, Set<String> resultMapIds, Set<String> existMappingColumns, boolean isNested, String nestedPrefix) {
|
private ResultMap doBuildResultMap(Configuration configuration, Set<String> resultMapIds, boolean isNested, String nestedPrefix) {
|
||||||
|
|
||||||
String resultMapId = isNested ? "nested-" + nestedPrefix + ":" + entityClass.getName() : entityClass.getName();
|
String resultMapId = isNested ? "nested-" + nestedPrefix + ":" + entityClass.getName() : entityClass.getName();
|
||||||
|
|
||||||
@ -1007,27 +1007,26 @@ public class TableInfo {
|
|||||||
if (configuration.hasResultMap(resultMapId)) {
|
if (configuration.hasResultMap(resultMapId)) {
|
||||||
return configuration.getResultMap(resultMapId);
|
return configuration.getResultMap(resultMapId);
|
||||||
}
|
}
|
||||||
List<ResultMapping> resultMappings = new ArrayList<>();
|
|
||||||
|
|
||||||
|
List<ResultMapping> resultMappings = new ArrayList<>();
|
||||||
|
|
||||||
// <resultMap> 标签下的 <id> 标签映射
|
// <resultMap> 标签下的 <id> 标签映射
|
||||||
for (IdInfo idInfo : primaryKeyList) {
|
for (IdInfo idInfo : primaryKeyList) {
|
||||||
doBuildColumnResultMapping(configuration, existMappingColumns, resultMappings, idInfo, CollectionUtil.newArrayList(ResultFlag.ID), isNested);
|
doBuildColumnResultMapping(configuration, resultMappings, idInfo, CollectionUtil.newArrayList(ResultFlag.ID));
|
||||||
}
|
}
|
||||||
|
|
||||||
// <resultMap> 标签下的 <result> 标签映射
|
// <resultMap> 标签下的 <result> 标签映射
|
||||||
for (ColumnInfo columnInfo : columnInfoList) {
|
for (ColumnInfo columnInfo : columnInfoList) {
|
||||||
doBuildColumnResultMapping(configuration, existMappingColumns, resultMappings, columnInfo, Collections.emptyList(), isNested);
|
doBuildColumnResultMapping(configuration, resultMappings, columnInfo, Collections.emptyList());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// <resultMap> 标签下的 <association> 标签映射
|
// <resultMap> 标签下的 <association> 标签映射
|
||||||
if (associationType != null) {
|
if (associationType != null) {
|
||||||
associationType.forEach((fieldName, fieldType) -> {
|
associationType.forEach((fieldName, fieldType) -> {
|
||||||
// 获取嵌套类型的信息,也就是 javaType 属性
|
// 获取嵌套类型的信息,也就是 javaType 属性
|
||||||
TableInfo tableInfo = TableInfoFactory.ofEntityClass(fieldType);
|
TableInfo tableInfo = TableInfoFactory.ofEntityClass(fieldType);
|
||||||
// 构建嵌套类型的 ResultMap 对象,也就是 <association> 标签下的内容
|
// 构建嵌套类型的 ResultMap 对象,也就是 <association> 标签下的内容
|
||||||
ResultMap nestedResultMap = tableInfo.doBuildResultMap(configuration, resultMapIds, existMappingColumns, true, nestedPrefix);
|
ResultMap nestedResultMap = tableInfo.doBuildResultMap(configuration, resultMapIds, true, nestedPrefix);
|
||||||
if (nestedResultMap != null) {
|
if (nestedResultMap != null) {
|
||||||
resultMappings.add(new ResultMapping.Builder(configuration, fieldName)
|
resultMappings.add(new ResultMapping.Builder(configuration, fieldName)
|
||||||
.javaType(fieldType)
|
.javaType(fieldType)
|
||||||
@ -1044,16 +1043,12 @@ public class TableInfo {
|
|||||||
// List<String> List<Integer> 等
|
// List<String> List<Integer> 等
|
||||||
String columnName = TableInfoFactory.getColumnName(camelToUnderline, field, field.getAnnotation(Column.class));
|
String columnName = TableInfoFactory.getColumnName(camelToUnderline, field, field.getAnnotation(Column.class));
|
||||||
// 映射 <result column="..."/>
|
// 映射 <result column="..."/>
|
||||||
|
|
||||||
ResultMapping resultMapping = new ResultMapping.Builder(configuration, null)
|
ResultMapping resultMapping = new ResultMapping.Builder(configuration, null)
|
||||||
.column(columnName)
|
.column(columnName)
|
||||||
.typeHandler(configuration.getTypeHandlerRegistry().getTypeHandler(genericClass))
|
.typeHandler(configuration.getTypeHandlerRegistry().getTypeHandler(genericClass))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
String nestedResultMapId = entityClass.getName() + "." + field.getName();
|
String nestedResultMapId = entityClass.getName() + "." + field.getName();
|
||||||
ResultMap nestedResultMap = new ResultMap.Builder(configuration, nestedResultMapId, genericClass
|
ResultMap nestedResultMap = new ResultMap.Builder(configuration, nestedResultMapId, genericClass, Collections.singletonList(resultMapping)).build();
|
||||||
, Collections.singletonList(resultMapping)).build();
|
|
||||||
|
|
||||||
configuration.addResultMap(nestedResultMap);
|
configuration.addResultMap(nestedResultMap);
|
||||||
// 映射 <collection property="..." ofType="genericClass">
|
// 映射 <collection property="..." ofType="genericClass">
|
||||||
resultMappings.add(new ResultMapping.Builder(configuration, field.getName())
|
resultMappings.add(new ResultMapping.Builder(configuration, field.getName())
|
||||||
@ -1064,7 +1059,7 @@ public class TableInfo {
|
|||||||
// 获取集合泛型类型的信息,也就是 ofType 属性
|
// 获取集合泛型类型的信息,也就是 ofType 属性
|
||||||
TableInfo tableInfo = TableInfoFactory.ofEntityClass(genericClass);
|
TableInfo tableInfo = TableInfoFactory.ofEntityClass(genericClass);
|
||||||
// 构建嵌套类型的 ResultMap 对象,也就是 <collection> 标签下的内容
|
// 构建嵌套类型的 ResultMap 对象,也就是 <collection> 标签下的内容
|
||||||
ResultMap nestedResultMap = tableInfo.doBuildResultMap(configuration, resultMapIds, existMappingColumns, true, nestedPrefix);
|
ResultMap nestedResultMap = tableInfo.doBuildResultMap(configuration, resultMapIds, true, nestedPrefix);
|
||||||
if (nestedResultMap != null) {
|
if (nestedResultMap != null) {
|
||||||
resultMappings.add(new ResultMapping.Builder(configuration, field.getName())
|
resultMappings.add(new ResultMapping.Builder(configuration, field.getName())
|
||||||
.javaType(field.getType())
|
.javaType(field.getType())
|
||||||
@ -1081,42 +1076,55 @@ public class TableInfo {
|
|||||||
return resultMap;
|
return resultMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void doBuildColumnResultMapping(Configuration configuration, List<ResultMapping> resultMappings
|
||||||
|
, ColumnInfo columnInfo, List<ResultFlag> flags) {
|
||||||
|
|
||||||
private void doBuildColumnResultMapping(Configuration configuration, Set<String> existMappingColumns, List<ResultMapping> resultMappings
|
// userName -> user_name
|
||||||
, ColumnInfo columnInfo, List<ResultFlag> flags, boolean isNested) {
|
resultMappings.add(new ResultMapping.Builder(configuration
|
||||||
String[] columns = ArrayUtil.concat(new String[]{columnInfo.column, columnInfo.property}, columnInfo.alias);
|
|
||||||
for (String column : columns) {
|
|
||||||
if (!existMappingColumns.contains(column)) {
|
|
||||||
ResultMapping mapping = new ResultMapping.Builder(configuration
|
|
||||||
, columnInfo.property
|
, columnInfo.property
|
||||||
, column
|
, columnInfo.column
|
||||||
, columnInfo.propertyType)
|
, columnInfo.propertyType)
|
||||||
.jdbcType(columnInfo.getJdbcType())
|
.jdbcType(columnInfo.getJdbcType())
|
||||||
.flags(flags)
|
.flags(flags)
|
||||||
.typeHandler(columnInfo.buildTypeHandler(configuration))
|
.typeHandler(columnInfo.buildTypeHandler(configuration))
|
||||||
.build();
|
.build());
|
||||||
resultMappings.add(mapping);
|
|
||||||
existMappingColumns.add(mapping.getColumn());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isNested) {
|
// userName -> tb_user$user_name
|
||||||
for (String column : columns) {
|
resultMappings.add(new ResultMapping.Builder(configuration
|
||||||
column = tableName + "$" + column;
|
|
||||||
if (!existMappingColumns.contains(column)) {
|
|
||||||
ResultMapping mapping = new ResultMapping.Builder(configuration
|
|
||||||
, columnInfo.property
|
, columnInfo.property
|
||||||
, column
|
, tableName + "$" + columnInfo.column
|
||||||
, columnInfo.propertyType)
|
, columnInfo.propertyType)
|
||||||
.jdbcType(columnInfo.getJdbcType())
|
.jdbcType(columnInfo.getJdbcType())
|
||||||
.flags(flags)
|
.flags(flags)
|
||||||
.typeHandler(columnInfo.buildTypeHandler(configuration))
|
.typeHandler(columnInfo.buildTypeHandler(configuration))
|
||||||
.build();
|
.build());
|
||||||
resultMappings.add(mapping);
|
|
||||||
existMappingColumns.add(mapping.getColumn());
|
if (!Objects.equals(columnInfo.column, columnInfo.property)) {
|
||||||
}
|
// userName -> userName
|
||||||
|
resultMappings.add(new ResultMapping.Builder(configuration
|
||||||
|
, columnInfo.property
|
||||||
|
, columnInfo.property
|
||||||
|
, columnInfo.propertyType)
|
||||||
|
.jdbcType(columnInfo.getJdbcType())
|
||||||
|
.flags(flags)
|
||||||
|
.typeHandler(columnInfo.buildTypeHandler(configuration))
|
||||||
|
.build());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ArrayUtil.isNotEmpty(columnInfo.alias)) {
|
||||||
|
for (String alias : columnInfo.alias) {
|
||||||
|
// userName -> alias
|
||||||
|
resultMappings.add(new ResultMapping.Builder(configuration
|
||||||
|
, columnInfo.property
|
||||||
|
, alias
|
||||||
|
, columnInfo.propertyType)
|
||||||
|
.jdbcType(columnInfo.getJdbcType())
|
||||||
|
.flags(flags)
|
||||||
|
.typeHandler(columnInfo.buildTypeHandler(configuration))
|
||||||
|
.build());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user