feat: Relation Query 方式关联查询构建。

This commit is contained in:
Suomm 2023-07-31 16:14:09 +08:00
parent 8dee20eb3f
commit df8c3264b4

View File

@ -0,0 +1,129 @@
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mybatisflex.core.activerecord.query;
import com.mybatisflex.core.activerecord.Model;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.relation.RelationManager;
import java.util.List;
/**
* 使用 {@code Relations Query} 的方式进行关联查询
*
* @author 王帅
* @since 2023-07-30
*/
public class RelationsQuery<T extends Model<T>> extends AbstractQuery<T> {
public RelationsQuery(Model<T> model) {
super(model);
}
/**
* 忽略查询部分 {@code Relations} 注解标记的属性
*
* @param fields 属性
* @return {@code Relations} 查询构建
*/
public RelationsQuery<T> ignoreRelations(String... fields) {
RelationManager.addIgnoreRelations(fields);
return this;
}
/**
* 设置父子关系查询中默认的递归查询深度
*
* @param maxDepth 查询深度
* @return {@code Relations} 查询构建
*/
public RelationsQuery<T> maxDepth(int maxDepth) {
RelationManager.setMaxDepth(maxDepth);
return this;
}
/**
* 添加额外的 {@code Relations} 查询条件
*
* @param key
* @param value
* @return {@code Relations} 查询构建
*/
public RelationsQuery<T> extraCondition(String key, Object value) {
RelationManager.addExtraConditionParam(key, value);
return this;
}
@Override
public T oneById() {
return baseMapper().selectOneWithRelationsById(pkValues());
}
@Override
public <R> R oneByIdAs(Class<R> asType) {
return baseMapper().selectOneWithRelationsByIdAs(pkValues(), asType);
}
/**
* {@inheritDoc}
*/
@Override
public T one() {
return baseMapper().selectOneWithRelationsByQuery(queryWrapper());
}
/**
* {@inheritDoc}
*/
@Override
public <R> R oneAs(Class<R> asType) {
return baseMapper().selectOneWithRelationsByQueryAs(queryWrapper(), asType);
}
/**
* {@inheritDoc}
*/
@Override
public List<T> list() {
return baseMapper().selectListWithRelationsByQuery(queryWrapper());
}
/**
* {@inheritDoc}
*/
@Override
public <R> List<R> listAs(Class<R> asType) {
return baseMapper().selectListWithRelationsByQueryAs(queryWrapper(), asType);
}
/**
* {@inheritDoc}
*/
@Override
public Page<T> page(Page<T> page) {
return baseMapper().paginateWithRelations(page, queryWrapper());
}
/**
* {@inheritDoc}
*/
@Override
public <R> Page<R> pageAs(Page<R> page, Class<R> asType) {
return baseMapper().paginateWithRelationsAs(page, queryWrapper(), asType);
}
}