Spring boot配置多數(shù)據(jù)源代碼實(shí)例
因項(xiàng)目需要在一個(gè)應(yīng)用里從兩個(gè)數(shù)據(jù)庫(kù)取數(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)先級(jí)最高
* @return
*/
@Primary
@Bean(name = "primaryDataSource") //數(shù)據(jù)源1配置名
@Qualifier("primaryDataSource") //數(shù)據(jù)源1配置名
@ConfigurationProperties(prefix="spring.datasource.primary") //見(jiàn)配置文件
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") //見(jiàn)配置文件
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í)體類(lèi)所在文件夾,兩個(gè)數(shù)據(jù)源的實(shí)體類(lèi)可相同***")
.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í)體類(lèi)所在文件夾,兩個(gè)數(shù)據(jù)源的實(shí)體類(lèi)可相同***")
.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());
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java使用DOM對(duì)XML文檔進(jìn)行增刪改查操作實(shí)例代碼
這篇文章主要介紹了java使用DOM對(duì)XML文檔進(jìn)行增刪改查操作實(shí)例代碼,實(shí)例涉及對(duì)xml文檔的增刪改查,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02
解決java后臺(tái)登錄前后cookie不一致問(wèn)題
本文主要介紹了java后臺(tái)登錄前后cookie不一致的解決方案,具有很好的參考價(jià)值,需要的朋友一起來(lái)看下吧2016-12-12
Java反轉(zhuǎn)字符串和相關(guān)字符編碼的問(wèn)題解決
反轉(zhuǎn)字符串一直被當(dāng)作是簡(jiǎn)單問(wèn)題,大家的思想主要就是利用遍歷,首尾交換字符實(shí)現(xiàn)字符串的反轉(zhuǎn)。例如下面的代碼,就可以簡(jiǎn)單實(shí)現(xiàn)反轉(zhuǎn)。2013-05-05
SpringCloud項(xiàng)目中Feign組件添加請(qǐng)求頭所遇到的坑及解決
這篇文章主要介紹了SpringCloud項(xiàng)目中Feign組件添加請(qǐng)求頭所遇到的坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
Spring Boot ActiveMQ如何設(shè)置訪問(wèn)密碼
這篇文章主要介紹了Spring Boot ActiveMQ如何設(shè)置訪問(wèn)密碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
三道java新手入門(mén)面試題,通往自由的道路--多線程
這篇文章主要為大家分享了最有價(jià)值的3道多線程面試題,涵蓋內(nèi)容全面,包括數(shù)據(jù)結(jié)構(gòu)和算法相關(guān)的題目、經(jīng)典面試編程題等,對(duì)hashCode方法的設(shè)計(jì)、垃圾收集的堆和代進(jìn)行剖析,感興趣的小伙伴們可以參考一下2021-07-07
使用jenkins部署springboot項(xiàng)目的方法步驟
這篇文章主要介紹了使用jenkins部署springboot項(xiàng)目的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
spring中@ComponentScan自動(dòng)掃描并指定掃描規(guī)則
本文主要介紹了spring中@ComponentScan自動(dòng)掃描并指定掃描規(guī)則,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04

