fix: 重复列获取问题。

This commit is contained in:
Suomm 2024-01-21 21:27:27 +08:00
parent bef851d017
commit ce9fb7a6e7
2 changed files with 67 additions and 44 deletions

View File

@ -80,6 +80,7 @@ import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.StringJoiner; import java.util.StringJoiner;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -1029,16 +1030,21 @@ public class TableInfo {
Stream<Class<?>> ct = collectionType == null ? Stream.empty() : collectionType.values().stream(); Stream<Class<?>> ct = collectionType == null ? Stream.empty() : collectionType.values().stream();
Stream<Class<?>> at = associationType == null ? Stream.empty() : associationType.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) .map(TableInfoFactory::ofEntityClass)
.flatMap(e -> Arrays.stream(e.allColumns)) .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()); 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(); String resultMapId = isNested ? "nested-" + nestedPrefix + ":" + entityClass.getName() : entityClass.getName();
@ -1123,42 +1129,35 @@ public class TableInfo {
} }
private void doBuildColumnResultMapping(Configuration configuration, List<ResultMapping> resultMappings 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) { if (!isNested) {
// userName -> user_name if (existedColumns.contains(columnInfo.column)) {
resultMappings.add(new ResultMapping.Builder(configuration // userName -> tb_user$user_name
, columnInfo.property resultMappings.add(new ResultMapping.Builder(configuration
, columnInfo.column , columnInfo.property
, columnInfo.propertyType) , tableName + "$" + columnInfo.column
.jdbcType(columnInfo.getJdbcType()) , columnInfo.propertyType)
.flags(flags) .jdbcType(columnInfo.getJdbcType())
.typeHandler(columnInfo.buildTypeHandler(configuration)) .flags(flags)
.build()); .typeHandler(columnInfo.buildTypeHandler(configuration))
} .build());
}
if (existedColumns.contains(columnInfo.column)) { buildDefaultResultMapping(configuration, resultMappings, columnInfo, flags);
// userName -> tb_user$user_name } else {
resultMappings.add(new ResultMapping.Builder(configuration if (existedColumns.contains(columnInfo.column)) {
, columnInfo.property // userName -> tb_user$user_name
, tableName + "$" + columnInfo.column resultMappings.add(new ResultMapping.Builder(configuration
, columnInfo.propertyType) , columnInfo.property
.jdbcType(columnInfo.getJdbcType()) , tableName + "$" + columnInfo.column
.flags(flags) , columnInfo.propertyType)
.typeHandler(columnInfo.buildTypeHandler(configuration)) .jdbcType(columnInfo.getJdbcType())
.build()); .flags(flags)
} .typeHandler(columnInfo.buildTypeHandler(configuration))
.build());
if (!Objects.equals(columnInfo.column, columnInfo.property)) { } else {
// userName -> userName buildDefaultResultMapping(configuration, resultMappings, columnInfo, flags);
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)) { 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) { private Object buildColumnSqlArg(MetaObject metaObject, String column) {
ColumnInfo columnInfo = columnInfoMapping.get(column); ColumnInfo columnInfo = columnInfoMapping.get(column);

View File

@ -63,7 +63,7 @@ class AlisaTest {
.select(SYS_USER.DEFAULT_COLUMNS) .select(SYS_USER.DEFAULT_COLUMNS)
.select(SYS_ROLE.DEFAULT_COLUMNS) .select(SYS_ROLE.DEFAULT_COLUMNS)
.from(SYS_USER.as("u")) .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); printList(queryWrapper);
} }
@ -75,8 +75,8 @@ class AlisaTest {
.select(SYS_ROLE.DEFAULT_COLUMNS) .select(SYS_ROLE.DEFAULT_COLUMNS)
.select(SYS_DEPT.DEFAULT_COLUMNS) .select(SYS_DEPT.DEFAULT_COLUMNS)
.from(SYS_USER.as("u")) .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))
.leftJoin(SYS_DEPT).as("d").on(SYS_USER.ID.eq(SYS_DEPT.ID)); .leftJoin(SYS_DEPT).as("d").on(SYS_USER.ID.eq(1));
printList(queryWrapper); printList(queryWrapper);
} }
@ -89,8 +89,8 @@ class AlisaTest {
.select(SYS_DEPT.DEFAULT_COLUMNS) .select(SYS_DEPT.DEFAULT_COLUMNS)
.select(SYS_USER.DEFAULT_COLUMNS) .select(SYS_USER.DEFAULT_COLUMNS)
.from(SYS_USER.as("u")) .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))
.leftJoin(SYS_DEPT).as("d").on(SYS_USER.ID.eq(SYS_DEPT.ID)); .leftJoin(SYS_DEPT).as("d").on(SYS_USER.ID.eq(1));
printList(queryWrapper); printList(queryWrapper);
} }
@ -126,7 +126,7 @@ class AlisaTest {
.select(SYS_USER.ID, SYS_USER.USER_NAME, SYS_USER.AGE, SYS_USER.BIRTHDAY) .select(SYS_USER.ID, SYS_USER.USER_NAME, SYS_USER.AGE, SYS_USER.BIRTHDAY)
.select(SYS_ROLE.CREATE_BY.as("sys_role$create_by")) .select(SYS_ROLE.CREATE_BY.as("sys_role$create_by"))
.from(SYS_USER.as("u")) .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() Object[] objects = FlexGlobalConfig.getDefaultConfig()
.getConfiguration() .getConfiguration()