mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-08 09:38:26 +08:00
feat: Relation 注解添加支持递归查询的功能
This commit is contained in:
parent
d88aeb7cc5
commit
767eaf293d
@ -68,7 +68,6 @@ public @interface RelationManyToMany {
|
|||||||
*/
|
*/
|
||||||
String orderBy() default "";
|
String orderBy() default "";
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 默认使用哪个数据源,若系统找不到该指定的数据源时,默认使用第一个数据源。
|
* 默认使用哪个数据源,若系统找不到该指定的数据源时,默认使用第一个数据源。
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -32,7 +32,6 @@ public @interface RelationManyToOne {
|
|||||||
*/
|
*/
|
||||||
String selfField();
|
String selfField();
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 目标对象的关联属性
|
* 目标对象的关联属性
|
||||||
*
|
*
|
||||||
|
|||||||
@ -28,9 +28,7 @@ import com.mybatisflex.core.util.StringUtil;
|
|||||||
import org.apache.ibatis.util.MapUtil;
|
import org.apache.ibatis.util.MapUtil;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -72,20 +70,27 @@ public class RelationManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
|
||||||
public static <Entity> void queryRelations(BaseMapper<?> mapper, List<Entity> entities) {
|
public static <Entity> void queryRelations(BaseMapper<?> mapper, List<Entity> entities) {
|
||||||
|
doQueryRelations(mapper, entities, new HashSet<>());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||||
|
private static <Entity> void doQueryRelations(BaseMapper<?> mapper, List<Entity> entities, Set<Class<?>> queriedClass) {
|
||||||
if (CollectionUtil.isEmpty(entities)) {
|
if (CollectionUtil.isEmpty(entities)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Class<Entity> objectClass = (Class<Entity>) entities.get(0).getClass();
|
Class<Entity> objectClass = (Class<Entity>) entities.get(0).getClass();
|
||||||
|
if (queriedClass.contains(objectClass)) {
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
queriedClass.add(objectClass);
|
||||||
|
}
|
||||||
List<AbstractRelation> relations = getRelations(objectClass);
|
List<AbstractRelation> relations = getRelations(objectClass);
|
||||||
if (relations.isEmpty()) {
|
if (relations.isEmpty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
String currentDsKey = DataSourceKey.get();
|
String currentDsKey = DataSourceKey.get();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
relations.forEach(relation -> {
|
relations.forEach(relation -> {
|
||||||
|
|
||||||
@ -101,7 +106,10 @@ public class RelationManager {
|
|||||||
if (StringUtil.isNotBlank(dataSource)) {
|
if (StringUtil.isNotBlank(dataSource)) {
|
||||||
DataSourceKey.use(dataSource);
|
DataSourceKey.use(dataSource);
|
||||||
}
|
}
|
||||||
|
|
||||||
List<?> targetObjectList = mapper.selectListByQueryAs(queryWrapper, mappingType);
|
List<?> targetObjectList = mapper.selectListByQueryAs(queryWrapper, mappingType);
|
||||||
|
doQueryRelations(mapper, targetObjectList, queriedClass);
|
||||||
|
|
||||||
if (CollectionUtil.isNotEmpty(targetObjectList)) {
|
if (CollectionUtil.isNotEmpty(targetObjectList)) {
|
||||||
relation.join(entities, targetObjectList, mapper);
|
relation.join(entities, targetObjectList, mapper);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user