欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Mybatis多數(shù)據(jù)源切換實(shí)現(xiàn)代碼

 更新時(shí)間:2020年10月15日 09:31:49   作者:ayueC  
這篇文章主要介紹了Mybatis多數(shù)據(jù)源切換實(shí)現(xiàn)代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

這次要完成的是從一個(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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Spring?Boot項(xiàng)目如何使用Maven打包并帶上依賴

    Spring?Boot項(xiàng)目如何使用Maven打包并帶上依賴

    在這篇博客中,介紹如何使用Maven將Spring?Boot項(xiàng)目及其依賴項(xiàng)打包成一個(gè)可執(zhí)行的jar文件。我們將使用Spring?Boot的spring-boot-maven-plugin插件來(lái)完成這個(gè)任務(wù),感興趣的朋友跟隨小編一起看看吧
    2023-06-06
  • Java正則校驗(yàn)密碼至少包含字母數(shù)字特殊符號(hào)中的2種實(shí)例代碼

    Java正則校驗(yàn)密碼至少包含字母數(shù)字特殊符號(hào)中的2種實(shí)例代碼

    正則表達(dá)式驗(yàn)證密碼功能在項(xiàng)目中經(jīng)常被使用到,但是很多朋友還是不大會(huì)使用密碼正則表達(dá)式進(jìn)行驗(yàn)證,下面這篇文章主要給大家介紹了關(guān)于Java正則校驗(yàn)密碼至少包含字母數(shù)字特殊符號(hào)中2種的相關(guān)資料,需要的朋友可以參考下
    2022-08-08
  • mybatis打印SQL,并顯示參數(shù)的實(shí)例

    mybatis打印SQL,并顯示參數(shù)的實(shí)例

    這篇文章主要介紹了mybatis打印SQL,并顯示參數(shù)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-12-12
  • java中Iterator和ListIterator實(shí)例詳解

    java中Iterator和ListIterator實(shí)例詳解

    這篇文章主要介紹了java中Iterator和ListIterator實(shí)例詳解,具有一定借鑒價(jià)值,需要的朋友可以參考下。
    2017-12-12
  • SpringCloud Sleuth實(shí)現(xiàn)分布式請(qǐng)求鏈路跟蹤流程詳解

    SpringCloud Sleuth實(shí)現(xiàn)分布式請(qǐng)求鏈路跟蹤流程詳解

    這篇文章主要介紹了SpringCloud Sleuth實(shí)現(xiàn)分布式請(qǐng)求鏈路跟蹤流程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧
    2022-11-11
  • java解一個(gè)比較特殊的數(shù)組合并題

    java解一個(gè)比較特殊的數(shù)組合并題

    這篇文章主要介紹了java解一個(gè)比較特殊的數(shù)組合并題,需要的朋友可以參考下
    2014-06-06
  • IDEA配置Tomcat后,控制臺(tái)tomcat?catalina?log出現(xiàn)亂碼問(wèn)題

    IDEA配置Tomcat后,控制臺(tái)tomcat?catalina?log出現(xiàn)亂碼問(wèn)題

    本文介紹了如何通過(guò)設(shè)置Tomcat和IDEA的編碼格式來(lái)解決編碼問(wèn)題,首先嘗試修改Tomcat的logging.properties文件中的編碼設(shè)置,如果未解決問(wèn)題,則調(diào)整IDEA的編碼設(shè)置,通過(guò)修改vmoptions文件來(lái)全局設(shè)置IDEA的編碼格式,作者分享了個(gè)人成功解決問(wèn)題的方法和步驟,供其他開(kāi)發(fā)者參考
    2024-09-09
  • springboot logback如何從apollo配置中心讀取變量

    springboot logback如何從apollo配置中心讀取變量

    這篇文章主要介紹了springboot logback如何從apollo配置中心讀取變量的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Java并發(fā)編程之ConcurrentLinkedQueue解讀

    Java并發(fā)編程之ConcurrentLinkedQueue解讀

    這篇文章主要介紹了Java并發(fā)編程之ConcurrentLinkedQueue解讀,非阻塞的實(shí)現(xiàn)方式則可以使用循環(huán)CAS的方式來(lái)實(shí)現(xiàn),而ConcurrentLinkedQueue就是juc包中自帶的經(jīng)典非堵塞方式實(shí)現(xiàn)的工具類,需要的朋友可以參考下
    2023-12-12
  • SpringBoot入門編寫(xiě)第一個(gè)程序Helloworld

    SpringBoot入門編寫(xiě)第一個(gè)程序Helloworld

    這篇文章是Springboot入門篇,來(lái)教大家編寫(xiě)第一個(gè)Springboot程序Helloworld,文中附有詳細(xì)的示例代碼,有需要的同學(xué)可以借鑒參考下
    2021-09-09

最新評(píng)論