前言
項(xiàng)目中用到了兩個(gè)數(shù)據(jù)庫(kù),分別是Oracle和Mysql,涉及到了多數(shù)據(jù)源問(wèn)題,這里做下記錄
官方講解:https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter
日志JDBC配置:https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE_LogFilter
Druid常見(jiàn)問(wèn)題匯總:https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98
配置
- 首先配置application.yml文件,這里利用阿里的Druid作為連接池,base和follow分別代表主庫(kù)和從庫(kù)
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
base:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: oracle.jdbc.driver.OracleDriver
initialize: true #指定初始化數(shù)據(jù)源,是否用data.sql來(lái)初始化,默認(rèn): true
name: base
jdbc-url: jdbc:oracle:thin:@189.126.156.396:9522:oratest
username: 用戶(hù)名
password: 密碼
follow:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
initialize: true
name: follow
url: jdbc:mysql://162.66.66.66:3666/paygateway
username: 你的歌用戶(hù)名
password: 你的密碼
---
#下面是分頁(yè)和日志相關(guān)內(nèi)容,不需要可以刪掉
pagehelper:
reasonable: true
helperDialect: oracle
support-methods-arguments: true
params: count=countSql
mybatis:
configuration:
mapUnderscoreToCamelCase: true
logging:
level:
com:
xxx:
paygateway:
dao: DEBUG
org:
spring:
springboot:
dao: DEBUG
springframework: WARN
- 配置主庫(kù)Configuration,注意prefix前綴和@Primary注解(表示主庫(kù))和下劃線轉(zhuǎn)駝峰方法的配置
@Configuration
@MapperScan(basePackages = "com.xxx.paygateway.dao.db1", sqlSessionTemplateRef = "baseSqlSessionTemplate")
public class BaseDataSourceConfig {
//這里配置數(shù)據(jù)源
@Bean(name = "baseDataSource")
@ConfigurationProperties(prefix = "spring.datasource.base")
@Primary
public DataSource setDataSource() {
return DataSourceBuilder.create().build();
}
//這里配置事務(wù)管理器
@Bean(name = "baseTransactionManager")
@Primary
public DataSourceTransactionManager setTransactionManager(@Qualifier("baseDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
//這里配置SqlSessionFactory,連接工廠
@Bean(name = "baseSqlSessionFactory")
@Primary
public SqlSessionFactory setSqlSessionFactory(@Qualifier("baseDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
//此處配置下劃線轉(zhuǎn)駝峰
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
configuration.setMapUnderscoreToCamelCase(true);
bean.setConfiguration(configuration);
bean.setDataSource(dataSource);
//此處配置mybatis掃描路徑
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mappers/base/*.xml"));
return bean.getObject();
}
//這里配置SqlSessionTemplate,標(biāo)準(zhǔn)操作模板
@Bean(name = "baseSqlSessionTemplate")
@Primary
public SqlSessionTemplate setSqlSessionTemplate(@Qualifier("baseSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
- 配置從庫(kù)Configuration,與從庫(kù)基本一致,注意注解和前綴就可以了
@Configuration
@MapperScan(basePackages = "com.xxx.paygateway.dao.db2", sqlSessionTemplateRef = "fSqlSessionTemplate")
public class FollowDataSourceConfig {
@Bean(name = "fDataSource")
@ConfigurationProperties(prefix = "spring.datasource.follow")
public DataSource setDataSource() {
return new DruidDataSource();
}
@Bean(name = "fTransactionManager")
public DataSourceTransactionManager setTransactionManager(@Qualifier("fDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "fSqlSessionFactory")
public SqlSessionFactory setSqlSessionFactory(@Qualifier("fDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
configuration.setMapUnderscoreToCamelCase(true);
bean.setConfiguration(configuration);
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mappers/follow/*.xml"));
return bean.getObject();
}
@Bean(name = "fSqlSessionTemplate")
public SqlSessionTemplate setSqlSessionTemplate(@Qualifier("fSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
-
把兩個(gè)數(shù)據(jù)庫(kù)的dao層和mapper.xml文件分到不同的文件夾下面,1歸1,2歸2。service層正常調(diào)用即可!
image.png
image.png

