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

springboot2+mybatis多種方式實(shí)現(xiàn)多數(shù)據(jù)配置方法

 更新時(shí)間:2020年03月30日 14:21:34   作者:落孤秋葉  
這篇文章主要介紹了springboot2+mybatis多種方式實(shí)現(xiàn)多數(shù)據(jù)配置方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

業(yè)務(wù)系統(tǒng)復(fù)雜程度增加,為了解決數(shù)據(jù)庫(kù)I/O瓶頸,很自然會(huì)進(jìn)行拆庫(kù)拆表分服務(wù)來(lái)應(yīng)對(duì)。這就會(huì)出現(xiàn)一個(gè)系統(tǒng)中可能會(huì)訪問(wèn)多處數(shù)據(jù)庫(kù),需要配置多個(gè)數(shù)據(jù)源。

第一種場(chǎng)景:項(xiàng)目服務(wù)從其它多處數(shù)據(jù)庫(kù)取基礎(chǔ)數(shù)據(jù)進(jìn)行業(yè)務(wù)處理,因此各庫(kù)之間不會(huì)出現(xiàn)重表等情況。

第二種場(chǎng)景:為了減輕寫(xiě)入壓力進(jìn)行讀寫(xiě)分庫(kù),讀走從庫(kù),寫(xiě)為主庫(kù)。此種表名等信息皆為一致。

第三種場(chǎng)景:以上兩種皆有。對(duì)于某些業(yè)務(wù)需要大數(shù)據(jù)量的匯總統(tǒng)計(jì),希望不影響正常業(yè)務(wù)必須走從庫(kù)(表信息一致),某些配置信息不存在讀寫(xiě)壓力,出現(xiàn)不分庫(kù)(表信息不一致)

 項(xiàng)目源代碼:

https://github.com/zzsong/springboot-multiple-datasource.git

有三個(gè)目錄:

one:
    直接使用多@Bean配置,@MapperScan來(lái)路徑區(qū)分讀何庫(kù)

two:
    使用注解的方式來(lái)標(biāo)識(shí)走何dataSource,AOP攔截注入動(dòng)態(tài)數(shù)據(jù)源   

third:
    使用spring的Bean命名策略進(jìn)行區(qū)分?jǐn)?shù)據(jù)來(lái)源

項(xiàng)目技術(shù)選型: springBoot2.2.5 + mybatis + druid + mysql

先看主要的pom包

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.5.RELEASE</version>
    <relativePath/> 
  </parent>

        <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jdbc</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jdbc</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>2.1.2</version>
    </dependency>

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.19</version>
    </dependency>
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid-spring-boot-starter</artifactId>
      <version>1.1.21</version>
    </dependency>

application.yml

spring:
 datasource:
  druid:
   core:
    url: jdbc:mysql:///kc_core?characterEncoding=utf-8&serverTimezone=Asia/Shanghai
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
   schedule:
    url: jdbc:mysql:///kc_schedule?characterEncoding=utf-8&serverTimezone=Asia/Shanghai
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource

mysql新版本必須帶有serverTimezone,不然會(huì)報(bào)連接異常。

第一種:通過(guò)@MapperScans 掃描匹配相關(guān)的數(shù)據(jù)源

@Configuration
@MapperScans({
    @MapperScan(basePackages = "com.zss.one.mapper.core", sqlSessionTemplateRef = "coreSqlSessionTemplate",sqlSessionFactoryRef = "coreSqlSessionFactory"),
    @MapperScan(basePackages = "com.zss.one.mapper.schedule", sqlSessionTemplateRef = "scheduleSqlSessionTemplate",sqlSessionFactoryRef = "scheduleSqlSessionFactory")
})
public class MybatisOneConfig {

  @Bean
  @ConfigurationProperties(prefix = "spring.datasource.druid.core")
  public DataSource coreDataSource(){
    return DruidDataSourceBuilder.create().build();
  }

  @Bean
  public SqlSessionFactory coreSqlSessionFactory(@Qualifier("coreDataSource") DataSource coreDataSource) throws Exception {
    SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
    sessionFactory.setDataSource(coreDataSource);
    sessionFactory.getObject().getConfiguration().setJdbcTypeForNull(null);
    sessionFactory.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
    return sessionFactory.getObject();
  }

  @Bean
  public SqlSessionTemplate coreSqlSessionTemplate(@Qualifier("coreSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
    return new SqlSessionTemplate(sqlSessionFactory);
  }

  //======schedule========
  @Bean
  @ConfigurationProperties(prefix = "spring.datasource.druid.schedule")
  public DataSource scheduleDataSource(){
    return DruidDataSourceBuilder.create().build();
  }

  @Bean
  public SqlSessionFactory scheduleSqlSessionFactory(@Qualifier("scheduleDataSource") DataSource coreDataSource) throws Exception {
    SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
    sessionFactory.setDataSource(coreDataSource);
    sessionFactory.getObject().getConfiguration().setJdbcTypeForNull(null);
    sessionFactory.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
    return sessionFactory.getObject();
  }

  @Bean
  public SqlSessionTemplate scheduleSqlSessionTemplate(@Qualifier("scheduleSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
    return new SqlSessionTemplate(sqlSessionFactory);
  }
}

第二種是動(dòng)態(tài)數(shù)據(jù)源模式,通過(guò)AOP切入注解引導(dǎo)使用何數(shù)據(jù)源。用自定義注解@interface來(lái)標(biāo)識(shí)方法走對(duì)應(yīng)的數(shù)據(jù)源。

注意事項(xiàng):類中的方法再調(diào)用帶數(shù)據(jù)源的方法,不能被AOP切入

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface TargetDataSource {
  String value();
}

extends spring的動(dòng)態(tài)DataSource路由來(lái)匹配

public class DynamicDataSource extends AbstractRoutingDataSource {

  @Override
  protected Object determineCurrentLookupKey() {
    return DataSourceContextRouting.getDataSourceName();
  }
}
@Configuration
//@EnableConfigurationProperties(MybatisProperties.class)//不要使用此公共配置,Configuration會(huì)破壞相關(guān)dataSource的配置
@MapperScan("com.zss.two.mapper")
public class MybatisConfig {

  @Bean
  @ConfigurationProperties(prefix = "spring.datasource.druid.core")
  public DataSource coreDataSource() {
    return DruidDataSourceBuilder.create().build();
  }

  @Bean
  @ConfigurationProperties(prefix = "spring.datasource.druid.schedule")
  public DataSource scheduleDataSource() {
    return DruidDataSourceBuilder.create().build();
  }

  @Autowired
  @Qualifier("coreDataSource")
  private DataSource coreDataSource;

  @Autowired
  @Qualifier("scheduleDataSource")
  private DataSource scheduleDataSource;

  @Bean
  public DynamicDataSource dataSource() {
    Map<Object, Object> targetDataSources = new HashMap<>();
    targetDataSources.put(DataSourceConstants.CORE_DATA_SOURCE, coreDataSource);
    targetDataSources.put(DataSourceConstants.SCHEDULE_DATA_SOURCE, scheduleDataSource);

    DynamicDataSource dataSource = new DynamicDataSource();

    //設(shè)置數(shù)據(jù)源映射
    dataSource.setTargetDataSources(targetDataSources);
////    設(shè)置默認(rèn)數(shù)據(jù)源,當(dāng)無(wú)法映射到數(shù)據(jù)源時(shí)會(huì)使用默認(rèn)數(shù)據(jù)源
    dataSource.setDefaultTargetDataSource(coreDataSource);
    dataSource.afterPropertiesSet();
    return dataSource;
  }
  /**
   * 根據(jù)數(shù)據(jù)源創(chuàng)建SqlSessionFactory
   */
  @Bean
  public SqlSessionFactory sqlSessionFactory(DynamicDataSource dataSource) throws Exception {
    SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
    sessionFactory.setDataSource(dataSource);
    sessionFactory.getObject().getConfiguration().setJdbcTypeForNull(null);
    sessionFactory.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
    return sessionFactory.getObject();
  }

  @Bean
  public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) throws Exception {
    return new SqlSessionTemplate(sqlSessionFactory);
  }

第三種,自定義Bean命名策略,按beanName進(jìn)行自動(dòng)匹配使用數(shù)據(jù)源

@Component
public class CoreBeanNameGenerator implements BeanNameGenerator {
  @Override
  public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
    return "core"+ ClassUtils.getShortName(definition.getBeanClassName());
  }
}

@Component
public class ScheduleBeanNameGenerator implements BeanNameGenerator {
  @Override
  public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
    return "schedule"+ ClassUtils.getShortName(definition.getBeanClassName());
  }
}

使用mybatis MapperScannerConfigurer自動(dòng)掃描,將Mapper接口生成注入到spring

  @Bean
  public MapperScannerConfigurer coreMapperScannerConfig(CoreBeanNameGenerator coreBeanNameGenerator){
    MapperScannerConfigurer configurer = new MapperScannerConfigurer();
    configurer.setNameGenerator(coreBeanNameGenerator);
    configurer.setBasePackage("com.zss.third.mapper.core,com.zss.third.mapper.order");
    configurer.setSqlSessionFactoryBeanName("coreSqlSessionFactory");
    configurer.setSqlSessionTemplateBeanName("coreSqlSessionTemplate");
    return configurer;
  }

  @Bean
  public MapperScannerConfigurer scheduleMapperScannerConfig(ScheduleBeanNameGenerator scheduleBeanNameGenerator){
    MapperScannerConfigurer configurer = new MapperScannerConfigurer();
    configurer.setNameGenerator(scheduleBeanNameGenerator);
    configurer.setBasePackage("com.zss.third.mapper.schedule,com.zss.third.mapper.order");
    configurer.setSqlSessionFactoryBeanName("scheduleSqlSessionFactory");
    configurer.setSqlSessionTemplateBeanName("scheduleSqlSessionTemplate");
    return configurer;
  }

到此,三種多數(shù)據(jù)源匹配主要點(diǎn)介紹完,詳細(xì)直接下載github項(xiàng)目。 在resources/db含有相關(guān)測(cè)試表及數(shù)據(jù)腳本。

到此這篇關(guān)于springboot2+mybatis多種方式實(shí)現(xiàn)多數(shù)據(jù)配置方法的文章就介紹到這了,更多相關(guān)springboot2+mybatis 多數(shù)據(jù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • java加載properties文件的六種方法總結(jié)

    java加載properties文件的六種方法總結(jié)

    這篇文章主要介紹了java加載properties文件的六種方法總結(jié)的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • 解決JDK21中用不了TimeUtild問(wèn)題

    解決JDK21中用不了TimeUtild問(wèn)題

    在使用TimeUtil時(shí),可能因?yàn)镮DE版本不兼容導(dǎo)致問(wèn)題,升級(jí)IDEA到2023.2以上版本可解決此問(wèn)題,詳細(xì)步驟可以通過(guò)評(píng)論區(qū)索取安裝包或直接從官網(wǎng)下載,分享個(gè)人經(jīng)驗(yàn),希望對(duì)大家有幫助
    2024-10-10
  • SpringBoot集成vue的開(kāi)發(fā)解決方案

    SpringBoot集成vue的開(kāi)發(fā)解決方案

    這篇文章主要介紹了SpringBoot集成vue的開(kāi)發(fā)解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • Spring實(shí)現(xiàn)動(dòng)態(tài)切換多數(shù)據(jù)源的解決方案

    Spring實(shí)現(xiàn)動(dòng)態(tài)切換多數(shù)據(jù)源的解決方案

    這篇文章主要給大家介紹了Spring實(shí)現(xiàn)動(dòng)態(tài)切換多數(shù)據(jù)源的解決方案,文中給出了詳細(xì)的介紹和示例代碼,相信對(duì)大家的理解和學(xué)習(xí)具有一定的參考借鑒價(jià)值,有需要的朋友可以參考學(xué)習(xí),下面來(lái)一起看看吧。
    2017-01-01
  • Java自動(dòng)生成編號(hào)的方法步驟

    Java自動(dòng)生成編號(hào)的方法步驟

    在新增數(shù)據(jù)時(shí),往往需要自動(dòng)生成編號(hào),本文主要介紹了Java自動(dòng)生成編號(hào)的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • Maven Repository倉(cāng)庫(kù)的具體使用

    Maven Repository倉(cāng)庫(kù)的具體使用

    本文主要介紹了Maven Repository倉(cāng)庫(kù)的具體使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • SpringBoot最新定時(shí)任務(wù)的7種實(shí)現(xiàn)方案

    SpringBoot最新定時(shí)任務(wù)的7種實(shí)現(xiàn)方案

    在現(xiàn)代應(yīng)用中,定時(shí)任務(wù)是一個(gè)非常常見(jiàn)的需求,本文將通過(guò)7種方式講解如何在SpringBoot中實(shí)現(xiàn)定時(shí)任務(wù),包括使用@Scheduled注解、ScheduledExecutorService、Quartz、SpringTaskScheduler、Redis、XXL-JOB和Elastic-Job等,各有優(yōu)缺點(diǎn),選擇時(shí)應(yīng)根據(jù)實(shí)際需求進(jìn)行考慮
    2024-12-12
  • Java ThreadLocal原理解析以及應(yīng)用場(chǎng)景分析案例詳解

    Java ThreadLocal原理解析以及應(yīng)用場(chǎng)景分析案例詳解

    這篇文章主要介紹了Java ThreadLocal原理解析以及應(yīng)用場(chǎng)景分析案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • 使用Springboot封裝一個(gè)自適配的數(shù)據(jù)單位轉(zhuǎn)換工具類

    使用Springboot封裝一個(gè)自適配的數(shù)據(jù)單位轉(zhuǎn)換工具類

    我們?cè)诮邮涨芭_(tái)傳輸?shù)臄?shù)據(jù)時(shí),往往SpringBoot使用內(nèi)置的數(shù)據(jù)類型轉(zhuǎn)換器把我們提交的數(shù)據(jù)自動(dòng)封裝成對(duì)象等類型,下面這篇文章主要給大家介紹了關(guān)于使用Springboot封裝一個(gè)自適配的數(shù)據(jù)單位轉(zhuǎn)換工具類的相關(guān)資料,需要的朋友可以參考下
    2023-03-03
  • Java隊(duì)列篇之實(shí)現(xiàn)數(shù)組模擬隊(duì)列及可復(fù)用環(huán)形隊(duì)列詳解

    Java隊(duì)列篇之實(shí)現(xiàn)數(shù)組模擬隊(duì)列及可復(fù)用環(huán)形隊(duì)列詳解

    像棧一樣,隊(duì)列(queue)也是一種線性表,它的特性是先進(jìn)先出,插入在一端,刪除在另一端。就像排隊(duì)一樣,剛來(lái)的人入隊(duì)(push)要排在隊(duì)尾(rear),每次出隊(duì)(pop)的都是隊(duì)首(front)的人
    2021-10-10

最新評(píng)論

余干县| 邯郸市| 武义县| 民和| 林州市| 昌黎县| 铜鼓县| 宁南县| 比如县| 江华| 江北区| 河西区| 康保县| 康马县| 田东县| 互助| 元朗区| 汉中市| 江阴市| 罗源县| 普洱| 休宁县| 榆树市| 林芝县| 津南区| 陇南市| 长武县| 郧西县| 泰兴市| 加查县| 富阳市| 吴旗县| 棋牌| 长葛市| 南昌县| 靖宇县| 长顺县| 沁水县| 伊宁市| 波密县| 房产|