SpringBoot中的雙數(shù)據(jù)源配置過程
1.引入使用的數(shù)據(jù)源類型(mysql oracle sqlserver等)依賴
本文配置為oracel和postgresql
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
2.yml中配置數(shù)據(jù)源

3.數(shù)據(jù)源的配合
package com.cbd.findig.data.process.config;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import javax.sql.DataSource;
/**
* 多數(shù)據(jù)源配置
*/
@Configuration
public class DataSourceConfig {
//主數(shù)據(jù)源配置 ds1數(shù)據(jù)源
@Primary
@Bean(name = "ds1DataSourceProperties")
@ConfigurationProperties(prefix = "spring.datasource.ds1")
public DataSourceProperties ds1DataSourceProperties() {
return new DataSourceProperties();
}
//主數(shù)據(jù)源 ds1數(shù)據(jù)源
@Primary
@Bean(name = "ds1DataSource")
public DataSource ds1DataSource(@Qualifier("ds1DataSourceProperties") DataSourceProperties dataSourceProperties) {
return dataSourceProperties.initializeDataSourceBuilder().build();
}
//第二個ds2數(shù)據(jù)源配置
@Bean(name = "ds2DataSourceProperties")
@ConfigurationProperties(prefix = "spring.datasource.ds2")
public DataSourceProperties ds2DataSourceProperties() {
return new DataSourceProperties();
}
//第二個ds2數(shù)據(jù)源
@Bean("ds2DataSource")
public DataSource ds2DataSource(@Qualifier("ds2DataSourceProperties") DataSourceProperties dataSourceProperties) {
return dataSourceProperties.initializeDataSourceBuilder().build();
}
}
4.JdbcTemplate的配置
package com.cbd.findig.data.process.config;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;
import javax.sql.DataSource;
/**
* JdbcTemplate多數(shù)據(jù)源配置
* 依賴于數(shù)據(jù)源配置
*
* @see DataSourceConfig
*/
@Configuration
public class JdbcTemplateDataSourceConfig {
//JdbcTemplate主數(shù)據(jù)源ds1數(shù)據(jù)源
@Primary
@Bean(name = "ds1JdbcTemplate")
public JdbcTemplate ds1JdbcTemplate(@Qualifier("ds1DataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
//JdbcTemplate第二個ds2數(shù)據(jù)源
@Bean(name = "ds2JdbcTemplate")
public JdbcTemplate ds2JdbcTemplate(@Qualifier("ds2DataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}
5.分別配置兩個數(shù)據(jù)源的使用的包路徑
(注意換為自己的包路徑)
package com.cbd.findig.data.process.config;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
/**
* Mybatis主數(shù)據(jù)源ds1配置
* 多數(shù)據(jù)源配置依賴數(shù)據(jù)源配置
* @see DataSourceConfig
*/
@Configuration
@MapperScan(basePackages ="com.cbd.findig.data.process.mapper.**", sqlSessionTemplateRef = "ds1SqlSessionTemplate")
public class MybatisPlusConfig4ds1 {
//主數(shù)據(jù)源 ds1數(shù)據(jù)源
@Primary
@Bean("ds1SqlSessionFactory")
public SqlSessionFactory ds1SqlSessionFactory(@Qualifier("ds1DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
sqlSessionFactory.setDataSource(dataSource);
sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().
getResources("classpath*:com/cbd/findig/data/process/mapper/**/*.xml"));
return sqlSessionFactory.getObject();
}
@Primary
@Bean(name = "ds1TransactionManager")
public DataSourceTransactionManager ds1TransactionManager(@Qualifier("ds1DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Primary
@Bean(name = "ds1SqlSessionTemplate")
public SqlSessionTemplate ds1SqlSessionTemplate(@Qualifier("ds1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
package com.cbd.findig.data.process.config;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
/**
* Mybatis 第二個ds2數(shù)據(jù)源配置
* 多數(shù)據(jù)源配置依賴數(shù)據(jù)源配置
* @see DataSourceConfig
*/
@Configuration
@MapperScan(basePackages ="com.cbd.findig.data.process.mapperds2.**", sqlSessionTemplateRef = "ds2SqlSessionTemplate")
public class MybatisPlusConfig4ds2 {
//ds2數(shù)據(jù)源
@Bean("ds2SqlSessionFactory")
public SqlSessionFactory ds2SqlSessionFactory(@Qualifier("ds2DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
sqlSessionFactory.setDataSource(dataSource);
sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().
getResources("classpath*:com/cbd/findig/data/process/mapperds2/**/*.xml"));
return sqlSessionFactory.getObject();
}
//事務(wù)支持
@Bean(name = "ds2TransactionManager")
public DataSourceTransactionManager ds2TransactionManager(@Qualifier("ds2DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "ds2SqlSessionTemplate")
public SqlSessionTemplate ds2SqlSessionTemplate(@Qualifier("ds2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
java Timer 定時每天凌晨1點執(zhí)行任務(wù)
這篇文章主要介紹了java Timer 定時每天凌晨1點執(zhí)行任務(wù)的代碼,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-09-09
Java使用觀察者模式實現(xiàn)氣象局高溫預(yù)警功能示例
這篇文章主要介紹了Java使用觀察者模式實現(xiàn)氣象局高溫預(yù)警功能,結(jié)合完整實例形式分析了java觀察者模式實現(xiàn)氣象局高溫預(yù)警的相關(guān)接口定義、使用、功能操作技巧,并總結(jié)了其設(shè)計原則與適用場合,具有一定參考借鑒價值,需要的朋友可以參考下2018-04-04
SpringBoot學(xué)習(xí)之Json數(shù)據(jù)交互的方法
這篇文章主要介紹了SpringBoot學(xué)習(xí)之Json數(shù)據(jù)交互的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12
Java 8 動態(tài)類型語言Lambda表達式實現(xiàn)原理解析
Java 8支持動態(tài)語言,看到了很酷的Lambda表達式,對一直以靜態(tài)類型語言自居的Java,讓人看到了Java虛擬機可以支持動態(tài)語言的目標。接下來通過本文給大家介紹Java 8 動態(tài)類型語言Lambda表達式實現(xiàn)原理分析,需要的朋友可以參考下2017-02-02
Spring?Cloud?Alibaba?Nacos服務(wù)治理平臺服務(wù)注冊、RestTemplate實現(xiàn)微服務(wù)之間訪
這篇文章主要介紹了Spring?Cloud?Alibaba:Nacos服務(wù)治理平臺,服務(wù)注冊、RestTemplate實現(xiàn)微服務(wù)之間訪問,負載均衡訪問,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06
基于Spring Boot應(yīng)用ApplicationEvent案例場景
這篇文章主要介紹了基于Spring Boot應(yīng)用ApplicationEvent,利用Spring的機制發(fā)布ApplicationEvent和監(jiān)聽ApplicationEvent,需要的朋友可以參考下2023-03-03
java多線程編程之從線程返回數(shù)據(jù)的兩種方法
從線程中返回數(shù)據(jù)和向線程傳遞數(shù)據(jù)類似。也可以通過類成員以及回調(diào)函數(shù)來返回數(shù)據(jù)。但類成員在返回數(shù)據(jù)和傳遞數(shù)據(jù)時有一些區(qū)別,下面讓我們來看看它們區(qū)別在哪2014-01-01
java設(shè)計模式之裝飾器模式(Decorator)
這篇文章主要為大家詳細介紹了java設(shè)計模式之裝飾器模式Decorator,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01

