91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Spring boot配置多數據源的方法

發布時間:2020-07-09 09:47:44 來源:億速云 閱讀:205 作者:清晨 欄目:開發技術

不懂Spring boot配置多數據源的方法?其實想解決這個問題也不難,下面讓小編帶著大家一起學習怎么去解決,希望大家閱讀完這篇文章后大所收獲。

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

配置文件

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

通用數據源配置

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 {

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

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

數據源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= { "***此處為數據源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("***實體類所在文件夾,兩個數據源的實體類可相同***")
        .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());
  }
}

數據源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",
    //事務管理
    transactionManagerRef="transactionManagerSecond",
    //實體掃描,設置Repository所在位置
    basePackages= { "***此處為數據源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("***實體類所在文件夾,兩個數據源的實體類可相同***")
        .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());
  }
}

感謝你能夠認真閱讀完這篇文章,希望小編分享Spring boot配置多數據源的方法內容對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,遇到問題就找億速云,詳細的解決方法等著你來學習!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

金沙县| 浏阳市| 会泽县| 宜兰市| 德兴市| 岢岚县| 杭锦旗| 山西省| 依安县| 左贡县| 溧阳市| 平山县| 驻马店市| 东海县| 鞍山市| 津市市| 乐都县| 盱眙县| 华安县| 阳西县| 长寿区| 抚州市| 明光市| 古田县| 泽州县| 平罗县| 墨竹工卡县| 阳江市| 汝州市| 伊通| 高州市| 晋城| 内乡县| 民乐县| 泰来县| 镇安县| 瓦房店市| 汕尾市| 中西区| 凤城市| 汉川市|