optimize TableInfo.doBuildResultMap and close #I7D9JX

This commit is contained in:
开源海哥 2023-06-14 09:07:49 +08:00
parent 04da02260e
commit 25136b0192

View File

@ -694,7 +694,11 @@ public class TableInfo {
//是否有循环引用
boolean withCircularReference = context.contains(entityClass.getName());
String resultMapId = entityClass.getName() + (withCircularReference ? "$nested" : "");
if (withCircularReference) {
return null;
}
String resultMapId = entityClass.getName();
context.add(resultMapId);
if (configuration.hasResultMap(resultMapId)) {
@ -734,30 +738,34 @@ public class TableInfo {
}
// <resultMap> 标签下的 <association> 标签映射
if (!withCircularReference && associationType != null) {
if (associationType != null) {
associationType.forEach((fieldName, fieldType) -> {
// 获取嵌套类型的信息也就是 javaType 属性
TableInfo tableInfo = TableInfoFactory.ofEntityClass(fieldType);
// 构建嵌套类型的 ResultMap 对象也就是 <association> 标签下的内容
ResultMap nestedResultMap = tableInfo.doBuildResultMap(configuration, context);
resultMappings.add(new ResultMapping.Builder(configuration, fieldName)
.javaType(fieldType)
.nestedResultMapId(nestedResultMap.getId())
.build());
if (nestedResultMap != null) {
resultMappings.add(new ResultMapping.Builder(configuration, fieldName)
.javaType(fieldType)
.nestedResultMapId(nestedResultMap.getId())
.build());
}
});
}
// <resultMap> 标签下的 <collection> 标签映射
if (!withCircularReference && collectionType != null) {
if (collectionType != null) {
collectionType.forEach((field, genericClass) -> {
// 获取集合泛型类型的信息也就是 ofType 属性
TableInfo tableInfo = TableInfoFactory.ofEntityClass(genericClass);
// 构建嵌套类型的 ResultMap 对象也就是 <collection> 标签下的内容
ResultMap nestedResultMap = tableInfo.doBuildResultMap(configuration, context);
resultMappings.add(new ResultMapping.Builder(configuration, field.getName())
.javaType(field.getType())
.nestedResultMapId(nestedResultMap.getId())
.build());
if (nestedResultMap != null) {
resultMappings.add(new ResultMapping.Builder(configuration, field.getName())
.javaType(field.getType())
.nestedResultMapId(nestedResultMap.getId())
.build());
}
});
}