前言
由于業(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ù)源文章的深坑
正文
多數(shù)據(jù)源配置實(shí)戰(zhàn)(整合MyBatis)
SpringBoot版本:2.0.6.RELEASE
項目結(jié)構(gòu)圖(原諒我保護(hù)隱私代碼):
排除SpringBoot的自動配置類DataSourceAutoConfiguration
首先要在@SpringBootApplication排除該類,因?yàn)樗鼤x取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
官方文檔的解釋是:因?yàn)檫B接池的實(shí)際類型沒有被公開,所以在您的自定義數(shù)據(jù)源的元數(shù)據(jù)中沒有生成密鑰膘滨,而且在IDE中沒有完成(因?yàn)镈ataSource接口沒有暴露屬性)。另外火邓,如果您碰巧在類路徑上有Hikari,那么這個基本設(shè)置就不起作用了摇天,因?yàn)镠ikari沒有url屬性(但是確實(shí)有一個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é)假期快樂!