Spring Boot 整合mybatis 使用多數(shù)據(jù)源的實現(xiàn)方法
前言
本篇教程偏向?qū)崙?zhàn),程序猿直接copy代碼加入到自己的項目中做簡單的修修改改便可使用,而對于springboot以及mybatis不在此進行展開介紹,如有讀者希望了解可以給我留言,并持續(xù)關注,我后續(xù)會慢慢更新。(黑色區(qū)域代碼部分,安卓手機可手動向左滑動,來查看全部代碼)
整合
其實整合很簡單,如果是用gradle的話,在build.gradle文件里加入
compile('org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.1')
如果是用maven的話在pom.xml文件里加入
單庫配置:
引入之后,默認情況下,Spring Boot會自動為我們配置好一個DataSource,它會在classpath中搜索H2、hsqldb等內(nèi)存數(shù)據(jù)庫的jar包,如果找到了,就會自動配置一個內(nèi)存數(shù)據(jù)庫的DataSource。
如果在application.yml或application.property中指定了spring.datasource.*的相關配置參數(shù),Spring Boot就會使用該配置創(chuàng)建一個DataSource。
然后會自動創(chuàng)建使用該DataSource的SqlSessionFactoryBean以及SqlSessionTemplate。會自動掃描你的Mappers,連接到SqlSessionTemplate,并注冊到Spring上下文中。
spring.datasource.url=jdbc:mysql://localhost/test spring.datasource.username=dbuser spring.datasource.password=dbpass spring.datasource.driver-class-name=com.mysql.jdbc.Driver
更多參數(shù)請查看DataSourceProperties
多庫配置:
由于業(yè)務需要,項目要同時使用多個數(shù)據(jù)庫進行業(yè)務開發(fā):
首先,我們必須在application.property中自定義兩個數(shù)據(jù)源的配置,一個使用first.datasource.*,另一個使用second.datasource.*,為了能使別人一眼看出連接的是什么庫,可以使用數(shù)據(jù)庫命名,比如user庫,則可以使用user.datasource.*,在使用多數(shù)據(jù)源的時候,所有必要配置都不能省略。
first.datasource.url=jdbc:mysql://localhost/first first.datasource.username=dbuser1 first.datasource.password=dbpass1 first.datasource.driver-class-name=com.mysql.jdbc.Driver first.datasource.type=com.alibaba.druid.pool.DruidDataSource//我用的是Druid,也可以不加用默認的 second.datasource.url=jdbc:mysql://localhost/second second.datasource.username=dbuser2 second.datasource.password=dbpass2 second.datasource.driver-class-name=com.mysql.jdbc.Driver second.datasource.type=com.alibaba.druid.pool.DruidDataSource
直接上代碼,我的做法是將兩個數(shù)據(jù)源用兩個配置類創(chuàng)建:
@Configuration
@MapperScan(basePackages = {"com.user.server.dao"}, sqlSessionTemplateRef = "userSqlSessionTemplate")
public class UserMybatisConfig {
@Bean(name = "userDataSource")
@Primary //必須加此注解,不然報錯,下一個類則不需要添加
@ConfigurationProperties(prefix = "first.datasource") // prefix值必須是application.properteis中對應屬性的前綴
public DataSource userDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
public SqlSessionFactory userSqlSessionFactory(@Qualifier("userDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
//添加XML目錄
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
try {
bean.setMapperLocations(resolver.getResources("classpath*:com/user/server/dao/mapping/*.xml"));
return bean.getObject();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
@Bean
public SqlSessionTemplate userSqlSessionTemplate(@Qualifier("userSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory); // 使用上面配置的Factory
return template;
}
}
@Configuration
@MapperScan(basePackages = {"com.airmi.server.dao"}, sqlSessionTemplateRef = "autoTestSqlSessionTemplate")
public class AutoTestMybatisConfig {
@Bean
@ConfigurationProperties(prefix = "autotest.datasource")
public DataSource autoTestDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
public SqlSessionTemplate autoTestSqlSessionTemplate(@Qualifier("autoTestSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory);
return template;
}
@Bean
public SqlSessionFactory autoTestSqlSessionFactory(@Qualifier("autoTestDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
//添加XML目錄
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
try {
bean.setMapperLocations(resolver.getResources("classpath*:com/airmi/server/dao/mapping/*.xml"));
return bean.getObject();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
@Primary //該注解表示在同一個接口有多個實現(xiàn)類可以注入的時候,默認選擇哪一個,而不是讓autowire注解報錯,官網(wǎng)要求當多個數(shù)據(jù)源時,必須指定一個datasource,另一個datasource則不用添加。
@Qualifier 根據(jù)名稱進行注入,通常是在具有相同的多個類型的實例的一個注入(例如有多個DataSource類型的實例)。
@MapperScan (basePackages = {"com.user.server.dao"}, sqlSessionTemplateRef = "userSqlSessionTemplate") basePackages為mapper所在的包,sqlSessionTemplateRef要引用的實例。
user代碼結構如下:
總結
以上所述是小編給大家介紹的Spring Boot 整合mybatis 使用多數(shù)據(jù)源的實現(xiàn)方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關文章
SpringDataJPA之Specification復雜查詢實戰(zhàn)
這篇文章主要介紹了SpringDataJPA之Specification復雜查詢實戰(zhàn),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
Java的ThreadPoolExecutor業(yè)務線程池詳細解析
這篇文章主要介紹了Java線程池ThreadPoolExecutor詳細解析,任務剛開始進來的時候就創(chuàng)建核心線程,核心線程滿了會把任務放到阻塞隊列,阻塞隊列滿了之后才會創(chuàng)建空閑線程,達到最大線程數(shù)之后,再有任務進來,就只能執(zhí)行拒絕策略了,需要的朋友可以參考下2024-01-01
Spring Boot 如何使用Liquibase 進行數(shù)據(jù)庫遷移(操作方法)
在Spring Boot應用程序中使用Liquibase進行數(shù)據(jù)庫遷移是一種強大的方式來管理數(shù)據(jù)庫模式的變化,本文重點講解如何在Spring Boot應用程序中使用Liquibase進行數(shù)據(jù)庫遷移,從而更好地管理數(shù)據(jù)庫模式的變化,感興趣的朋友跟隨小編一起看看吧2023-09-09

