mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-08 09:38:26 +08:00
feat: 添加 Relation 默认查询深度支持在 spring 的 application.yml 进行配置;close #I7LLRU
This commit is contained in:
parent
9b246e79c6
commit
49c873ede2
@ -79,6 +79,12 @@ public class FlexGlobalConfig {
|
|||||||
*/
|
*/
|
||||||
private int defaultPageSize = 10;
|
private int defaultPageSize = 10;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 默认的 Relation 注解查询深度
|
||||||
|
*/
|
||||||
|
private int defaultRelationQueryDepth = 2;
|
||||||
|
|
||||||
public boolean isPrintBanner() {
|
public boolean isPrintBanner() {
|
||||||
return printBanner;
|
return printBanner;
|
||||||
}
|
}
|
||||||
@ -320,6 +326,14 @@ public class FlexGlobalConfig {
|
|||||||
this.defaultPageSize = defaultPageSize;
|
this.defaultPageSize = defaultPageSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getDefaultRelationQueryDepth() {
|
||||||
|
return defaultRelationQueryDepth;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDefaultRelationQueryDepth(int defaultRelationQueryDepth) {
|
||||||
|
this.defaultRelationQueryDepth = defaultRelationQueryDepth;
|
||||||
|
}
|
||||||
|
|
||||||
public static ConcurrentHashMap<String, FlexGlobalConfig> getGlobalConfigs() {
|
public static ConcurrentHashMap<String, FlexGlobalConfig> getGlobalConfigs() {
|
||||||
return globalConfigs;
|
return globalConfigs;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -21,6 +21,7 @@ import com.mybatisflex.annotation.RelationOneToMany;
|
|||||||
import com.mybatisflex.annotation.RelationOneToOne;
|
import com.mybatisflex.annotation.RelationOneToOne;
|
||||||
import com.mybatisflex.core.BaseMapper;
|
import com.mybatisflex.core.BaseMapper;
|
||||||
import com.mybatisflex.core.FlexConsts;
|
import com.mybatisflex.core.FlexConsts;
|
||||||
|
import com.mybatisflex.core.FlexGlobalConfig;
|
||||||
import com.mybatisflex.core.datasource.DataSourceKey;
|
import com.mybatisflex.core.datasource.DataSourceKey;
|
||||||
import com.mybatisflex.core.query.QueryWrapper;
|
import com.mybatisflex.core.query.QueryWrapper;
|
||||||
import com.mybatisflex.core.row.Row;
|
import com.mybatisflex.core.row.Row;
|
||||||
@ -38,6 +39,7 @@ import static com.mybatisflex.core.query.QueryMethods.column;
|
|||||||
/**
|
/**
|
||||||
* @author michael
|
* @author michael
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings("rawtypes")
|
||||||
public class RelationManager {
|
public class RelationManager {
|
||||||
|
|
||||||
private RelationManager() {
|
private RelationManager() {
|
||||||
@ -45,10 +47,15 @@ public class RelationManager {
|
|||||||
|
|
||||||
private static Map<Class<?>, List<AbstractRelation>> classRelations = new ConcurrentHashMap<>();
|
private static Map<Class<?>, List<AbstractRelation>> classRelations = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 默认查询深度
|
||||||
|
*/
|
||||||
|
private static int defaultQueryDepth = FlexGlobalConfig.getDefaultConfig().getDefaultRelationQueryDepth();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 递归查询深度,默认为 2,在一些特殊场景下可以修改这个值
|
* 递归查询深度,默认为 2,在一些特殊场景下可以修改这个值
|
||||||
*/
|
*/
|
||||||
private static ThreadLocal<Integer> depthThreadLocal = ThreadLocal.withInitial(() -> 2);
|
private static ThreadLocal<Integer> depthThreadLocal = ThreadLocal.withInitial(() -> defaultQueryDepth);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 附加条件的查询参数
|
* 附加条件的查询参数
|
||||||
@ -67,36 +74,12 @@ public class RelationManager {
|
|||||||
*/
|
*/
|
||||||
private static ThreadLocal<Boolean> autoClearConfig = ThreadLocal.withInitial(() -> true);
|
private static ThreadLocal<Boolean> autoClearConfig = ThreadLocal.withInitial(() -> true);
|
||||||
|
|
||||||
|
public static int getDefaultQueryDepth() {
|
||||||
private static List<AbstractRelation> getRelations(Class<?> clazz) {
|
return defaultQueryDepth;
|
||||||
return MapUtil.computeIfAbsent(classRelations, clazz, RelationManager::doGetRelations);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static List<AbstractRelation> doGetRelations(Class<?> entityClass) {
|
public static void setDefaultQueryDepth(int defaultQueryDepth) {
|
||||||
List<Field> allFields = ClassUtil.getAllFields(entityClass);
|
RelationManager.defaultQueryDepth = defaultQueryDepth;
|
||||||
List<AbstractRelation> relations = new ArrayList<>();
|
|
||||||
for (Field field : allFields) {
|
|
||||||
RelationManyToMany manyToManyAnnotation = field.getAnnotation(RelationManyToMany.class);
|
|
||||||
if (manyToManyAnnotation != null) {
|
|
||||||
relations.add(new ManyToMany<>(manyToManyAnnotation, entityClass, field));
|
|
||||||
}
|
|
||||||
|
|
||||||
RelationManyToOne manyToOneAnnotation = field.getAnnotation(RelationManyToOne.class);
|
|
||||||
if (manyToOneAnnotation != null) {
|
|
||||||
relations.add(new ManyToOne<>(manyToOneAnnotation, entityClass, field));
|
|
||||||
}
|
|
||||||
|
|
||||||
RelationOneToMany oneToManyAnnotation = field.getAnnotation(RelationOneToMany.class);
|
|
||||||
if (oneToManyAnnotation != null) {
|
|
||||||
relations.add(new OneToMany<>(oneToManyAnnotation, entityClass, field));
|
|
||||||
}
|
|
||||||
|
|
||||||
RelationOneToOne oneToOneAnnotation = field.getAnnotation(RelationOneToOne.class);
|
|
||||||
if (oneToOneAnnotation != null) {
|
|
||||||
relations.add(new OneToOne<>(oneToOneAnnotation, entityClass, field));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return relations;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setMaxDepth(int maxDepth) {
|
public static void setMaxDepth(int maxDepth) {
|
||||||
@ -187,6 +170,38 @@ public class RelationManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static List<AbstractRelation> getRelations(Class<?> clazz) {
|
||||||
|
return MapUtil.computeIfAbsent(classRelations, clazz, RelationManager::doGetRelations);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<AbstractRelation> doGetRelations(Class<?> entityClass) {
|
||||||
|
List<Field> allFields = ClassUtil.getAllFields(entityClass);
|
||||||
|
List<AbstractRelation> relations = new ArrayList<>();
|
||||||
|
for (Field field : allFields) {
|
||||||
|
RelationManyToMany manyToManyAnnotation = field.getAnnotation(RelationManyToMany.class);
|
||||||
|
if (manyToManyAnnotation != null) {
|
||||||
|
relations.add(new ManyToMany<>(manyToManyAnnotation, entityClass, field));
|
||||||
|
}
|
||||||
|
|
||||||
|
RelationManyToOne manyToOneAnnotation = field.getAnnotation(RelationManyToOne.class);
|
||||||
|
if (manyToOneAnnotation != null) {
|
||||||
|
relations.add(new ManyToOne<>(manyToOneAnnotation, entityClass, field));
|
||||||
|
}
|
||||||
|
|
||||||
|
RelationOneToMany oneToManyAnnotation = field.getAnnotation(RelationOneToMany.class);
|
||||||
|
if (oneToManyAnnotation != null) {
|
||||||
|
relations.add(new OneToMany<>(oneToManyAnnotation, entityClass, field));
|
||||||
|
}
|
||||||
|
|
||||||
|
RelationOneToOne oneToOneAnnotation = field.getAnnotation(RelationOneToOne.class);
|
||||||
|
if (oneToOneAnnotation != null) {
|
||||||
|
relations.add(new OneToOne<>(oneToOneAnnotation, entityClass, field));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return relations;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public static <Entity> void queryRelations(BaseMapper<?> mapper, List<Entity> entities) {
|
public static <Entity> void queryRelations(BaseMapper<?> mapper, List<Entity> entities) {
|
||||||
|
|
||||||
doQueryRelations(mapper, entities, 0, depthThreadLocal.get(), ignoreRelations.get());
|
doQueryRelations(mapper, entities, 0, depthThreadLocal.get(), ignoreRelations.get());
|
||||||
|
|||||||
@ -764,6 +764,18 @@ public class MybatisFlexProperties {
|
|||||||
private Object deletedValueOfLogicDelete = FlexConsts.LOGIC_DELETE_DELETED;
|
private Object deletedValueOfLogicDelete = FlexConsts.LOGIC_DELETE_DELETED;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 默认的分页查询时的每页数据量
|
||||||
|
*/
|
||||||
|
private int defaultPageSize = 10;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 默认的 Relation 注解查询深度
|
||||||
|
*/
|
||||||
|
private int defaultRelationQueryDepth = 2;
|
||||||
|
|
||||||
|
|
||||||
public boolean isPrintBanner() {
|
public boolean isPrintBanner() {
|
||||||
return printBanner;
|
return printBanner;
|
||||||
}
|
}
|
||||||
@ -796,12 +808,30 @@ public class MybatisFlexProperties {
|
|||||||
this.deletedValueOfLogicDelete = deletedValueOfLogicDelete;
|
this.deletedValueOfLogicDelete = deletedValueOfLogicDelete;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getDefaultPageSize() {
|
||||||
|
return defaultPageSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDefaultPageSize(int defaultPageSize) {
|
||||||
|
this.defaultPageSize = defaultPageSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getDefaultRelationQueryDepth() {
|
||||||
|
return defaultRelationQueryDepth;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDefaultRelationQueryDepth(int defaultRelationQueryDepth) {
|
||||||
|
this.defaultRelationQueryDepth = defaultRelationQueryDepth;
|
||||||
|
}
|
||||||
|
|
||||||
void applyTo(FlexGlobalConfig target) {
|
void applyTo(FlexGlobalConfig target) {
|
||||||
PropertyMapper mapper = PropertyMapper.get().alwaysApplyingWhenNonNull();
|
PropertyMapper mapper = PropertyMapper.get().alwaysApplyingWhenNonNull();
|
||||||
mapper.from(isPrintBanner()).to(target::setPrintBanner);
|
mapper.from(isPrintBanner()).to(target::setPrintBanner);
|
||||||
mapper.from(getKeyConfig()).to(target::setKeyConfig);
|
mapper.from(getKeyConfig()).to(target::setKeyConfig);
|
||||||
mapper.from(getNormalValueOfLogicDelete()).to(target::setNormalValueOfLogicDelete);
|
mapper.from(getNormalValueOfLogicDelete()).to(target::setNormalValueOfLogicDelete);
|
||||||
mapper.from(getDeletedValueOfLogicDelete()).to(target::setDeletedValueOfLogicDelete);
|
mapper.from(getDeletedValueOfLogicDelete()).to(target::setDeletedValueOfLogicDelete);
|
||||||
|
mapper.from(getDefaultPageSize()).to(target::setDefaultPageSize);
|
||||||
|
mapper.from(getDefaultRelationQueryDepth()).to(target::setDefaultRelationQueryDepth);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -44,6 +44,18 @@
|
|||||||
"description": "逻辑删除已删除值标记",
|
"description": "逻辑删除已删除值标记",
|
||||||
"type": "java.lang.Object"
|
"type": "java.lang.Object"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"defaultValue": 10,
|
||||||
|
"name": "mybatis-flex.global-config.default-page-size",
|
||||||
|
"description": "默认的分页查询时的每页数据量",
|
||||||
|
"type": "java.lang.Integer"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"defaultValue": 2,
|
||||||
|
"name": "mybatis-flex.global-config.default-relation-query-depth",
|
||||||
|
"description": "默认的 Relation 注解查询深度",
|
||||||
|
"type": "java.lang.Integer"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "mybatis-flex.global-config.key-config.value",
|
"name": "mybatis-flex.global-config.key-config.value",
|
||||||
"description": "使用的 ID 生成器名称 或者 Sequence 执行的 SQL 内容",
|
"description": "使用的 ID 生成器名称 或者 Sequence 执行的 SQL 内容",
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user