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中的值可以通過@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,然后通過注解@MapperScan指定掃描指定的mapper接口時(shí)用指定的SqlSessionFactory進(jìn)行連接構(gòu)建,從而實(shí)現(xiàn)了多數(shù)據(jù)源。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. MYSQL(電話號(hào)碼,身份證)數(shù)據(jù)脫敏的實(shí)現(xiàn)2. 使用mysql記錄從url返回的http GET請(qǐng)求數(shù)據(jù)操作3. DB2中多種常用功能的解決方法(1)4. MySQL中常見的幾種日志匯總5. ASP.NET應(yīng)用中緩存Oracle數(shù)據(jù)6. Oracle建表與創(chuàng)建序列詳細(xì)實(shí)例7. mariadb的主從復(fù)制、主主復(fù)制、半同步復(fù)制配置詳解8. SQL Server 2005-如何在SQL Server用戶自訂函數(shù)中調(diào)用GetDate()函數(shù)9. mysql 模糊查詢 concat()的用法詳解10. MySQL之高可用集群部署及故障切換實(shí)現(xiàn)
