diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/FlexGlobalConfig.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/FlexGlobalConfig.java index 0b0a35b9..0477f66a 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/FlexGlobalConfig.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/FlexGlobalConfig.java @@ -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 getGlobalConfigs() { return globalConfigs; } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/RelationManager.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/RelationManager.java index ab8a63af..2c38fd0d 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/RelationManager.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/relation/RelationManager.java @@ -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, List> classRelations = new ConcurrentHashMap<>(); + /** + * 默认查询深度 + */ + private static int defaultQueryDepth = FlexGlobalConfig.getDefaultConfig().getDefaultRelationQueryDepth(); + /** * 递归查询深度,默认为 2,在一些特殊场景下可以修改这个值 */ - private static ThreadLocal depthThreadLocal = ThreadLocal.withInitial(() -> 2); + private static ThreadLocal depthThreadLocal = ThreadLocal.withInitial(() -> defaultQueryDepth); /** * 附加条件的查询参数 @@ -67,36 +74,12 @@ public class RelationManager { */ private static ThreadLocal autoClearConfig = ThreadLocal.withInitial(() -> true); - - private static List getRelations(Class clazz) { - return MapUtil.computeIfAbsent(classRelations, clazz, RelationManager::doGetRelations); + public static int getDefaultQueryDepth() { + return defaultQueryDepth; } - private static List doGetRelations(Class entityClass) { - List allFields = ClassUtil.getAllFields(entityClass); - List 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 getRelations(Class clazz) { + return MapUtil.computeIfAbsent(classRelations, clazz, RelationManager::doGetRelations); + } + + private static List doGetRelations(Class entityClass) { + List allFields = ClassUtil.getAllFields(entityClass); + List 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 queryRelations(BaseMapper mapper, List entities) { doQueryRelations(mapper, entities, 0, depthThreadLocal.get(), ignoreRelations.get()); diff --git a/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/MybatisFlexProperties.java b/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/MybatisFlexProperties.java index 0f1a3882..7920cefb 100644 --- a/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/MybatisFlexProperties.java +++ b/mybatis-flex-spring-boot-starter/src/main/java/com/mybatisflex/spring/boot/MybatisFlexProperties.java @@ -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); } } diff --git a/mybatis-flex-spring-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/mybatis-flex-spring-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json index a8ab3477..d2603595 100644 --- a/mybatis-flex-spring-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/mybatis-flex-spring-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -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" - }, - { - "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" + }, + { + "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" + } + ] }