mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 17:18:24 +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);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,64 +1,76 @@
|
|||||||
{
|
{
|
||||||
"properties": [
|
"properties": [
|
||||||
{
|
{
|
||||||
"defaultValue": false,
|
"defaultValue": false,
|
||||||
"name": "mybatis-flex.lazy-initialization",
|
"name": "mybatis-flex.lazy-initialization",
|
||||||
"description": "Set whether enable lazy initialization for mapper bean.",
|
"description": "Set whether enable lazy initialization for mapper bean.",
|
||||||
"type": "java.lang.Boolean"
|
"type": "java.lang.Boolean"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"defaultValue": "",
|
"defaultValue": "",
|
||||||
"name": "mybatis-flex.mapper-default-scope",
|
"name": "mybatis-flex.mapper-default-scope",
|
||||||
"description": "A default scope for mapper bean that scanned by auto-configure.",
|
"description": "A default scope for mapper bean that scanned by auto-configure.",
|
||||||
"type": "java.lang.String"
|
"type": "java.lang.String"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"defaultValue": true,
|
"defaultValue": true,
|
||||||
"name": "mybatis-flex.inject-sql-session-on-mapper-scan",
|
"name": "mybatis-flex.inject-sql-session-on-mapper-scan",
|
||||||
"description": "Set whether inject a SqlSessionTemplate or SqlSessionFactory bean (If you want to back to the behavior of 2.2.1 or before, specify false). If you use together with spring-native, should be set true.",
|
"description": "Set whether inject a SqlSessionTemplate or SqlSessionFactory bean (If you want to back to the behavior of 2.2.1 or before, specify false). If you use together with spring-native, should be set true.",
|
||||||
"type": "java.lang.Boolean"
|
"type": "java.lang.Boolean"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "mybatis-flex.scripting-language-driver.velocity.userdirective",
|
"name": "mybatis-flex.scripting-language-driver.velocity.userdirective",
|
||||||
"deprecation": {
|
"deprecation": {
|
||||||
"level": "error",
|
"level": "error",
|
||||||
"reason": "The 'userdirective' is deprecated since Velocity 2.x. This property defined for keeping backward compatibility with older velocity version.",
|
"reason": "The 'userdirective' is deprecated since Velocity 2.x. This property defined for keeping backward compatibility with older velocity version.",
|
||||||
"replacement": "mybatis-flex.scripting-language-driver.velocity.velocity-settings.runtime.custom_directives"
|
"replacement": "mybatis-flex.scripting-language-driver.velocity.velocity-settings.runtime.custom_directives"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"defaultValue": true,
|
"defaultValue": true,
|
||||||
"name": "mybatis-flex.datasource",
|
"name": "mybatis-flex.datasource",
|
||||||
"description": "多数据源配置",
|
"description": "多数据源配置",
|
||||||
"type": "java.util.Map<java.lang.String,com.mybatisflex.spring.boot.DataSourceProperty>"
|
"type": "java.util.Map<java.lang.String,com.mybatisflex.spring.boot.DataSourceProperty>"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"defaultValue": 0,
|
"defaultValue": 0,
|
||||||
"name": "mybatis-flex.global-config.normal-value-of-logic-delete",
|
"name": "mybatis-flex.global-config.normal-value-of-logic-delete",
|
||||||
"description": "逻辑删除未删除值标记",
|
"description": "逻辑删除未删除值标记",
|
||||||
"type": "java.lang.Object"
|
"type": "java.lang.Object"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"defaultValue": 1,
|
"defaultValue": 1,
|
||||||
"name": "mybatis-flex.global-config.deleted-value-of-logic-delete",
|
"name": "mybatis-flex.global-config.deleted-value-of-logic-delete",
|
||||||
"description": "逻辑删除已删除值标记",
|
"description": "逻辑删除已删除值标记",
|
||||||
"type": "java.lang.Object"
|
"type": "java.lang.Object"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "mybatis-flex.global-config.key-config.value",
|
"defaultValue": 10,
|
||||||
"description": "使用的 ID 生成器名称 或者 Sequence 执行的 SQL 内容",
|
"name": "mybatis-flex.global-config.default-page-size",
|
||||||
"type": "java.lang.String"
|
"description": "默认的分页查询时的每页数据量",
|
||||||
},
|
"type": "java.lang.Integer"
|
||||||
{
|
},
|
||||||
"defaultValue": true,
|
{
|
||||||
"name": "mybatis-flex.global-config.key-config.before",
|
"defaultValue": 2,
|
||||||
"description": "是否在数据插入之前执行,只在非自增上配置有效",
|
"name": "mybatis-flex.global-config.default-relation-query-depth",
|
||||||
"type": "java.lang.Boolean"
|
"description": "默认的 Relation 注解查询深度",
|
||||||
},
|
"type": "java.lang.Integer"
|
||||||
{
|
},
|
||||||
"name": "mybatis-flex.global-config.key-config.key-type",
|
{
|
||||||
"description": "ID 生成策略",
|
"name": "mybatis-flex.global-config.key-config.value",
|
||||||
"type": "com.mybatisflex.annotation.KeyType"
|
"description": "使用的 ID 生成器名称 或者 Sequence 执行的 SQL 内容",
|
||||||
}
|
"type": "java.lang.String"
|
||||||
]
|
},
|
||||||
|
{
|
||||||
|
"defaultValue": true,
|
||||||
|
"name": "mybatis-flex.global-config.key-config.before",
|
||||||
|
"description": "是否在数据插入之前执行,只在非自增上配置有效",
|
||||||
|
"type": "java.lang.Boolean"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "mybatis-flex.global-config.key-config.key-type",
|
||||||
|
"description": "ID 生成策略",
|
||||||
|
"type": "com.mybatisflex.annotation.KeyType"
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user