mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
refactor: 重构 Relation 注解查询构建。
This commit is contained in:
parent
b6f3553c3f
commit
d7131f48e7
@ -17,10 +17,7 @@
|
||||
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;
|
||||
import com.mybatisflex.core.query.RelationsBuilder;
|
||||
|
||||
/**
|
||||
* 使用 {@code Relations Query} 的方式进行关联查询。
|
||||
@ -28,102 +25,53 @@ import java.util.List;
|
||||
* @author 王帅
|
||||
* @since 2023-07-30
|
||||
*/
|
||||
public class RelationsQuery<T extends Model<T>> extends AbstractQuery<T> {
|
||||
public class RelationsQuery<T extends Model<T>> extends RelationsBuilder<T> {
|
||||
|
||||
public RelationsQuery(Model<T> model) {
|
||||
super(model);
|
||||
}
|
||||
|
||||
/**
|
||||
* 忽略查询部分 {@code Relations} 注解标记的属性。
|
||||
*
|
||||
* @param fields 属性
|
||||
* @return {@code Relations} 查询构建
|
||||
*/
|
||||
@Override
|
||||
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> extraConditionParam(String key, Object value) {
|
||||
RelationManager.addExtraConditionParam(key, value);
|
||||
super.ignoreRelations(fields);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RelationsQuery<T> maxDepth(int maxDepth) {
|
||||
super.maxDepth(maxDepth);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public RelationsQuery<T> extraConditionParam(String key, Object value) {
|
||||
super.extraConditionParam(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
protected Object[] pkValues() {
|
||||
// 懒加载,实际用到的时候才会生成 主键值
|
||||
return ((Model<T>) delegate).pkValues();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据主键查询一条数据。
|
||||
*
|
||||
* @return 一条数据
|
||||
*/
|
||||
public T oneById() {
|
||||
return baseMapper().selectOneWithRelationsById(pkValues());
|
||||
}
|
||||
|
||||
@Override
|
||||
/**
|
||||
* 根据主键查询一条数据,返回的数据为 asType 类型。
|
||||
*
|
||||
* @param asType 接收数据类型
|
||||
* @param <R> 接收数据类型
|
||||
* @return 一条数据
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user