一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|

服務器之家 - 編程語言 - JAVA教程 - Spring boot配置多數據源代碼實例

Spring boot配置多數據源代碼實例

2020-07-09 11:25阮帥 JAVA教程

這篇文章主要介紹了Spring boot配置多數據源代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

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

配置文件

?
1
2
3
4
5
6
7
8
9
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

通用數據源配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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());
  }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://www.cnblogs.com/ruanshuai/p/13252625.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 青青青青青 | 国产福利专区精品视频 | 精品国产一区二区在线观看 | 精品国产一区二区三区在线 | 亚洲精品高清中文字幕完整版 | 99av涩导航 | 日产精品一二三四区国产 | 精品国产品香蕉在线观看 | 摸逼网 | www毛片| 国产精品激情综合久久 | 日韩在线二区全免费 | 全黄一级裸片视频免费 | 无颜之月全集免费观看 | 国产精品久久久免费视频 | 美女脱了内裤打开腿让人桶网站o | 高h短篇合集 | 1313午夜精品理伦片 | 91麻豆精品国产 | 欧美性f| 亚洲免费色图 | 腿交hd | 亚洲无线一二三四区 | 欧美理论片手机在线观看片免费 | 波多野结在线 | 扒开黑女人p大荫蒂老女人 扒开大腿狠狠挺进视频 | 赤坂丽女医bd无删减在线观看 | 亚洲欧美专区精品伊人久久 | 5555国产在线观看精品 | 成人网免费视频 | 成人不卡在线 | 大ji吧快给我别停受不了视频 | 第一福利在线导航 | 成年人黄色录像 | 91搞搞 | 91啦在线播放 | 国产精品视频一区二区三区 | 久久久精品免费免费直播 | 国内在线观看 | 91精品国产高清久久久久久 | 精品日韩欧美一区二区三区 |