Skip to content

Commit 978e4ab

Browse files
committed
ApiBoot Mybatis Pageable 自动化配置
1 parent 7263561 commit 978e4ab

File tree

4 files changed

+222
-2
lines changed

4 files changed

+222
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright [2019] [恒宇少年 - 于起宇]
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package org.minbox.framework.api.boot.autoconfigure.pageable;
19+
20+
import com.gitee.hengboy.mybatis.pageable.common.enums.DialectEnum;
21+
import lombok.Data;
22+
import org.springframework.boot.context.properties.ConfigurationProperties;
23+
import org.springframework.context.annotation.Configuration;
24+
25+
import java.lang.reflect.Field;
26+
import java.util.Properties;
27+
28+
import static org.minbox.framework.api.boot.autoconfigure.pageable.ApiBootMyBatisPageableProperties.API_BOOT_PAGEABLE_PREFIX;
29+
30+
31+
/**
32+
* mybatis-pageable自动化配置属性
33+
*
34+
* @author:于起宇 ===============================
35+
* Created with IDEA.
36+
* Date:2018/8/4
37+
* Time:2:22 PM
38+
* 简书:http://www.jianshu.com/u/092df3f77bca
39+
* ================================
40+
*/
41+
@Data
42+
@Configuration
43+
@ConfigurationProperties(prefix = API_BOOT_PAGEABLE_PREFIX)
44+
public class ApiBootMyBatisPageableProperties {
45+
/**
46+
* mybatis pageable prefix
47+
*/
48+
public static final String API_BOOT_PAGEABLE_PREFIX = "api.boot.pageable";
49+
/**
50+
* 数据库方言
51+
* 默认使用mysql数据库方言
52+
*/
53+
private DialectEnum dialect = DialectEnum.MYSQL;
54+
55+
/**
56+
* 获取属性配置
57+
*
58+
* @return 配置文件对象
59+
*/
60+
public Properties getProperties() {
61+
62+
// 返回的配置对象
63+
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+
83+
return properties;
84+
}
85+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
* Copyright [2019] [恒宇少年 - 于起宇]
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package org.minbox.framework.api.boot.autoconfigure.pageable;
19+
20+
import com.gitee.hengboy.mybatis.pageable.config.PageableConfigurer;
21+
import com.gitee.hengboy.mybatis.pageable.interceptor.MyBatisExecutePageableInterceptor;
22+
import org.apache.ibatis.plugin.Interceptor;
23+
import org.apache.ibatis.session.SqlSessionFactory;
24+
import org.springframework.beans.factory.ObjectProvider;
25+
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
26+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
27+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
28+
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
29+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
30+
import org.springframework.context.annotation.Configuration;
31+
32+
import javax.annotation.PostConstruct;
33+
import java.util.List;
34+
35+
/**
36+
* ApiBoot Mybatis Pageable Auto Configuration
37+
*
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
45+
*/
46+
@Configuration
47+
@ConditionalOnBean(SqlSessionFactory.class)
48+
@EnableConfigurationProperties(ApiBootMyBatisPageableProperties.class)
49+
@ConditionalOnClass(DataSourceAutoConfiguration.class)
50+
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
51+
public class ApiBootMybatisPageableAutoConfiguration {
52+
/**
53+
* myabtis pageable properties
54+
*/
55+
private ApiBootMyBatisPageableProperties myBatisPageableProperties;
56+
/**
57+
* mybatis sqlSession factory
58+
*/
59+
private List<SqlSessionFactory> sqlSessionFactoryList;
60+
/**
61+
* mybatis pageable configurer
62+
*/
63+
private PageableConfigurer pageableConfigurer;
64+
65+
public ApiBootMybatisPageableAutoConfiguration(ApiBootMyBatisPageableProperties myBatisPageableProperties,
66+
ObjectProvider<List<SqlSessionFactory>> interceptorsProvider,
67+
ObjectProvider<PageableConfigurer> pageableConfigurerObjectProvider) {
68+
this.myBatisPageableProperties = myBatisPageableProperties;
69+
this.sqlSessionFactoryList = interceptorsProvider.getIfAvailable();
70+
this.pageableConfigurer = pageableConfigurerObjectProvider.getIfAvailable();
71+
}
72+
73+
/**
74+
* init interceptors
75+
*/
76+
@PostConstruct
77+
void addInterceptors() {
78+
Interceptor interceptor = new MyBatisExecutePageableInterceptor();
79+
// set properties to interceptor
80+
interceptor.setProperties(myBatisPageableProperties.getProperties());
81+
82+
for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {
83+
// pre
84+
addPreInterceptors(sqlSessionFactory);
85+
// mybatis pageable interceptor
86+
sqlSessionFactory.getConfiguration().addInterceptor(interceptor);
87+
// post
88+
addPostInterceptors(sqlSessionFactory);
89+
}
90+
}
91+
92+
/**
93+
* pre interceptors
94+
*
95+
* @param sqlSessionFactory sqlSessionFactory对象实例
96+
*/
97+
void addPreInterceptors(SqlSessionFactory sqlSessionFactory) {
98+
if (allowPageableConfigurer() && pageableConfigurer.prePlugins() != null) {
99+
loopAddInterceptor(pageableConfigurer.prePlugins(), sqlSessionFactory);
100+
}
101+
}
102+
103+
/**
104+
* after interceptors
105+
*
106+
* @param sqlSessionFactory sqlSessionFactory对象实例
107+
*/
108+
void addPostInterceptors(SqlSessionFactory sqlSessionFactory) {
109+
if (allowPageableConfigurer() && pageableConfigurer.postPlugins() != null) {
110+
loopAddInterceptor(pageableConfigurer.postPlugins(), sqlSessionFactory);
111+
}
112+
}
113+
114+
/**
115+
* add interceptor to sqlSessionFactory
116+
*
117+
* @param interceptors interceptors
118+
* @param sqlSessionFactory sqlSessionFactory instance
119+
*/
120+
void loopAddInterceptor(List<Interceptor> interceptors, SqlSessionFactory sqlSessionFactory) {
121+
for (Interceptor interceptor : interceptors) {
122+
sqlSessionFactory.getConfiguration().addInterceptor(interceptor);
123+
}
124+
}
125+
126+
/**
127+
* check have pageable configurer
128+
*
129+
* @return
130+
*/
131+
boolean allowPageableConfigurer() {
132+
return pageableConfigurer != null;
133+
}
134+
}

api-boot-project/api-boot-autoconfigure/src/main/resources/META-INF/spring.factories

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
1111
org.minbox.framework.api.boot.autoconfigure.quartz.ApiBootQuartzAutoConfiguration,\
1212
org.minbox.framework.api.boot.autoconfigure.datasource.ApiBootDataSourceSwitchAutoConfiguration,\
1313
org.minbox.framework.api.boot.autoconfigure.resource.ApiBootResourceLoadAutoConfiguration,\
14-
org.minbox.framework.api.boot.autoconfigure.push.ApiBootMessagePushAutoConfiguration
14+
org.minbox.framework.api.boot.autoconfigure.push.ApiBootMessagePushAutoConfiguration,\
15+
org.minbox.framework.api.boot.autoconfigure.pageable.ApiBootMybatisPageableAutoConfiguration

api-boot-project/api-boot-autoconfigure/src/main/resources/banner.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
|
77
'
88
:: Based On SpringBoot Version : ${spring-boot.formatted-version} ::
9-
:: ApiBoot Version : v2.0.5.RELEASE ::
9+
:: ApiBoot Version : v2.0.6.RC1 ::

0 commit comments

Comments
 (0)