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

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

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

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

配置文件

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 {

  /**
   * 第一個(gè)數(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();
  }

  /**
   * 第二個(gè)數(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í)體類所在文件夾,兩個(gè)數(shù)據(jù)源的實(shí)體類可相同***")
        .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(
    //實(shí)體管理
    entityManagerFactoryRef="entityManagerFactorySecond",
    //事務(wù)管理
    transactionManagerRef="transactionManagerSecond",
    //實(shí)體掃描,設(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í)體類所在文件夾,兩個(gè)數(shù)據(jù)源的實(shí)體類可相同***")
        .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使用DOM對XML文檔進(jìn)行增刪改查操作實(shí)例代碼

    java使用DOM對XML文檔進(jìn)行增刪改查操作實(shí)例代碼

    這篇文章主要介紹了java使用DOM對XML文檔進(jìn)行增刪改查操作實(shí)例代碼,實(shí)例涉及對xml文檔的增刪改查,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-02-02
  • 解決java后臺(tái)登錄前后cookie不一致問題

    解決java后臺(tái)登錄前后cookie不一致問題

    本文主要介紹了java后臺(tái)登錄前后cookie不一致的解決方案,具有很好的參考價(jià)值,需要的朋友一起來看下吧
    2016-12-12
  • Java的Spring框架的三種連接池的基本用法示例

    Java的Spring框架的三種連接池的基本用法示例

    這篇文章主要介紹了Java的Spring框架的三種連接池的基本用法示例,Spring框架是Java下注明的SSH三大web開發(fā)框架之一,需要的朋友可以參考下
    2015-11-11
  • Java反轉(zhuǎn)字符串和相關(guān)字符編碼的問題解決

    Java反轉(zhuǎn)字符串和相關(guān)字符編碼的問題解決

    反轉(zhuǎn)字符串一直被當(dāng)作是簡單問題,大家的思想主要就是利用遍歷,首尾交換字符實(shí)現(xiàn)字符串的反轉(zhuǎn)。例如下面的代碼,就可以簡單實(shí)現(xiàn)反轉(zhuǎn)。
    2013-05-05
  • SpringCloud項(xiàng)目中Feign組件添加請求頭所遇到的坑及解決

    SpringCloud項(xiàng)目中Feign組件添加請求頭所遇到的坑及解決

    這篇文章主要介紹了SpringCloud項(xiàng)目中Feign組件添加請求頭所遇到的坑及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • Spring Boot ActiveMQ如何設(shè)置訪問密碼

    Spring Boot ActiveMQ如何設(shè)置訪問密碼

    這篇文章主要介紹了Spring Boot ActiveMQ如何設(shè)置訪問密碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-07-07
  • 深入理解Java顯式鎖的相關(guān)知識(shí)

    深入理解Java顯式鎖的相關(guān)知識(shí)

    今天帶大家學(xué)習(xí)的是關(guān)于Java的相關(guān)知識(shí),文章圍繞著Java顯式鎖展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-06-06
  • 三道java新手入門面試題,通往自由的道路--多線程

    三道java新手入門面試題,通往自由的道路--多線程

    這篇文章主要為大家分享了最有價(jià)值的3道多線程面試題,涵蓋內(nèi)容全面,包括數(shù)據(jù)結(jié)構(gòu)和算法相關(guān)的題目、經(jīng)典面試編程題等,對hashCode方法的設(shè)計(jì)、垃圾收集的堆和代進(jìn)行剖析,感興趣的小伙伴們可以參考一下
    2021-07-07
  • 使用jenkins部署springboot項(xiàng)目的方法步驟

    使用jenkins部署springboot項(xiàng)目的方法步驟

    這篇文章主要介紹了使用jenkins部署springboot項(xiàng)目的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • spring中@ComponentScan自動(dòng)掃描并指定掃描規(guī)則

    spring中@ComponentScan自動(dòng)掃描并指定掃描規(guī)則

    本文主要介紹了spring中@ComponentScan自動(dòng)掃描并指定掃描規(guī)則,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04

最新評論

西峡县| 中方县| 弋阳县| 梁山县| 句容市| 辽阳县| 河间市| 宣武区| 杂多县| 沙雅县| 屏山县| 蒙阴县| 襄汾县| 二连浩特市| 化隆| 隆昌县| 葫芦岛市| 隆子县| 两当县| 扬中市| 奉新县| 吕梁市| 扎兰屯市| 奇台县| 水城县| 凭祥市| 宝清县| 张家口市| 山东| 河北省| 子长县| 洛南县| 马公市| 衢州市| 梅河口市| 富川| 伊川县| 武夷山市| 崇州市| 高雄县| 高安市|