javascript
db2 springboot 整合_[SpringBoot]快速配置多数据源(整合MyBatis)
前言
由于業(yè)務(wù)需求,需要同時(shí)在SpringBoot中配置兩套數(shù)據(jù)源(連接兩個(gè)數(shù)據(jù)庫(kù)),要求能做到service層在調(diào)用各數(shù)據(jù)庫(kù)表的mapper時(shí)能夠自動(dòng)切換數(shù)據(jù)源,也就是mapper自動(dòng)訪問(wèn)正確的數(shù)據(jù)庫(kù)。
本文內(nèi)容:
在Springboot+Mybatis項(xiàng)目的基礎(chǔ)上,學(xué)習(xí)多數(shù)據(jù)源的快速配置
避免網(wǎng)上某些配置數(shù)據(jù)源文章的深坑
SpringBoot實(shí)戰(zhàn)系列教程回顧:
正文
多數(shù)據(jù)源配置實(shí)戰(zhàn)(整合MyBatis)
SpringBoot版本:2.0.6.RELEASE
項(xiàng)目結(jié)構(gòu)圖(原諒我保護(hù)隱私代碼):
image.png
排除SpringBoot的自動(dòng)配置類(lèi)DataSourceAutoConfiguration
首先要在@SpringBootApplication排除該類(lèi),因?yàn)樗鼤?huì)讀取application.properties文件的spring.datasource.*屬性并自動(dòng)配置單數(shù)據(jù)源
@SpringBootApplication(exclude?=?{
DataSourceAutoConfiguration.class
})
在application.properties中配置多數(shù)據(jù)源連接信息
你需要連接多少個(gè)數(shù)據(jù)庫(kù)源,就配置幾個(gè),名字可以自由命名代替db1,db2
#?database
db.conn.str?=?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useLocalSessionState=true&tinyInt1isBit=false
spring.datasource.db1.jdbc-url=jdbc:mysql://xxxx1:xxxx/xxxxx1?${db.conn.str}
spring.datasource.db1.username=xxxxx
spring.datasource.db1.password=xxxxx
spring.datasource.db1.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.db2.jdbc-url=jdbc:mysql://xxxxx2:xxxx/xxxxx2?${db.conn.str}
spring.datasource.db2.username=xxxxx
spring.datasource.db2.password=xxxxx
spring.datasource.db2.driver-class-name=com.mysql.jdbc.Driver
注意:這里請(qǐng)一定將spring.datasource.db1.url改為spring.datasource.db1.jdbc-url
官方文檔的解釋是:因?yàn)檫B接池的實(shí)際類(lèi)型沒(méi)有被公開(kāi),所以在您的自定義數(shù)據(jù)源的元數(shù)據(jù)中沒(méi)有生成密鑰,而且在IDE中沒(méi)有完成(因?yàn)镈ataSource接口沒(méi)有暴露屬性)。另外,如果您碰巧在類(lèi)路徑上有Hikari,那么這個(gè)基本設(shè)置就不起作用了,因?yàn)镠ikari沒(méi)有url屬性(但是確實(shí)有一個(gè)jdbcUrl屬性)。在這種情況下,您必須重寫(xiě)您的配置如下:
手動(dòng)創(chuàng)建數(shù)據(jù)庫(kù)配置類(lèi)
由于我們禁掉了自動(dòng)數(shù)據(jù)源配置,因?yàn)橄乱徊骄托枰謩?dòng)將這些數(shù)據(jù)源創(chuàng)建出來(lái),創(chuàng)建DataSourceConfig類(lèi)
@Configuration
public?class?DataSourceConfig{
@Bean(name?=?"db1")
@ConfigurationProperties(prefix?=?"spring.datasource.db1")
public?DataSource?businessDbDataSource()?{
return?DataSourceBuilder.create().build();
}
@Bean(name?=?"db2")
@ConfigurationProperties(prefix?=?"spring.datasource.db2")
public?DataSource?newhomeDbDataSource()?{
return?DataSourceBuilder.create().build();
}
}
分別配置不同數(shù)據(jù)源的mybatis的SqlSessionFactory
這樣做可以讓我們的不同包名底下的mapper自動(dòng)使用不同的數(shù)據(jù)源
創(chuàng)建Db1Config:
/**
*?@author?yangzhendong01
*/
@Configuration
@MapperScan(basePackages?=?{"com.xxxxx.webApi.mapper.db1"},?sqlSessionFactoryRef?=?"sqlSessionFactoryDb1")
public?class?Db1Config{
@Autowired
@Qualifier("db1")
private?DataSource?dataSourceDb1;
@Bean
public?SqlSessionFactory?sqlSessionFactoryDb1()?throws?Exception{
SqlSessionFactoryBean?factoryBean?=?new?SqlSessionFactoryBean();
factoryBean.setDataSource(dataSourceDb1);
factoryBean.setMapperLocations(new?PathMatchingResourcePatternResolver().getResources("classpath:mapper/db1/*.xml"));
return?factoryBean.getObject();
}
@Bean
public?SqlSessionTemplate?sqlSessionTemplateDb1()?throws?Exception{
return?new?SqlSessionTemplate(sqlSessionFactoryDb1());
}
}
創(chuàng)建Db2Config:
/**
*?@author?yangzhendong01
*/
@Configuration
@MapperScan(basePackages?=?{"com.xxxxx.webApi.mapper.db2"},?sqlSessionFactoryRef?=?"sqlSessionFactoryDb2")
public?class?Db2Config{
@Autowired
@Qualifier("db2")
private?DataSource?dataSourceDb2;
@Bean
public?SqlSessionFactory?sqlSessionFactoryDb2()?throws?Exception{
SqlSessionFactoryBean?factoryBean?=?new?SqlSessionFactoryBean();
factoryBean.setDataSource(dataSourceDb2);
factoryBean.setMapperLocations(new?PathMatchingResourcePatternResolver().getResources("classpath:mapper/db2/*.xml"));
return?factoryBean.getObject();
}
@Bean
public?SqlSessionTemplate?sqlSessionTemplateDb2()?throws?Exception{
return?new?SqlSessionTemplate(sqlSessionFactoryDb2());
}
}
注意:此步一定要添加mapper.xml文件掃描路徑,否則報(bào)錯(cuò)Invalid bound statement (not found)
factoryBean.setMapperLocations(new?PathMatchingResourcePatternResolver().getResources("classpath:mapper/xxxxxx/*.xml"));
完成這些配置后,假設(shè)我們有2個(gè)Mapper :
mapper.db1.xxxMapper和mapper.db2.xxxMapper
我們?cè)诔绦虻娜魏挝恢檬褂们罢邥r(shí)會(huì)自動(dòng)連接db1庫(kù),后者連接db2庫(kù)。
參考文獻(xiàn)
主要參考:
https://blog.csdn.net/neosmith/article/details/61202084
其他參考:
http://blog.didispace.com/springbootmultidatasource/
總結(jié)
本文在一個(gè)Springboot+Mybatis項(xiàng)目的基礎(chǔ)上,學(xué)習(xí)多數(shù)據(jù)源的快速配置。
祝大家國(guó)慶節(jié)假期快樂(lè)!
關(guān)注我
我目前是一名后端開(kāi)發(fā)工程師。主要關(guān)注后端開(kāi)發(fā),數(shù)據(jù)安全,邊緣計(jì)算等方向。
微信:yangzd1102(請(qǐng)注明來(lái)意)
Github:@qqxx6661
個(gè)人博客:
CSDN:@Rude3Knife
知乎:@Zhendong
簡(jiǎn)書(shū):@蠻三刀把刀
掘金:@蠻三刀把刀
原創(chuàng)博客主要內(nèi)容
Java知識(shí)點(diǎn)復(fù)習(xí)全手冊(cè)
Leetcode算法題解析
劍指offer算法題解析
SpringCloud菜鳥(niǎo)入門(mén)實(shí)戰(zhàn)系列
SpringBoot菜鳥(niǎo)入門(mén)實(shí)戰(zhàn)系列
爬蟲(chóng)相關(guān)技術(shù)文章
后端開(kāi)發(fā)相關(guān)技術(shù)文章
個(gè)人公眾號(hào):后端技術(shù)漫談
公眾號(hào)201992.jpg
如果文章對(duì)你有幫助,不妨收藏起來(lái)并轉(zhuǎn)發(fā)給您的 朋友們~
總結(jié)
以上是生活随笔為你收集整理的db2 springboot 整合_[SpringBoot]快速配置多数据源(整合MyBatis)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 9528开头是什么电话 哪些电话是952
- 下一篇: gradle idea java ssm