feat: 添加 Relation 默认查询深度支持在 spring 的 application.yml 进行配置;close #I7LLRU

This commit is contained in:
开源海哥 2023-07-18 10:06:50 +08:00
parent 9b246e79c6
commit 49c873ede2
4 changed files with 162 additions and 91 deletions

View File

@ -79,6 +79,12 @@ public class FlexGlobalConfig {
*/
private int defaultPageSize = 10;
/**
* 默认的 Relation 注解查询深度
*/
private int defaultRelationQueryDepth = 2;
public boolean isPrintBanner() {
return printBanner;
}
@ -320,6 +326,14 @@ public class FlexGlobalConfig {
this.defaultPageSize = defaultPageSize;
}
public int getDefaultRelationQueryDepth() {
return defaultRelationQueryDepth;
}
public void setDefaultRelationQueryDepth(int defaultRelationQueryDepth) {
this.defaultRelationQueryDepth = defaultRelationQueryDepth;
}
public static ConcurrentHashMap<String, FlexGlobalConfig> getGlobalConfigs() {
return globalConfigs;
}

View File

@ -21,6 +21,7 @@ import com.mybatisflex.annotation.RelationOneToMany;
import com.mybatisflex.annotation.RelationOneToOne;
import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.FlexConsts;
import com.mybatisflex.core.FlexGlobalConfig;
import com.mybatisflex.core.datasource.DataSourceKey;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.row.Row;
@ -38,6 +39,7 @@ import static com.mybatisflex.core.query.QueryMethods.column;
/**
* @author michael
*/
@SuppressWarnings("rawtypes")
public class RelationManager {
private RelationManager() {
@ -45,10 +47,15 @@ public class RelationManager {
private static Map<Class<?>, List<AbstractRelation>> classRelations = new ConcurrentHashMap<>();
/**
* 默认查询深度
*/
private static int defaultQueryDepth = FlexGlobalConfig.getDefaultConfig().getDefaultRelationQueryDepth();
/**
* 递归查询深度默认为 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 List<AbstractRelation> getRelations(Class<?> clazz) {
return MapUtil.computeIfAbsent(classRelations, clazz, RelationManager::doGetRelations);
public static int getDefaultQueryDepth() {
return defaultQueryDepth;
}
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 void setDefaultQueryDepth(int defaultQueryDepth) {
RelationManager.defaultQueryDepth = defaultQueryDepth;
}
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) {
doQueryRelations(mapper, entities, 0, depthThreadLocal.get(), ignoreRelations.get());

View File

@ -764,6 +764,18 @@ public class MybatisFlexProperties {
private Object deletedValueOfLogicDelete = FlexConsts.LOGIC_DELETE_DELETED;
/**
* 默认的分页查询时的每页数据量
*/
private int defaultPageSize = 10;
/**
* 默认的 Relation 注解查询深度
*/
private int defaultRelationQueryDepth = 2;
public boolean isPrintBanner() {
return printBanner;
}
@ -796,12 +808,30 @@ public class MybatisFlexProperties {
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) {
PropertyMapper mapper = PropertyMapper.get().alwaysApplyingWhenNonNull();
mapper.from(isPrintBanner()).to(target::setPrintBanner);
mapper.from(getKeyConfig()).to(target::setKeyConfig);
mapper.from(getNormalValueOfLogicDelete()).to(target::setNormalValueOfLogicDelete);
mapper.from(getDeletedValueOfLogicDelete()).to(target::setDeletedValueOfLogicDelete);
mapper.from(getDefaultPageSize()).to(target::setDefaultPageSize);
mapper.from(getDefaultRelationQueryDepth()).to(target::setDefaultRelationQueryDepth);
}
}

View File

@ -1,64 +1,76 @@
{
"properties": [
{
"defaultValue": false,
"name": "mybatis-flex.lazy-initialization",
"description": "Set whether enable lazy initialization for mapper bean.",
"type": "java.lang.Boolean"
},
{
"defaultValue": "",
"name": "mybatis-flex.mapper-default-scope",
"description": "A default scope for mapper bean that scanned by auto-configure.",
"type": "java.lang.String"
},
{
"defaultValue": true,
"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.",
"type": "java.lang.Boolean"
},
{
"name": "mybatis-flex.scripting-language-driver.velocity.userdirective",
"deprecation": {
"level": "error",
"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"
}
},
{
"defaultValue": true,
"name": "mybatis-flex.datasource",
"description": "多数据源配置",
"type": "java.util.Map<java.lang.String,com.mybatisflex.spring.boot.DataSourceProperty>"
},
{
"defaultValue": 0,
"name": "mybatis-flex.global-config.normal-value-of-logic-delete",
"description": "逻辑删除未删除值标记",
"type": "java.lang.Object"
},
{
"defaultValue": 1,
"name": "mybatis-flex.global-config.deleted-value-of-logic-delete",
"description": "逻辑删除已删除值标记",
"type": "java.lang.Object"
},
{
"name": "mybatis-flex.global-config.key-config.value",
"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"
}
]
"properties": [
{
"defaultValue": false,
"name": "mybatis-flex.lazy-initialization",
"description": "Set whether enable lazy initialization for mapper bean.",
"type": "java.lang.Boolean"
},
{
"defaultValue": "",
"name": "mybatis-flex.mapper-default-scope",
"description": "A default scope for mapper bean that scanned by auto-configure.",
"type": "java.lang.String"
},
{
"defaultValue": true,
"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.",
"type": "java.lang.Boolean"
},
{
"name": "mybatis-flex.scripting-language-driver.velocity.userdirective",
"deprecation": {
"level": "error",
"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"
}
},
{
"defaultValue": true,
"name": "mybatis-flex.datasource",
"description": "多数据源配置",
"type": "java.util.Map<java.lang.String,com.mybatisflex.spring.boot.DataSourceProperty>"
},
{
"defaultValue": 0,
"name": "mybatis-flex.global-config.normal-value-of-logic-delete",
"description": "逻辑删除未删除值标记",
"type": "java.lang.Object"
},
{
"defaultValue": 1,
"name": "mybatis-flex.global-config.deleted-value-of-logic-delete",
"description": "逻辑删除已删除值标记",
"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",
"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"
}
]
}