Mybatis多數(shù)據(jù)源切換實(shí)現(xiàn)代碼
這次要完成的是從一個(gè)數(shù)據(jù)庫(kù)中讀取數(shù)據(jù),然后再把數(shù)據(jù)插入到另一個(gè)數(shù)據(jù)庫(kù)中。在同一套項(xiàng)目代碼中要完成這個(gè)操作,就不可避免的涉及到了多數(shù)據(jù)源。本文即介紹在mybatis中完成多數(shù)據(jù)源的切換相關(guān)內(nèi)容
指定數(shù)據(jù)源一
@Configuration
// 掃描 Mapper 接口并容器管理
@MapperScan(basePackages = MasterDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "masterSqlSessionFactory")
public class MasterDataSourceConfig {
// 精確到 master 目錄,以便跟其他數(shù)據(jù)源隔離
static final String PACKAGE = "com.datareach.kafka.dao.master";
static final String MAPPER_LOCATION = "classpath:mapper/master/*.xml";
//application.yml中的值可以通過(guò)@Value注解進(jìn)行讀取
@Value("${master.datasource.url}")
private String url;
@Value("${master.datasource.username}")
private String user;
@Value("${master.datasource.password}")
private String password;
@Value("${master.datasource.driver-class-name}")
private String driverClass;
@Bean(name = "masterDataSource")
@Primary
public DataSource masterDataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driverClass);
dataSource.setUrl(url);
dataSource.setUsername(user);
dataSource.setPassword(password);
return dataSource;
}
@Bean(name = "masterTransactionManager")
@Primary
public DataSourceTransactionManager masterTransactionManager() {
return new DataSourceTransactionManager(masterDataSource());
}
@Bean(name = "masterSqlSessionFactory")
@Primary
public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource masterDataSource)
throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(masterDataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources(MasterDataSourceConfig.MAPPER_LOCATION));
return sessionFactory.getObject();
}
}
數(shù)據(jù)源一的相關(guān)配置
# master 數(shù)據(jù)源配置 master: datasource: url: jdbc:postgresql://localhost:5432/postgres?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8 driver-class-name: org.postgresql.Driver username: product password: initial-size: 1 min-idle: 1 max-active: 20 test-on-borrow: true max-wait: 60000 time-between-eviction-runs-millis: 60000 min-evictable-idle-time-millis: 300000 validation-query: SELECT 1 FROM DUAL test-While-Idle: true test-on-return: false pool-prepared-statements: false max-pool-prepared-statement-per-connection-size: 20 filters: stat,wall,log4j,config
指定數(shù)據(jù)源二
@Configuration
// 掃描 Mapper 接口并容器管理
@MapperScan(basePackages = SecondDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "secondSqlSessionFactory")
public class SecondDataSourceConfig {
// 精確到 cluster 目錄,以便跟其他數(shù)據(jù)源隔離
static final String PACKAGE = "com.datareach.kafka.dao.secondary";
static final String MAPPER_LOCATION = "classpath:mapper/secondary/*.xml";
@Value("${second.datasource.url}")
private String url;
@Value("${second.datasource.username}")
private String user;
@Value("${second.datasource.password}")
private String password;
@Value("${second.datasource.driver-class-name}")
private String driverClass;
@Bean(name = "secondDataSource")
public DataSource clusterDataSource() {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driverClass);
dataSource.setUrl(url);
dataSource.setUsername(user);
dataSource.setPassword(password);
return dataSource;
}
@Bean(name = "secondTransactionManager")
public DataSourceTransactionManager clusterTransactionManager() {
return new DataSourceTransactionManager(clusterDataSource());
}
@Bean(name = "secondSqlSessionFactory")
public SqlSessionFactory clusterSqlSessionFactory(@Qualifier("secondDataSource") DataSource clusterDataSource)
throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(clusterDataSource);
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources(SecondDataSourceConfig.MAPPER_LOCATION));
return sessionFactory.getObject();
}
}
數(shù)據(jù)源二的相關(guān)配置
second: datasource: url: jdbc:mysql://localhost:40000/PG_Data?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8 username: root password: driver-class-name: com.mysql.jdbc.Driver max-idle: 10 max-wait: 10000 min-idle: 5 initial-size: 5
其實(shí)就是實(shí)例化了兩個(gè)SqlSessionFactory——masterSqlSessionFactory和secondSqlSessionFactory,然后通過(guò)注解@MapperScan指定掃描指定的mapper接口時(shí)用指定的SqlSessionFactory進(jìn)行連接構(gòu)建,從而實(shí)現(xiàn)了多數(shù)據(jù)源。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Spring+Mybatis動(dòng)態(tài)切換數(shù)據(jù)源的方法
- Spring與Mybatis相結(jié)合實(shí)現(xiàn)多數(shù)據(jù)源切換功能
- spring boot+mybatis 多數(shù)據(jù)源切換(實(shí)例講解)
- Spring + Mybatis 項(xiàng)目實(shí)現(xiàn)動(dòng)態(tài)切換數(shù)據(jù)源實(shí)例詳解
- 關(guān)于Spring3 + Mybatis3整合時(shí)多數(shù)據(jù)源動(dòng)態(tài)切換的問(wèn)題
- SpringMVC Mybatis配置多個(gè)數(shù)據(jù)源并切換代碼詳解
- spring boot + mybatis實(shí)現(xiàn)動(dòng)態(tài)切換數(shù)據(jù)源實(shí)例代碼
- SpringBoot Mybatis動(dòng)態(tài)數(shù)據(jù)源切換方案實(shí)現(xiàn)過(guò)程
- Spring AOP如何實(shí)現(xiàn)注解式的Mybatis多數(shù)據(jù)源切換詳解
- mybatis多數(shù)據(jù)源動(dòng)態(tài)切換的完整步驟
相關(guān)文章
利用Java實(shí)現(xiàn)帶GUI的氣泡詩(shī)詞特效
這篇文章主要為大家介紹了如何利用Java語(yǔ)言實(shí)現(xiàn)帶GUI的氣泡詩(shī)詞特效,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Java有一定幫助,感興趣的可以了解一下2022-08-08
使用maven實(shí)現(xiàn)有關(guān)Jsoup簡(jiǎn)單爬蟲的步驟
這篇文章主要介紹了使用maven實(shí)現(xiàn)有關(guān)Jsoup簡(jiǎn)單爬蟲的步驟,文中附含詳細(xì)示例代碼,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-09-09
Java?OpenCV學(xué)習(xí)之Mat的基本操作詳解
OpenCV用來(lái)存儲(chǔ)圖像,很多時(shí)候都會(huì)用到這個(gè)Mat方法。數(shù)字圖像可看做一個(gè)數(shù)值矩陣,?其中的每一個(gè)元素表明一個(gè)像素點(diǎn)。Mat在?OpenCV?中表示的是?N?維稠密矩陣,與稠密矩陣相對(duì)的是稀疏矩陣。本文將重點(diǎn)介紹OpenCV中Mat的一些基本操作,需要的可以參考一下2022-03-03
Spring Cloud下OAUTH2注銷的實(shí)現(xiàn)示例
本篇文章主要介紹了Spring Cloud下OAUTH2注銷的實(shí)現(xiàn)示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
Java getParameter()獲取數(shù)據(jù)為空的問(wèn)題
這篇文章主要介紹了Java getParameter()獲取數(shù)據(jù)為空的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Springboot整合Netty自定義協(xié)議實(shí)現(xiàn)示例詳解
這篇文章主要為大家介紹了Springboot整合Netty自定義協(xié)議實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
解決CentOS7中運(yùn)行jar包報(bào)錯(cuò):xxx(Permission?denied)
在實(shí)際工作我們經(jīng)常會(huì)在linux上運(yùn)行Spring boot編寫的微服務(wù)程序,下面這篇文章主要給大家介紹了關(guān)于如何解決CentOS7中運(yùn)行jar包報(bào)錯(cuò):xxx(Permission?denied)的相關(guān)資料,需要的朋友可以參考下2024-02-02

