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

Spring boot配置多數(shù)據(jù)源代碼實例

 更新時間:2020年07月08日 11:54:59   作者:阮帥  
這篇文章主要介紹了Spring boot配置多數(shù)據(jù)源代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

因項目需要在一個應(yīng)用里從兩個數(shù)據(jù)庫取數(shù),所以需要配置多數(shù)據(jù)源,網(wǎng)上找了好多方法才啟動成功,整理如下。注意兩個數(shù)據(jù)源的repository文件名不能相同,即使在不同的文件夾下,否則系統(tǒng)啟動會報錯。

配置文件

spring.datasource.primary.url=***
spring.datasource.primary.username=***
spring.datasource.primary.password=***
spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driver

spring.datasource.second.url=***
spring.datasource.second.username=***
spring.datasource.second.password=***
spring.datasource.second.driver-class-name=com.mysql.jdbc.Driver

通用數(shù)據(jù)源配置

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

/**
 * @author ruanshuai
 * @date 2020/5/13
 */

@Configuration
public class DataSourceConfig {

  /**
   * 第一個數(shù)據(jù)連接,默認(rèn)優(yōu)先級最高
   * @return
   */
  @Primary
  @Bean(name = "primaryDataSource") //數(shù)據(jù)源1配置名
  @Qualifier("primaryDataSource") //數(shù)據(jù)源1配置名
  @ConfigurationProperties(prefix="spring.datasource.primary") //見配置文件
  public DataSource PrimaryDataSource() {
    return DataSourceBuilder.create().type(DruidDataSource.class).build();
  }

  /**
   * 第二個數(shù)據(jù)源
   * @return
   */
  @Bean(name = "secondDataSource") //數(shù)據(jù)源2配置名
  @Qualifier("secondDataSource") //數(shù)據(jù)源2配置名
  @ConfigurationProperties(prefix="spring.datasource.second") //見配置文件
  public DataSource secondaryDataSource() {
    return DataSourceBuilder.create().type(DruidDataSource.class).build();
  }
}

數(shù)據(jù)源1配置

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ruanshuai
 * @date 2020/5/13
 */

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
    entityManagerFactoryRef="entityManagerFactoryPrimary",
    transactionManagerRef="transactionManagerPrimary",
    basePackages= { "***此處為數(shù)據(jù)源1 repository的存放文件夾***" })
public class PrimaryConfig {


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

  @Primary
  @Bean(name = "entityManagerPrimary")
  public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
    return entityManagerFactoryPrimary(builder).getObject().createEntityManager();
  }

  @Primary
  @Bean(name = "entityManagerFactoryPrimary")
  public LocalContainerEntityManagerFactoryBean entityManagerFactoryPrimary (EntityManagerFactoryBuilder builder) {
    return builder
        .dataSource(primaryDataSource)
        .properties(getVendorProperties())
        .packages("***實體類所在文件夾,兩個數(shù)據(jù)源的實體類可相同***")
        .persistenceUnit("primaryPersistenceUnit")
        .build();
  }



  private Map<String, String> getVendorProperties() {
    Map<String, String> jpaProperties = new HashMap<>(16);
    jpaProperties.put("hibernate.hbm2ddl.auto", "update");
    jpaProperties.put("hibernate.show_sql", System.getProperty("spring.jpa.show-sql"));
    jpaProperties.put("hibernate.dialect", System.getProperty("spring.jpa.properties.hibernate.dialect"));
    jpaProperties.put("hibernate.current_session_context_class", "org.springframework.orm.hibernate5.SpringSessionContext");
    return jpaProperties;
  }

  @Primary
  @Bean(name = "transactionManagerPrimary")
  public PlatformTransactionManager transactionManagerPrimary(EntityManagerFactoryBuilder builder) {
    return new JpaTransactionManager(entityManagerFactoryPrimary(builder).getObject());
  }
}

數(shù)據(jù)源2配置

import org.omg.CORBA.Environment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManager;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ruanshuai
 * @date 2020/5/13
 */

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
    //實體管理
    entityManagerFactoryRef="entityManagerFactorySecond",
    //事務(wù)管理
    transactionManagerRef="transactionManagerSecond",
    //實體掃描,設(shè)置Repository所在位置
    basePackages= { "***此處為數(shù)據(jù)源1 repository的存放文件夾***" })
public class SecondConfig {

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


  @Bean(name = "entityManagerSecond")
  public EntityManager entityManager(EntityManagerFactoryBuilder builder) {
    return entityManagerFactorySecond(builder).getObject().createEntityManager();
  }

  @Bean(name = "entityManagerFactorySecond")
  public LocalContainerEntityManagerFactoryBean entityManagerFactorySecond (EntityManagerFactoryBuilder builder) {
    return builder
        .dataSource(secondDataSource)
        .properties(getVendorProperties())
        .packages("***實體類所在文件夾,兩個數(shù)據(jù)源的實體類可相同***")
        .persistenceUnit("secondPersistenceUnit")
        .build();
  }

  private Map<String, String> getVendorProperties() {
    Map<String, String> jpaProperties = new HashMap<>(16);
    jpaProperties.put("hibernate.hbm2ddl.auto", "update");
    jpaProperties.put("hibernate.show_sql", System.getProperty("spring.jpa.show-sql"));
    jpaProperties.put("hibernate.dialect", System.getProperty("spring.jpa.properties.hibernate.dialect"));
    jpaProperties.put("hibernate.current_session_context_class", "org.springframework.orm.hibernate5.SpringSessionContext");
    return jpaProperties;
  }

  @Bean(name = "transactionManagerSecond")
  PlatformTransactionManager transactionManagerSecond(EntityManagerFactoryBuilder builder) {
    return new JpaTransactionManager(entityManagerFactorySecond(builder).getObject());
  }
}

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

相關(guān)文章

  • 簡單了解JAVA中類、實例與Class對象

    簡單了解JAVA中類、實例與Class對象

    這篇文章主要介紹了簡單了解JAVA中類、實例與Class對象,類是面向?qū)ο缶幊陶Z言的一個重要概念,它是對一項事物的抽象概括,可以包含該事物的一些屬性定義,以及操作屬性的方法,需要的朋友可以參考下
    2019-06-06
  • Spring?Data?Jpa?復(fù)雜查詢方式總結(jié)(多表關(guān)聯(lián)及自定義分頁)

    Spring?Data?Jpa?復(fù)雜查詢方式總結(jié)(多表關(guān)聯(lián)及自定義分頁)

    這篇文章主要介紹了Spring?Data?Jpa?復(fù)雜查詢方式總結(jié)(多表關(guān)聯(lián)及自定義分頁),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • springcloud引入spring-cloud-starter-openfeign失敗的解決

    springcloud引入spring-cloud-starter-openfeign失敗的解決

    這篇文章主要介紹了springcloud?引入spring-cloud-starter-openfeign失敗的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • 使用maven命令安裝jar包到本地倉庫的方法步驟

    使用maven命令安裝jar包到本地倉庫的方法步驟

    這篇文章主要介紹了使用maven命令安裝jar包到本地倉庫的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • Java數(shù)據(jù)結(jié)構(gòu)專題解析之棧和隊列的實現(xiàn)

    Java數(shù)據(jù)結(jié)構(gòu)專題解析之棧和隊列的實現(xiàn)

    從數(shù)據(jù)結(jié)構(gòu)的定義看,棧和隊列也是一種線性表。其不同之處在于棧和隊列的相關(guān)運算具有特殊性,只是線性表相關(guān)運算的一個子集。更準(zhǔn)確的說,一般線性表的插入、刪除運算不受限制,而棧和隊列上的插入刪除運算均受某種特殊限制。因此,棧和隊列也稱作操作受限的線性表
    2021-10-10
  • 深入解析Java編程中方法的參數(shù)傳遞

    深入解析Java編程中方法的參數(shù)傳遞

    這篇文章主要介紹了Java編程中方法的參數(shù)傳遞,是Java入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-10-10
  • 使用Java獲取linux和window序列號

    使用Java獲取linux和window序列號

    這篇文章主要為大家詳細介紹了如何使用Java獲取Windows和Linux系統(tǒng)上的CPU序列號、磁盤、mac地址等信息,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-01-01
  • 關(guān)于Java的二叉樹、紅黑樹、B+樹詳解

    關(guān)于Java的二叉樹、紅黑樹、B+樹詳解

    這篇文章主要介紹了關(guān)于Java的二叉樹、紅黑樹、B+樹詳解,能同時具備數(shù)組查找快的優(yōu)點以及鏈表插入和刪除快的優(yōu)點的數(shù)據(jù)結(jié)構(gòu)就是樹,需要的朋友可以參考下
    2023-05-05
  • Opencv實現(xiàn)身份證OCR識別的示例詳解

    Opencv實現(xiàn)身份證OCR識別的示例詳解

    這篇文章主要為大家詳細介紹了如何使用Opencv實現(xiàn)身份證OCR識別功能,文中的示例代碼講解詳細,具有一定的學(xué)習(xí)價值,感興趣的小伙伴可以跟隨小編一起了解一下
    2024-03-03
  • spring配置文件中util:properties和context:property-placeholder用法

    spring配置文件中util:properties和context:property-placeholder用法

    這篇文章主要介紹了spring配置文件中util:properties和context:property-placeholder用法,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01

最新評論