詳解SpringBoot Mybatis如何對接多數(shù)據(jù)源
引言
在我們開發(fā)一些具有綜合功能的項目時,往往會碰到一種情況,需要同時連接多個數(shù)據(jù)庫,這個時候就需要用到多數(shù)據(jù)源的設計。而Spring 與 Myabtis 其實做了多數(shù)據(jù)源的適配,只需少許改動即可對接多數(shù)據(jù)源。本期我們就貼近實戰(zhàn),以一個單數(shù)據(jù)源的Demo為例,講述將其改為多數(shù)據(jù)源項目的過程,希望大家能有所體會
一、數(shù)據(jù)源的定義
數(shù)據(jù)源(Data Source)是指數(shù)據(jù)存儲的地方,大多數(shù)情況是指數(shù)據(jù)庫,不過文件服務器、傳感器、API等也能算數(shù)據(jù)源,主要是提供了對數(shù)據(jù)的訪問和操作。數(shù)據(jù)源中存儲了所有建立數(shù)據(jù)庫連接的信息。就像通過指定文件名稱可以在文件系統(tǒng)中找到文件一樣,通過提供正確的數(shù)據(jù)源名稱,你可以找到相應的數(shù)據(jù)庫連接

二、單數(shù)據(jù)源配置
因為SpringBoot對數(shù)據(jù)源有著高度的默認配置,只配置一個數(shù)據(jù)源時,該數(shù)據(jù)源會被作為默認,所以對接單數(shù)據(jù)源其實是非常簡單的。如果你的工程采用的yaml格式配置文件,我們僅需做如下配置:
spring:
#數(shù)據(jù)庫連接配置
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/springtest
username: root
password: root如果是采用properties配置文件的也是一樣的:
spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springtest spring.datasource.username=root spring.datasource.password=root
三、如何配置多數(shù)據(jù)源
1. 工程層級調整
我們以曾經搭建的工程為原始模板,進行對接多數(shù)據(jù)源的操作。沒看過的可以點此查看: 從零開始,手把手教你搭建Spring Boot后臺工程并說明

因為僅變動數(shù)據(jù)源,所以我們不改動其他層級,僅僅將 mapper 拆為 mapper1 與 mapper2 兩部分

2. Spring項目配置
然后我們需要在 application.properties 或者 application.yml 中定義多個數(shù)據(jù)源:
spring:
#數(shù)據(jù)庫連接配置
datasource1:
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://127.0.0.1:3306/springtest2
username: root
password: root
datasource2:
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://127.0.0.1:3306/springtest
username: root
password: root這里有兩個細節(jié)需要注意:
- 因為我們決定使用雙數(shù)據(jù)源,所以把數(shù)據(jù)源的連接配置改成了
datasource1和datasource2。而不再保留datasource,這樣SpringBoot就不再會為我們設定默認數(shù)據(jù)庫 - 因為我們目前采用的 springBoot2.5.2,默認的連接池為
Hikari,該連接池數(shù)據(jù)源的地址字段為jdbc-url而非url。在只有單個數(shù)據(jù)源時,SpringBoot走默認數(shù)據(jù)源邏輯為我們把url與jdbc-url進行映射,保證我們獲得數(shù)據(jù)源。此時我們自己設置的數(shù)據(jù)源沒有進行映射處理,就需要保證字段符合Hikari的要求。否則會出現(xiàn)java.lang.IllegalArgumentException: jdbcUrl is required with driverClassName異常
3. 會話配置
僅有配置文件可不行,接下來,我們需要在代碼中讀取到配置,并建立兩個數(shù)據(jù)源。如下,每個數(shù)據(jù)源都有隔離的mapper接口、xml文件、會話工廠及會話模板
第一個數(shù)據(jù)源 如下(示例):
@Configuration
@MapperScan(basePackages = "com.zhanfu.springboot.demo.mapper1", sqlSessionFactoryRef = "sqlSessionFactory1")
public class DataSource1Config {
@Bean
@ConfigurationProperties(prefix = "spring.datasource1")
public DataSource dataSource1() {
return DataSourceBuilder.create().build();
}
@Bean
public SqlSessionFactory sqlSessionFactory1() throws Exception {
SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
sessionFactoryBean.setDataSource(dataSource1());
String locationPattern = "classpath*:/mapper1/*.xml";
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
sessionFactoryBean.setMapperLocations(resolver.getResources(locationPattern));
return sessionFactoryBean.getObject();
}
@Bean(name = "sqlSessionTemplate1")
public SqlSessionTemplate sqlSessionTemplate1(@Qualifier("sqlSessionFactory1") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}配置第二個數(shù)據(jù)源 DataSource2Config
@Configuration
@MapperScan(basePackages = "com.zhanfu.springboot.demo.mapper2", sqlSessionFactoryRef = "sqlSessionFactory2")
public class DataSource2Config {
@Bean
@ConfigurationProperties(prefix = "spring.datasource2")
public DataSource dataSource2() {
return DataSourceBuilder.create().build();
}
@Bean
public SqlSessionFactory sqlSessionFactory2() throws Exception {
SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
sessionFactoryBean.setDataSource(dataSource2());
String locationPattern = "classpath*:/mapper2/*.xml";
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
sessionFactoryBean.setMapperLocations(resolver.getResources(locationPattern));
return sessionFactoryBean.getObject();
}
@Bean(name = "sqlSessionTemplate2")
public SqlSessionTemplate sqlSessionTemplate2(@Qualifier("sqlSessionFactory2") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
@Bean(name = "productMapper")
public ProductMapper mapper2(@Qualifier("sqlSessionTemplate2") SqlSessionTemplate sqlSessionTemplate) throws Exception {
return sqlSessionTemplate.getMapper(ProductMapper.class);
}
}4. 事務管理器
為兩個數(shù)據(jù)源分別配置自己的事務管理器,如果你的項目里通篇沒有方法級別的事務(一個SQL就是一個事務),那不設置這個也不影響,否則還是建議加上。
@Configuration
public class TransactionManagerConfig {
@Autowired
private DataSource dataSource1;
@Autowired
private DataSource dataSource2;
@Bean
public PlatformTransactionManager txManager1() {
return new DataSourceTransactionManager(dataSource1);
}
@Bean
public PlatformTransactionManager txManager2() {
return new DataSourceTransactionManager(dataSource2);
}
}四、驗證
我們把兩張表拆進兩個庫中,以兩個庫模擬兩個數(shù)據(jù)源,使得程序可以同時連接兩個庫

瀏覽器輸入 http://127.0.0.1:8080/user/findall 查詢接口成功

再在瀏覽器輸入 http://127.0.0.1:8080/product/findall 查詢第二個庫的數(shù)據(jù)亦成功返回

這樣我們就完成了一個工程同時連接兩個數(shù)據(jù)源。
總結
經過上述的操作,我們已經成功把項目對接了多數(shù)據(jù)源。當然,方案肯定不止這一種,后續(xù)圍繞該問題,我們還會講解其他方式。但不論是什么方式,主旨都是加深大家對SpringBoot 和 Mybatis的理解,我們曾經梳理過全流程,但只是蜻蜓點水帶大家看一遍大體輪廓,并不足以讓你精通,后面本專欄將繼續(xù)深入講解,更多關于SpringBoot Mybatis對接多數(shù)據(jù)源的資料請關注腳本之家其它相關文章!
- MyBatisPuls多數(shù)據(jù)源操作數(shù)據(jù)源偶爾報錯問題
- Mybatis-plus配置多數(shù)據(jù)源,連接多數(shù)據(jù)庫方式
- MyBatis-Plus多數(shù)據(jù)源的示例代碼
- SpringBoot集成Mybatis實現(xiàn)對多數(shù)據(jù)源訪問原理
- Seata集成Mybatis-Plus解決多數(shù)據(jù)源事務問題
- Mybatis操作多數(shù)據(jù)源的實現(xiàn)
- 一文搞懂MyBatis多數(shù)據(jù)源Starter實現(xiàn)
- Mybatis-plus多數(shù)據(jù)源配置的兩種方式總結
- MyBatis-Plus 集成動態(tài)多數(shù)據(jù)源的實現(xiàn)示例
- Mybatis-Plus的多數(shù)據(jù)源你了解嗎
- mybatis-flex實現(xiàn)多數(shù)據(jù)源操作
相關文章
springboot整合xxl-job的實現(xiàn)示例
本文主要介紹了springboot整合xxl-job的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-06-06
IDEA2020如何打開Run Dashboard的方法步驟
這篇文章主要介紹了IDEA2020如何打開Run Dashboard的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-07-07
Spring?MVC基于注解的使用之JSON數(shù)據(jù)處理的方法
這篇文章主要介紹了Spring?MVC基于注解的使用JSON數(shù)據(jù)處理,json是一種輕量級的數(shù)據(jù)交換格式,是一種理想的數(shù)據(jù)交互語言,它易于閱讀和編寫,同時也易于機器解析和生成,本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2022-05-05
SparkStreaming-Kafka通過指定偏移量獲取數(shù)據(jù)實現(xiàn)
這篇文章主要為大家介紹了SparkStreaming-Kafka通過指定偏移量獲取數(shù)據(jù),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06
Java8中對于LocalDateTime的序列化和反序列化問題
這篇文章主要介紹了Java8中對于LocalDateTime的序列化和反序列化問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
SpringBoot中動態(tài)數(shù)據(jù)源是實現(xiàn)與用途
這篇文章主要是來和大家討論一下SpringBoot中動態(tài)數(shù)據(jù)源是實現(xiàn)與用途,文中的示例代碼簡潔易懂,具有一定的學習價值,感興趣的可以了解一下2023-08-08

