mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
fix: 重复列获取问题。
This commit is contained in:
parent
bef851d017
commit
ce9fb7a6e7
@ -80,6 +80,7 @@ import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.StringJoiner;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@ -1029,16 +1030,21 @@ public class TableInfo {
|
||||
Stream<Class<?>> ct = collectionType == null ? Stream.empty() : collectionType.values().stream();
|
||||
Stream<Class<?>> at = associationType == null ? Stream.empty() : associationType.values().stream();
|
||||
|
||||
// 预加载所有的列,重复列去重
|
||||
Set<String> existedColumns = Stream.concat(at, ct)
|
||||
// 预加载所有重复列,用于判断重名属性
|
||||
List<String> existedColumns = Stream.concat(at, ct)
|
||||
.map(TableInfoFactory::ofEntityClass)
|
||||
.flatMap(e -> Arrays.stream(e.allColumns))
|
||||
.collect(Collectors.toSet());
|
||||
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
|
||||
.entrySet()
|
||||
.stream()
|
||||
.filter(e -> e.getValue().intValue() > 1)
|
||||
.map(Map.Entry::getKey)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return doBuildResultMap(configuration, new HashSet<>(), existedColumns, false, getTableNameWithSchema());
|
||||
}
|
||||
|
||||
private ResultMap doBuildResultMap(Configuration configuration, Set<String> resultMapIds, Set<String> existedColumns, boolean isNested, String nestedPrefix) {
|
||||
private ResultMap doBuildResultMap(Configuration configuration, Set<String> resultMapIds, List<String> existedColumns, boolean isNested, String nestedPrefix) {
|
||||
|
||||
String resultMapId = isNested ? "nested-" + nestedPrefix + ":" + entityClass.getName() : entityClass.getName();
|
||||
|
||||
@ -1123,42 +1129,35 @@ public class TableInfo {
|
||||
}
|
||||
|
||||
private void doBuildColumnResultMapping(Configuration configuration, List<ResultMapping> resultMappings
|
||||
, Set<String> existedColumns, ColumnInfo columnInfo, List<ResultFlag> flags, boolean isNested) {
|
||||
, List<String> existedColumns, ColumnInfo columnInfo, List<ResultFlag> flags, boolean isNested) {
|
||||
|
||||
if (!isNested) {
|
||||
// userName -> user_name
|
||||
resultMappings.add(new ResultMapping.Builder(configuration
|
||||
, columnInfo.property
|
||||
, columnInfo.column
|
||||
, columnInfo.propertyType)
|
||||
.jdbcType(columnInfo.getJdbcType())
|
||||
.flags(flags)
|
||||
.typeHandler(columnInfo.buildTypeHandler(configuration))
|
||||
.build());
|
||||
}
|
||||
|
||||
if (existedColumns.contains(columnInfo.column)) {
|
||||
// userName -> tb_user$user_name
|
||||
resultMappings.add(new ResultMapping.Builder(configuration
|
||||
, columnInfo.property
|
||||
, tableName + "$" + columnInfo.column
|
||||
, columnInfo.propertyType)
|
||||
.jdbcType(columnInfo.getJdbcType())
|
||||
.flags(flags)
|
||||
.typeHandler(columnInfo.buildTypeHandler(configuration))
|
||||
.build());
|
||||
}
|
||||
|
||||
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 (existedColumns.contains(columnInfo.column)) {
|
||||
// userName -> tb_user$user_name
|
||||
resultMappings.add(new ResultMapping.Builder(configuration
|
||||
, columnInfo.property
|
||||
, tableName + "$" + columnInfo.column
|
||||
, columnInfo.propertyType)
|
||||
.jdbcType(columnInfo.getJdbcType())
|
||||
.flags(flags)
|
||||
.typeHandler(columnInfo.buildTypeHandler(configuration))
|
||||
.build());
|
||||
}
|
||||
buildDefaultResultMapping(configuration, resultMappings, columnInfo, flags);
|
||||
} else {
|
||||
if (existedColumns.contains(columnInfo.column)) {
|
||||
// userName -> tb_user$user_name
|
||||
resultMappings.add(new ResultMapping.Builder(configuration
|
||||
, columnInfo.property
|
||||
, tableName + "$" + columnInfo.column
|
||||
, columnInfo.propertyType)
|
||||
.jdbcType(columnInfo.getJdbcType())
|
||||
.flags(flags)
|
||||
.typeHandler(columnInfo.buildTypeHandler(configuration))
|
||||
.build());
|
||||
} else {
|
||||
buildDefaultResultMapping(configuration, resultMappings, columnInfo, flags);
|
||||
}
|
||||
}
|
||||
|
||||
if (ArrayUtil.isNotEmpty(columnInfo.alias)) {
|
||||
@ -1177,6 +1176,30 @@ public class TableInfo {
|
||||
|
||||
}
|
||||
|
||||
private void buildDefaultResultMapping(Configuration configuration, List<ResultMapping> resultMappings, ColumnInfo columnInfo, List<ResultFlag> flags) {
|
||||
// userName -> user_name
|
||||
resultMappings.add(new ResultMapping.Builder(configuration
|
||||
, columnInfo.property
|
||||
, columnInfo.column
|
||||
, columnInfo.propertyType)
|
||||
.jdbcType(columnInfo.getJdbcType())
|
||||
.flags(flags)
|
||||
.typeHandler(columnInfo.buildTypeHandler(configuration))
|
||||
.build());
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private Object buildColumnSqlArg(MetaObject metaObject, String column) {
|
||||
ColumnInfo columnInfo = columnInfoMapping.get(column);
|
||||
|
||||
@ -63,7 +63,7 @@ class AlisaTest {
|
||||
.select(SYS_USER.DEFAULT_COLUMNS)
|
||||
.select(SYS_ROLE.DEFAULT_COLUMNS)
|
||||
.from(SYS_USER.as("u"))
|
||||
.leftJoin(SYS_ROLE).as("r").on(SYS_USER.ID.eq(SYS_ROLE.ID));
|
||||
.leftJoin(SYS_ROLE).as("r").on(SYS_USER.ID.eq(1));
|
||||
|
||||
printList(queryWrapper);
|
||||
}
|
||||
@ -75,8 +75,8 @@ class AlisaTest {
|
||||
.select(SYS_ROLE.DEFAULT_COLUMNS)
|
||||
.select(SYS_DEPT.DEFAULT_COLUMNS)
|
||||
.from(SYS_USER.as("u"))
|
||||
.leftJoin(SYS_ROLE).as("r").on(SYS_USER.ID.eq(SYS_ROLE.ID))
|
||||
.leftJoin(SYS_DEPT).as("d").on(SYS_USER.ID.eq(SYS_DEPT.ID));
|
||||
.leftJoin(SYS_ROLE).as("r").on(SYS_USER.ID.eq(1))
|
||||
.leftJoin(SYS_DEPT).as("d").on(SYS_USER.ID.eq(1));
|
||||
|
||||
printList(queryWrapper);
|
||||
}
|
||||
@ -89,8 +89,8 @@ class AlisaTest {
|
||||
.select(SYS_DEPT.DEFAULT_COLUMNS)
|
||||
.select(SYS_USER.DEFAULT_COLUMNS)
|
||||
.from(SYS_USER.as("u"))
|
||||
.leftJoin(SYS_ROLE).as("r").on(SYS_USER.ID.eq(SYS_ROLE.ID))
|
||||
.leftJoin(SYS_DEPT).as("d").on(SYS_USER.ID.eq(SYS_DEPT.ID));
|
||||
.leftJoin(SYS_ROLE).as("r").on(SYS_USER.ID.eq(1))
|
||||
.leftJoin(SYS_DEPT).as("d").on(SYS_USER.ID.eq(1));
|
||||
|
||||
printList(queryWrapper);
|
||||
}
|
||||
@ -126,7 +126,7 @@ class AlisaTest {
|
||||
.select(SYS_USER.ID, SYS_USER.USER_NAME, SYS_USER.AGE, SYS_USER.BIRTHDAY)
|
||||
.select(SYS_ROLE.CREATE_BY.as("sys_role$create_by"))
|
||||
.from(SYS_USER.as("u"))
|
||||
.leftJoin(SYS_ROLE).as("r").on(SYS_USER.ID.eq(SYS_ROLE.ID));
|
||||
.leftJoin(SYS_ROLE).as("r").on(SYS_USER.ID.eq(1));
|
||||
|
||||
Object[] objects = FlexGlobalConfig.getDefaultConfig()
|
||||
.getConfiguration()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user