javascript
db2 springboot 整合_[SpringBoot]快速配置多数据源(整合MyBatis)
前言
由于業(yè)務(wù)需求,需要同時在SpringBoot中配置兩套數(shù)據(jù)源(連接兩個數(shù)據(jù)庫),要求能做到service層在調(diào)用各數(shù)據(jù)庫表的mapper時能夠自動切換數(shù)據(jù)源,也就是mapper自動訪問正確的數(shù)據(jù)庫。
本文內(nèi)容:
在Springboot+Mybatis項目的基礎(chǔ)上,學(xué)習(xí)多數(shù)據(jù)源的快速配置
避免網(wǎng)上某些配置數(shù)據(jù)源文章的深坑
SpringBoot實戰(zhàn)系列教程回顧:
正文
多數(shù)據(jù)源配置實戰(zhàn)(整合MyBatis)
SpringBoot版本:2.0.6.RELEASE
項目結(jié)構(gòu)圖(原諒我保護(hù)隱私代碼):
image.png
排除SpringBoot的自動配置類DataSourceAutoConfiguration
首先要在@SpringBootApplication排除該類,因為它會讀取application.properties文件的spring.datasource.*屬性并自動配置單數(shù)據(jù)源
@SpringBootApplication(exclude?=?{
DataSourceAutoConfiguration.class
})
在application.properties中配置多數(shù)據(jù)源連接信息
你需要連接多少個數(shù)據(jù)庫源,就配置幾個,名字可以自由命名代替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
注意:這里請一定將spring.datasource.db1.url改為spring.datasource.db1.jdbc-url
官方文檔的解釋是:因為連接池的實際類型沒有被公開,所以在您的自定義數(shù)據(jù)源的元數(shù)據(jù)中沒有生成密鑰,而且在IDE中沒有完成(因為DataSource接口沒有暴露屬性)。另外,如果您碰巧在類路徑上有Hikari,那么這個基本設(shè)置就不起作用了,因為Hikari沒有url屬性(但是確實有一個jdbcUrl屬性)。在這種情況下,您必須重寫您的配置如下:
手動創(chuàng)建數(shù)據(jù)庫配置類
由于我們禁掉了自動數(shù)據(jù)源配置,因為下一步就需要手動將這些數(shù)據(jù)源創(chuàng)建出來,創(chuàng)建DataSourceConfig類
@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自動使用不同的數(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文件掃描路徑,否則報錯Invalid bound statement (not found)
factoryBean.setMapperLocations(new?PathMatchingResourcePatternResolver().getResources("classpath:mapper/xxxxxx/*.xml"));
完成這些配置后,假設(shè)我們有2個Mapper :
mapper.db1.xxxMapper和mapper.db2.xxxMapper
我們在程序的任何位置使用前者時會自動連接db1庫,后者連接db2庫。
參考文獻(xiàn)
主要參考:
https://blog.csdn.net/neosmith/article/details/61202084
其他參考:
http://blog.didispace.com/springbootmultidatasource/
總結(jié)
本文在一個Springboot+Mybatis項目的基礎(chǔ)上,學(xué)習(xí)多數(shù)據(jù)源的快速配置。
祝大家國慶節(jié)假期快樂!
關(guān)注我
我目前是一名后端開發(fā)工程師。主要關(guān)注后端開發(fā),數(shù)據(jù)安全,邊緣計算等方向。
微信:yangzd1102(請注明來意)
Github:@qqxx6661
個人博客:
CSDN:@Rude3Knife
知乎:@Zhendong
簡書:@蠻三刀把刀
掘金:@蠻三刀把刀
原創(chuàng)博客主要內(nèi)容
Java知識點復(fù)習(xí)全手冊
Leetcode算法題解析
劍指offer算法題解析
SpringCloud菜鳥入門實戰(zhàn)系列
SpringBoot菜鳥入門實戰(zhàn)系列
爬蟲相關(guān)技術(shù)文章
后端開發(fā)相關(guān)技術(shù)文章
個人公眾號:后端技術(shù)漫談
公眾號201992.jpg
如果文章對你有幫助,不妨收藏起來并轉(zhuǎn)發(fā)給您的 朋友們~
總結(jié)
以上是生活随笔為你收集整理的db2 springboot 整合_[SpringBoot]快速配置多数据源(整合MyBatis)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 9528开头是什么电话 哪些电话是952
- 下一篇: VIVO手机怎么关闭省电模式