最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

這次要完成的是從一個(gè)數(shù)據(jù)庫中讀取數(shù)據(jù),然后再把數(shù)據(jù)插入到另一個(gè)數(shù)據(jù)庫中。在同一套項(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ù)源。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java中抽象類的作用及說明

    Java中抽象類的作用及說明

    這篇文章主要介紹了Java中抽象類的作用及說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • java的IO流詳細(xì)解讀

    java的IO流詳細(xì)解讀

    這篇文章主要介紹了java的IO流詳細(xì)解讀,具有一定借鑒價(jià)值,需要的朋友可以參考下。
    2017-12-12
  • 利用Java實(shí)現(xiàn)帶GUI的氣泡詩詞特效

    利用Java實(shí)現(xiàn)帶GUI的氣泡詩詞特效

    這篇文章主要為大家介紹了如何利用Java語言實(shí)現(xiàn)帶GUI的氣泡詩詞特效,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Java有一定幫助,感興趣的可以了解一下
    2022-08-08
  • 使用maven實(shí)現(xiàn)有關(guān)Jsoup簡單爬蟲的步驟

    使用maven實(shí)現(xiàn)有關(guān)Jsoup簡單爬蟲的步驟

    這篇文章主要介紹了使用maven實(shí)現(xiàn)有關(guān)Jsoup簡單爬蟲的步驟,文中附含詳細(xì)示例代碼,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2021-09-09
  • Java?OpenCV學(xué)習(xí)之Mat的基本操作詳解

    Java?OpenCV學(xué)習(xí)之Mat的基本操作詳解

    OpenCV用來存儲圖像,很多時(shí)候都會用到這個(gè)Mat方法。數(shù)字圖像可看做一個(gè)數(shù)值矩陣,?其中的每一個(gè)元素表明一個(gè)像素點(diǎn)。Mat在?OpenCV?中表示的是?N?維稠密矩陣,與稠密矩陣相對的是稀疏矩陣。本文將重點(diǎn)介紹OpenCV中Mat的一些基本操作,需要的可以參考一下
    2022-03-03
  • Spring Cloud下OAUTH2注銷的實(shí)現(xiàn)示例

    Spring Cloud下OAUTH2注銷的實(shí)現(xiàn)示例

    本篇文章主要介紹了Spring Cloud下OAUTH2注銷的實(shí)現(xiàn)示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-03-03
  • 淺談Spring Boot中Redis緩存還能這么用

    淺談Spring Boot中Redis緩存還能這么用

    這篇文章主要介紹了淺談Spring Boot中Redis緩存還能這么用,這種方式是Spring Cache提供的統(tǒng)一接口,實(shí)現(xiàn)既可以是Redis,也可以是Ehcache或者其他支持這種規(guī)范的緩存框架,感興趣的小伙伴們可以參考一下
    2019-06-06
  • Java getParameter()獲取數(shù)據(jù)為空的問題

    Java getParameter()獲取數(shù)據(jù)為空的問題

    這篇文章主要介紹了Java getParameter()獲取數(shù)據(jù)為空的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Springboot整合Netty自定義協(xié)議實(shí)現(xiàn)示例詳解

    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)

    解決CentOS7中運(yùn)行jar包報(bào)錯(cuò):xxx(Permission?denied)

    在實(shí)際工作我們經(jīng)常會在linux上運(yùn)行Spring boot編寫的微服務(wù)程序,下面這篇文章主要給大家介紹了關(guān)于如何解決CentOS7中運(yùn)行jar包報(bào)錯(cuò):xxx(Permission?denied)的相關(guān)資料,需要的朋友可以參考下
    2024-02-02

最新評論

桂阳县| 北京市| 马鞍山市| 政和县| 兰州市| 格尔木市| 温泉县| 泽普县| 布尔津县| 吴堡县| 汉川市| 德化县| 始兴县| 娄烦县| 广东省| 博乐市| 偏关县| 双桥区| 昌图县| 淮滨县| 苗栗县| 宁国市| 叙永县| 汉源县| 南投县| 西安市| 吴忠市| 合作市| 中超| 新竹市| 河东区| 孟村| 巴楚县| 惠东县| 凭祥市| 剑川县| 洛扎县| 横峰县| 浮山县| 五莲县| 栾城县|