重构 TableInfo.buildResultMap

This commit is contained in:
开源海哥 2023-06-13 13:27:28 +08:00
parent 4cc7fdb4e7
commit 4e2e874c91
3 changed files with 19 additions and 7 deletions

View File

@ -194,7 +194,7 @@ public class FlexConfiguration extends Configuration {
if (hasResultMap(resultMapId)) { if (hasResultMap(resultMapId)) {
resultMap = getResultMap(resultMapId); resultMap = getResultMap(resultMapId);
} else { } else {
resultMap = tableInfo.buildResultMap(this); resultMap = tableInfo.buildResultMap(this, true);
} }
return new MappedStatement.Builder(ms.getConfiguration(), ms.getId(), ms.getSqlSource(), ms.getSqlCommandType()) return new MappedStatement.Builder(ms.getConfiguration(), ms.getId(), ms.getSqlSource(), ms.getSqlCommandType())

View File

@ -707,8 +707,8 @@ public class TableInfo {
} }
public ResultMap buildResultMap(Configuration configuration) { public ResultMap buildResultMap(Configuration configuration, boolean withNested) {
String resultMapId = entityClass.getName(); String resultMapId = entityClass.getName() + (withNested ? "" : "$nested");
if (configuration.hasResultMap(resultMapId)) { if (configuration.hasResultMap(resultMapId)) {
return configuration.getResultMap(resultMapId); return configuration.getResultMap(resultMapId);
} }
@ -746,12 +746,12 @@ public class TableInfo {
} }
// <resultMap> 标签下的 <association> 标签映射 // <resultMap> 标签下的 <association> 标签映射
if (associationType != null) { if (withNested && 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.buildResultMap(configuration); ResultMap nestedResultMap = tableInfo.buildResultMap(configuration, false);
resultMappings.add(new ResultMapping.Builder(configuration, fieldName) resultMappings.add(new ResultMapping.Builder(configuration, fieldName)
.javaType(fieldType) .javaType(fieldType)
.nestedResultMapId(nestedResultMap.getId()) .nestedResultMapId(nestedResultMap.getId())
@ -760,12 +760,12 @@ public class TableInfo {
} }
// <resultMap> 标签下的 <collection> 标签映射 // <resultMap> 标签下的 <collection> 标签映射
if (collectionType != null) { if (withNested && collectionType != null) {
collectionType.forEach((field, genericClass) -> { collectionType.forEach((field, genericClass) -> {
// 获取集合泛型类型的信息也就是 ofType 属性 // 获取集合泛型类型的信息也就是 ofType 属性
TableInfo tableInfo = TableInfoFactory.ofEntityClass(genericClass); TableInfo tableInfo = TableInfoFactory.ofEntityClass(genericClass);
// 构建嵌套类型的 ResultMap 对象也就是 <collection> 标签下的内容 // 构建嵌套类型的 ResultMap 对象也就是 <collection> 标签下的内容
ResultMap nestedResultMap = tableInfo.buildResultMap(configuration); ResultMap nestedResultMap = tableInfo.buildResultMap(configuration, false);
resultMappings.add(new ResultMapping.Builder(configuration, field.getName()) resultMappings.add(new ResultMapping.Builder(configuration, field.getName())
.javaType(field.getType()) .javaType(field.getType())
.nestedResultMapId(nestedResultMap.getId()) .nestedResultMapId(nestedResultMap.getId())

View File

@ -19,6 +19,8 @@ package com.mybatisflex.test.model;
import com.mybatisflex.annotation.Id; import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.Table; import com.mybatisflex.annotation.Table;
import java.util.List;
/** /**
* 角色 * 角色
* *
@ -33,6 +35,8 @@ public class Role implements Comparable<Role> {
private String roleKey; private String roleKey;
private String roleName; private String roleName;
private List<UserVO> userVOS;
public Integer getRoleId() { public Integer getRoleId() {
return roleId; return roleId;
} }
@ -57,6 +61,14 @@ public class Role implements Comparable<Role> {
this.roleName = roleName; this.roleName = roleName;
} }
public List<UserVO> getUserVOS() {
return userVOS;
}
public void setUserVOS(List<UserVO> userVOS) {
this.userVOS = userVOS;
}
@Override @Override
public String toString() { public String toString() {
return "Role{" + return "Role{" +