mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
feat: Relation 注解添加支持递归查询的功能
This commit is contained in:
parent
8b3a2cdaf2
commit
6bc0e88338
@ -215,6 +215,7 @@ abstract class AbstractRelation<SelfEntity> {
|
||||
* @param selfEntities 当前的实体类列表
|
||||
* @param targetObjectList 查询到的结果
|
||||
* @param mapper 查询的 Mapper
|
||||
* @param queriedClasses
|
||||
*/
|
||||
public abstract void join(List<SelfEntity> selfEntities, List<?> targetObjectList, BaseMapper<?> mapper);
|
||||
public abstract void join(List<SelfEntity> selfEntities, List<?> targetObjectList, BaseMapper<?> mapper, Set<Class<?>> queriedClasses);
|
||||
}
|
||||
|
||||
@ -77,7 +77,7 @@ class ManyToMany<SelfEntity> extends AbstractRelation<SelfEntity> {
|
||||
|
||||
|
||||
@Override
|
||||
public void join(List<SelfEntity> selfEntities, List<?> mappingObjectList, BaseMapper<?> mapper) {
|
||||
public void join(List<SelfEntity> selfEntities, List<?> mappingObjectList, BaseMapper<?> mapper, Set<Class<?>> queriedClasses) {
|
||||
List<Row> mappingRows = (List<Row>) mappingObjectList;
|
||||
Set<Object> targetValues = new LinkedHashSet<>();
|
||||
for (Row row : mappingRows) {
|
||||
@ -106,6 +106,8 @@ class ManyToMany<SelfEntity> extends AbstractRelation<SelfEntity> {
|
||||
|
||||
List<?> targetObjectList = mapper.selectListByQueryAs(queryWrapper, relationFieldWrapper.getMappingType());
|
||||
|
||||
RelationManager.doQueryRelations(mapper, targetObjectList, queriedClasses);
|
||||
|
||||
if (CollectionUtil.isNotEmpty(targetObjectList)) {
|
||||
selfEntities.forEach(selfEntity -> {
|
||||
Object selfValue = selfFieldWrapper.get(selfEntity);
|
||||
|
||||
@ -71,7 +71,7 @@ class OneToMany<SelfEntity> extends AbstractRelation<SelfEntity> {
|
||||
|
||||
|
||||
@Override
|
||||
public void join(List<SelfEntity> selfEntities, List<?> targetObjectList, BaseMapper<?> mapper) {
|
||||
public void join(List<SelfEntity> selfEntities, List<?> targetObjectList, BaseMapper<?> mapper, Set<Class<?>> queriedClasses) {
|
||||
selfEntities.forEach(selfEntity -> {
|
||||
Object selfValue = selfFieldWrapper.get(selfEntity);
|
||||
if (selfValue != null) {
|
||||
|
||||
@ -22,6 +22,7 @@ import com.mybatisflex.annotation.RelationOneToOne;
|
||||
import com.mybatisflex.core.BaseMapper;
|
||||
import com.mybatisflex.core.datasource.DataSourceKey;
|
||||
import com.mybatisflex.core.query.QueryWrapper;
|
||||
import com.mybatisflex.core.row.Row;
|
||||
import com.mybatisflex.core.util.ClassUtil;
|
||||
import com.mybatisflex.core.util.CollectionUtil;
|
||||
import com.mybatisflex.core.util.StringUtil;
|
||||
@ -79,15 +80,15 @@ public class RelationManager {
|
||||
|
||||
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
private static <Entity> void doQueryRelations(BaseMapper<?> mapper, List<Entity> entities, Set<Class<?>> queriedClass) {
|
||||
static <Entity> void doQueryRelations(BaseMapper<?> mapper, List<Entity> entities, Set<Class<?>> queriedClasses) {
|
||||
if (CollectionUtil.isEmpty(entities)) {
|
||||
return;
|
||||
}
|
||||
Class<Entity> objectClass = (Class<Entity>) entities.get(0).getClass();
|
||||
if (queriedClass.contains(objectClass)) {
|
||||
if (queriedClasses.contains(objectClass)) {
|
||||
return;
|
||||
} else {
|
||||
queriedClass.add(objectClass);
|
||||
queriedClasses.add(objectClass);
|
||||
}
|
||||
List<AbstractRelation> relations = getRelations(objectClass);
|
||||
if (relations.isEmpty()) {
|
||||
@ -111,10 +112,12 @@ public class RelationManager {
|
||||
}
|
||||
|
||||
List<?> targetObjectList = mapper.selectListByQueryAs(queryWrapper, mappingType);
|
||||
doQueryRelations(mapper, targetObjectList, queriedClass);
|
||||
if (mappingType != Row.class) {
|
||||
doQueryRelations(mapper, targetObjectList, queriedClasses);
|
||||
}
|
||||
|
||||
if (CollectionUtil.isNotEmpty(targetObjectList)) {
|
||||
relation.join(entities, targetObjectList, mapper);
|
||||
relation.join(entities, targetObjectList, mapper, queriedClasses);
|
||||
}
|
||||
} finally {
|
||||
if (StringUtil.isNotBlank(dataSource)) {
|
||||
|
||||
@ -51,7 +51,7 @@ class ToOneRelation<SelfEntity> extends AbstractRelation<SelfEntity> {
|
||||
|
||||
|
||||
@Override
|
||||
public void join(List<SelfEntity> selfEntities, List<?> targetObjectList, BaseMapper<?> mapper) {
|
||||
public void join(List<SelfEntity> selfEntities, List<?> targetObjectList, BaseMapper<?> mapper, Set<Class<?>> queriedClasses) {
|
||||
selfEntities.forEach(selfEntity -> {
|
||||
Object selfValue = selfFieldWrapper.get(selfEntity);
|
||||
if (selfValue != null) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user