Michael Yang 2025-01-10 09:17:36 +08:00
parent c88155de85
commit 39b0efbd6a

View File

@ -309,98 +309,87 @@ public class RelationManager {
return; return;
} }
String currentDsKey = DataSourceKey.get(); relations.forEach(relation -> {
try {
relations.forEach(relation -> {
//ignore //ignore
if (ignoreRelations != null && (ignoreRelations.contains(relation.getSimpleName()) if (ignoreRelations != null && (ignoreRelations.contains(relation.getSimpleName())
|| ignoreRelations.contains(relation.getName()))) { || ignoreRelations.contains(relation.getName()))) {
return; return;
} }
//only query //only query
if (queryRelations != null && !queryRelations.isEmpty() if (queryRelations != null && !queryRelations.isEmpty()
&& !queryRelations.contains(relation.getSimpleName()) && !queryRelations.contains(relation.getSimpleName())
&& !queryRelations.contains(relation.getName())) { && !queryRelations.contains(relation.getName())) {
return; return;
} }
//注解配置的数据源 //注解配置的数据源
String configDsKey = relation.getDataSource(); String relationDsKey = relation.getDataSource();
if (StringUtil.noText(configDsKey) && currentDsKey != null) { if (StringUtil.hasText(relationDsKey)) {
configDsKey = currentDsKey; DataSourceKey.use(relationDsKey);
} }
try { try {
if (StringUtil.hasText(configDsKey)) { Set<Object> targetValues;
DataSourceKey.use(configDsKey); List<Row> mappingRows = null;
//通过中间表关联查询
if (relation.isRelationByMiddleTable()) {
Set selfFieldValues = relation.getSelfFieldValues(entities);
// 当数据对应的字段没有值的情况下直接返回
if (selfFieldValues.isEmpty()) {
return;
}
QueryWrapper queryWrapper = QueryWrapper.create().select()
.from(relation.getJoinTable());
if (selfFieldValues.size() > 1) {
queryWrapper.where(column(relation.getJoinSelfColumn()).in(selfFieldValues));
} else {
queryWrapper.where(column(relation.getJoinSelfColumn()).eq(selfFieldValues.iterator().next()));
} }
Set<Object> targetValues; mappingRows = mapper.selectRowsByQuery(queryWrapper);
List<Row> mappingRows = null; if (CollectionUtil.isEmpty(mappingRows)) {
//通过中间表关联查询
if (relation.isRelationByMiddleTable()) {
Set selfFieldValues = relation.getSelfFieldValues(entities);
// 当数据对应的字段没有值的情况下直接返回
if (selfFieldValues.isEmpty()) {
return;
}
QueryWrapper queryWrapper = QueryWrapper.create().select()
.from(relation.getJoinTable());
if (selfFieldValues.size() > 1) {
queryWrapper.where(column(relation.getJoinSelfColumn()).in(selfFieldValues));
} else {
queryWrapper.where(column(relation.getJoinSelfColumn()).eq(selfFieldValues.iterator().next()));
}
mappingRows = mapper.selectRowsByQuery(queryWrapper);
if (CollectionUtil.isEmpty(mappingRows)) {
return;
}
targetValues = new HashSet<>();
for (Row mappingData : mappingRows) {
Object targetValue = mappingData.getIgnoreCase(relation.getJoinTargetColumn());
if (targetValue != null) {
targetValues.add(targetValue);
}
}
}
//通过外键字段关联查询
else {
targetValues = relation.getSelfFieldValues(entities);
}
if (CollectionUtil.isEmpty(targetValues)) {
return; return;
} }
//仅绑定字段:As目标实体类 不进行字段绑定:As映射类型 targetValues = new HashSet<>();
QueryWrapper queryWrapper = relation.buildQueryWrapper(targetValues);
List<?> targetObjectList = mapper.selectListByQueryAs(queryWrapper, relation.isOnlyQueryValueField() ? relation.getTargetEntityClass() : relation.getMappingType());
if (CollectionUtil.isNotEmpty(targetObjectList)) {
//递归查询 for (Row mappingData : mappingRows) {
doQueryRelations(mapper, targetObjectList, currentDepth + 1, maxDepth, ignoreRelations, queryRelations); Object targetValue = mappingData.getIgnoreCase(relation.getJoinTargetColumn());
if (targetValue != null) {
//进行内存 join targetValues.add(targetValue);
relation.join(entities, targetObjectList, mappingRows); }
}
} finally {
if (StringUtil.hasText(configDsKey)) {
DataSourceKey.clear();
} }
} }
}); //通过外键字段关联查询
} finally { else {
if (currentDsKey != null) { targetValues = relation.getSelfFieldValues(entities);
DataSourceKey.use(currentDsKey); }
if (CollectionUtil.isEmpty(targetValues)) {
return;
}
//仅绑定字段:As目标实体类 不进行字段绑定:As映射类型
QueryWrapper queryWrapper = relation.buildQueryWrapper(targetValues);
List<?> targetObjectList = mapper.selectListByQueryAs(queryWrapper, relation.isOnlyQueryValueField() ? relation.getTargetEntityClass() : relation.getMappingType());
if (CollectionUtil.isNotEmpty(targetObjectList)) {
//递归查询
doQueryRelations(mapper, targetObjectList, currentDepth + 1, maxDepth, ignoreRelations, queryRelations);
//进行内存 join
relation.join(entities, targetObjectList, mappingRows);
}
} finally {
if (StringUtil.hasText(relationDsKey)) {
DataSourceKey.clear();
}
} }
} });
} }
} }