前提提示:
完整源代碼參照這裏
前面寫過一篇文章:SpringBoot 配置多數據源,介紹瞭如果用SpringBoot自定義多數據源,有人可能發現了配置文件中啓用了PageHelper-一個基於Mybatis的物理分頁組件。但是,該文中的PageHelper只配置了mysql一種,也就是說這篇文章講解的配置方案有侷限,只能配置多數據源都是mysql數據庫的配置。
那如果多數據源中有多中數據庫該如何配置的,下面我就給大家講講SpringBoot、Mybatis和PageHelper的深度整合,實現它們的完美配置。
在講之間,大家可以再看另一篇文章: SpringBoot+Mybatis完全基於註解開發MySql數據庫,以爲我講解的方法已經完全去除Mybatis的xml配置文件了。
多數據源DataSource的配置基本與原文配置一樣,只是改爲了yml配置。但是要支持多種數據源,麻煩點就在PageHelper上。我們先來看看原因:
找到圖中PageHelper的自動加載配置文件,找到如下代碼:
它的意思比較易懂,就是對於所有的數據源(SqlSessionFactoryList)都是用同一個PageHelper!這明顯與我們的要求是違背的!另外PageHelper有要求,同一個數據源(SqlSessionFactory)最多隻能支持一個PageHelper!那我們只能廢棄自動加載的PageInterceptor,然後在程序中手動加載我們自定義的PageInterceptor!
1.禁用PageHelperAutoConfiguration
我們還想用PageHelper的配置方式,但是這個禁用會導致下圖中圈起來的代碼的失效
也就是PageHelperAutoConfiguration所在的jar包定義的bean無法自動註冊,因此,我們先在數據庫配置文件中註冊一個同名bean,同時爲了使用yml配置文件屬性,要定義一個Properties接收配置的屬性
2.自定義PageInterceptor
好了,前期準備好了,該我們來定義PageInterceptor了
是不是與組件自定義的bean很相似~
3.註冊到SqlSessionFactory中
我們先進入MybatisAutoConfiguration文件看一下Mybatis的SqlSessionFactory是如何初始化的
嗯...它帶了@ConditionalOnMissingBean,那就意味着我們可以重新定義它!
爲了保持功能不缺失,我們完全參照這個來定義
@Bean(name = "sqlSessionFactory")
public SqlSessionFactory sqlSessionFactorySecond(
@Qualifier("dataSource") DataSource dataSource,
@Qualifier("mybatisProperties") MybatisProperties properties,
ResourceLoader resourceLoader,
ObjectProvider<List<ConfigurationCustomizer>> configurationCustomizersProvider,
ObjectProvider<Interceptor[]> interceptorsProvider,
@Qualifier("pageInterceptor") Interceptor pageInterceptor,
ObjectProvider<DatabaseIdProvider> databaseIdProvider) throws Exception {
SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
factory.setDataSource(dataSource);
factory.setVfs(SpringBootVFS.class);
if (StringUtils.hasText(properties.getConfigLocation())) {
factory.setConfigLocation(resourceLoader.getResource(properties.getConfigLocation()));
}
org.apache.ibatis.session.Configuration configuration = properties.getConfiguration();
if (configuration == null && !StringUtils.hasText(properties.getConfigLocation())) {
configuration = new org.apache.ibatis.session.Configuration();
}
List<ConfigurationCustomizer> configurationCustomizers = configurationCustomizersProvider.getIfAvailable();
if (configuration != null && !CollectionUtils.isEmpty(configurationCustomizers)) {
for (ConfigurationCustomizer customizer : configurationCustomizers) {
customizer.customize(configuration);
}
}
factory.setConfiguration(configuration);
if (properties.getConfigurationProperties() != null) {
factory.setConfigurationProperties(properties.getConfigurationProperties());
}
Interceptor[] interceptors = filterInterceptors(interceptorsProvider.getIfAvailable(), pageInterceptor);
if (!ObjectUtils.isEmpty(interceptors)) {
factory.setPlugins(interceptors);
}
DatabaseIdProvider _databaseIdProvider = databaseIdProvider.getIfAvailable();
if (_databaseIdProvider != null) {
factory.setDatabaseIdProvider(_databaseIdProvider);
}
if (StringUtils.hasLength(properties.getTypeAliasesPackage())) {
factory.setTypeAliasesPackage(properties.getTypeAliasesPackage());
}
if (StringUtils.hasLength(properties.getTypeHandlersPackage())) {
factory.setTypeHandlersPackage(properties.getTypeHandlersPackage());
}
if (!ObjectUtils.isEmpty(properties.resolveMapperLocations())) {
factory.setMapperLocations(properties.resolveMapperLocations());
}
return factory.getObject();
}
這個,有幾個需要注意的點:
入參
我們定義的分頁組件單獨加了進來。
添加組件
這裏,我們對單獨添加的分頁組件進行了處理
分頁組件慮重
爲了保證分頁組件唯一,我們需要慮重
參數yml配置
我們重新定義了參數配置
OK,這樣就完成了SpringBoot+Mybatis+PageHelper深度整合配置,完整源代碼參照這裏。
如果你喜歡,可以點擊收藏或關注,我會經常更新關於SpringBoot、SpringCloud等相關用法和技能。