|
| 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 | +} |
0 commit comments