feat: Relation 注解添加支持递归查询的功能

This commit is contained in:
开源海哥 2023-07-12 12:54:17 +08:00
parent 8b3a2cdaf2
commit 6bc0e88338
5 changed files with 15 additions and 9 deletions

View File

@ -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);
}

View File

@ -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);

View File

@ -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) {

View File

@ -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)) {

View File

@ -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) {