Skip to content

Commit f0942e1

Browse files
committed
🎨 修改pageable属性类转换方式
1 parent d29968d commit f0942e1

File tree

3 files changed

+33
-49
lines changed

3 files changed

+33
-49
lines changed

api-boot-project/api-boot-autoconfigure/src/main/java/org/minbox/framework/api/boot/autoconfigure/pageable/ApiBootMyBatisPageableProperties.java

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,15 @@
2222
import org.springframework.boot.context.properties.ConfigurationProperties;
2323
import org.springframework.context.annotation.Configuration;
2424

25-
import java.lang.reflect.Field;
2625
import java.util.Properties;
2726

2827
import static org.minbox.framework.api.boot.autoconfigure.pageable.ApiBootMyBatisPageableProperties.API_BOOT_PAGEABLE_PREFIX;
2928

3029

3130
/**
32-
* mybatis-pageable自动化配置属性
31+
* Mybatis pageable properties
3332
*
34-
* @author:于起宇 ===============================
35-
* Created with IDEA.
36-
* Date:2018/8/4
37-
* Time:2:22 PM
38-
* 简书:http://www.jianshu.com/u/092df3f77bca
39-
* ================================
33+
* @author 恒宇少年
4034
*/
4135
@Data
4236
@Configuration
@@ -47,39 +41,20 @@ public class ApiBootMyBatisPageableProperties {
4741
*/
4842
public static final String API_BOOT_PAGEABLE_PREFIX = "api.boot.pageable";
4943
/**
50-
* 数据库方言
51-
* 默认使用mysql数据库方言
44+
* The database dialect
45+
* <p>
46+
* default use {@link DialectEnum#MYSQL}
5247
*/
5348
private DialectEnum dialect = DialectEnum.MYSQL;
5449

5550
/**
56-
* 获取属性配置
51+
* Convert this instance to {@link Properties}
5752
*
58-
* @return 配置文件对象
53+
* @return The {@link Properties} config instance
5954
*/
60-
public Properties getProperties() {
61-
62-
// 返回的配置对象
55+
public Properties convertProperties() {
6356
Properties properties = new Properties();
64-
/*
65-
* 获取本类内创建的field列表
66-
* 添加到配置对象集合内
67-
*/
68-
Field[] fields = this.getClass().getDeclaredFields();
69-
for (Field field : fields) {
70-
try {
71-
field.setAccessible(true);
72-
// 数据库方言
73-
if ("dialect".equals(field.getName())) {
74-
properties.setProperty(field.getName(), dialect.getValue().getName());
75-
} else {
76-
properties.setProperty(field.getName(), String.valueOf(field.get(this)));
77-
}
78-
} catch (Exception e) {
79-
e.printStackTrace();
80-
}
81-
}
82-
57+
properties.setProperty(PropertiesNames.DATABASE_DIALECT, dialect.getValue().getName());
8358
return properties;
8459
}
8560
}

api-boot-project/api-boot-autoconfigure/src/main/java/org/minbox/framework/api/boot/autoconfigure/pageable/ApiBootMybatisPageableAutoConfiguration.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,12 @@
3131

3232
import javax.annotation.PostConstruct;
3333
import java.util.List;
34+
import java.util.Properties;
3435

3536
/**
3637
* ApiBoot Mybatis Pageable Auto Configuration
3738
*
38-
* @author:恒宇少年 - 于起宇
39-
* <p>
40-
* DateTime:2019-04-25 14:53
41-
* Blog:http://blog.yuqiyu.com
42-
* WebSite:http://www.jianshu.com/u/092df3f77bca
43-
* Gitee:https://gitee.com/hengboy
44-
* GitHub:https://github.com/hengboy
39+
* @author 恒宇少年
4540
*/
4641
@Configuration
4742
@ConditionalOnBean(SqlSessionFactory.class)
@@ -50,9 +45,9 @@
5045
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
5146
public class ApiBootMybatisPageableAutoConfiguration {
5247
/**
53-
* myabtis pageable properties
48+
* mybatis pageable properties
5449
*/
55-
private ApiBootMyBatisPageableProperties myBatisPageableProperties;
50+
private ApiBootMyBatisPageableProperties pageableProperties;
5651
/**
5752
* mybatis sqlSession factory
5853
*/
@@ -65,7 +60,7 @@ public class ApiBootMybatisPageableAutoConfiguration {
6560
public ApiBootMybatisPageableAutoConfiguration(ApiBootMyBatisPageableProperties myBatisPageableProperties,
6661
ObjectProvider<List<SqlSessionFactory>> interceptorsProvider,
6762
ObjectProvider<PageableConfigurer> pageableConfigurerObjectProvider) {
68-
this.myBatisPageableProperties = myBatisPageableProperties;
63+
this.pageableProperties = myBatisPageableProperties;
6964
this.sqlSessionFactoryList = interceptorsProvider.getIfAvailable();
7065
this.pageableConfigurer = pageableConfigurerObjectProvider.getIfAvailable();
7166
}
@@ -77,7 +72,8 @@ public ApiBootMybatisPageableAutoConfiguration(ApiBootMyBatisPageableProperties
7772
void addInterceptors() {
7873
Interceptor interceptor = new MyBatisExecutePageableInterceptor();
7974
// set properties to interceptor
80-
interceptor.setProperties(myBatisPageableProperties.getProperties());
75+
Properties properties = pageableProperties.convertProperties();
76+
interceptor.setProperties(properties);
8177

8278
for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {
8379
// pre
@@ -90,9 +86,9 @@ void addInterceptors() {
9086
}
9187

9288
/**
93-
* pre interceptors
89+
* add pre interceptors
9490
*
95-
* @param sqlSessionFactory sqlSessionFactory对象实例
91+
* @param sqlSessionFactory The {@link SqlSessionFactory} instance
9692
*/
9793
void addPreInterceptors(SqlSessionFactory sqlSessionFactory) {
9894
if (allowPageableConfigurer() && pageableConfigurer.prePlugins() != null) {
@@ -101,9 +97,9 @@ void addPreInterceptors(SqlSessionFactory sqlSessionFactory) {
10197
}
10298

10399
/**
104-
* after interceptors
100+
* add after interceptors
105101
*
106-
* @param sqlSessionFactory sqlSessionFactory对象实例
102+
* @param sqlSessionFactory The {@link SqlSessionFactory} instance
107103
*/
108104
void addPostInterceptors(SqlSessionFactory sqlSessionFactory) {
109105
if (allowPageableConfigurer() && pageableConfigurer.postPlugins() != null) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.minbox.framework.api.boot.autoconfigure.pageable;
2+
3+
/**
4+
* The MyBatis pageable config properties names
5+
*
6+
* @author 恒宇少年
7+
*/
8+
public interface PropertiesNames {
9+
/**
10+
* The name of database dialect properties
11+
*/
12+
String DATABASE_DIALECT = "dialect";
13+
}

0 commit comments

Comments
 (0)