SpringBoot+Jpa項(xiàng)目配置雙數(shù)據(jù)源的實(shí)現(xiàn)
引言
今天為大家?guī)硪恍┓浅S杏玫膶?shí)戰(zhàn)技巧,比如在我們需要對(duì)兩個(gè)數(shù)據(jù)庫進(jìn)行操作的時(shí)候而哦我們通常用的只是單數(shù)據(jù)庫查詢,這就觸及到知識(shí)盲點(diǎn)了,那么廢話不多說上代碼!
配置yml文件
server:
port: 8080
spring:
profiles:
active: dev
jackson:
time-zone: GMT+8
# 這里是我們的數(shù)據(jù)庫配置地方
datasource:
data1: #這里是數(shù)據(jù)庫一
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.0.77:3306/test1?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&autoReconnect=true&failOverReadOnly=false&rewriteBatchedStatements=true
username: root
password: 123
type: com.alibaba.druid.pool.DruidDataSource
data2: #這里是數(shù)據(jù)庫二
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.0.88:3306/test2?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&autoReconnect=true&failOverReadOnly=false&rewriteBatchedStatements=true
username: root
password: 123
type: com.alibaba.druid.pool.DruidDataSource
當(dāng)然到這里肯定是沒有結(jié)束的,需要配置一些參數(shù),否則啟動(dòng)會(huì)報(bào)錯(cuò)
創(chuàng)建數(shù)據(jù)源配置類
我們創(chuàng)建一個(gè)數(shù)據(jù)源的類,我們就給他取名為DataSourceConfig(這個(gè)名字自定義),在項(xiàng)目下創(chuàng)建一個(gè)包,方便管理

package com.eman.cdn.common.dataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
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 https://blog.csdn.net/weixin_42782429?spm=1000.2115.3001.5343
* @date 2021/12/21 11:35
* <p>
* description 數(shù)據(jù)源配置
*/
@Configuration
public class DataSourceConfig {
# 這里是我們?cè)趛ml文件配置的位置
private final static String DB_TEST1 = "spring.datasource.test1";
private final static String DB_TEST2= "spring.datasource.test1";
# 這個(gè)Bean名字子自定義只要不是重復(fù)的Bean名字就行了
@Bean(name = "test1Properties")
@Qualifier("test1Properties")
@Primary
@ConfigurationProperties(DB_TEST1)
public DataSourceProperties test1Properties() {
return new DataSourceProperties();
}
@Bean(name = "test1DataSource")
@Qualifier("test1DataSource")
@Primary
@ConfigurationProperties(prefix = DB_TEST1)
public DataSource test1DataSource() {
return test1Properties().initializeDataSourceBuilder().build();
}
@Bean(name = "test2Properties")
@Qualifier("test2Properties")
@ConfigurationProperties(DB_TEST2)
public DataSourceProperties test2Properties() {
return new DataSourceProperties();
}
@Bean(name = "test2DataSource")
@Qualifier("test2DataSource")
@ConfigurationProperties(prefix = DB_ANALYSIS)
public DataSource test2DataSource() {
return test2Properties().initializeDataSourceBuilder().build();
}
}
為每個(gè)數(shù)據(jù)庫創(chuàng)建配置類
由于需要用到Mybaitis-plus所以你得先導(dǎo)入Mybaitis-Plus的依賴
<!--mybatis-plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.0</version>
</dependency>
package com.eman.xx.xxx.xx;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateSettings;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
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.sql.DataSource;
import java.util.Objects;
/**
* @author tongJie
* @date 2021/12/21 11:59
* <p>
* description 日志處理端數(shù)據(jù)庫配置
*/
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
// 配置連接工廠
entityManagerFactoryRef = "tes1Factory",
// 配置事物管理器
transactionManagerRef = "tes1Transaction",
// 設(shè)置Jpa 的 repository所在位置
basePackages = {"com.xx.xx.xx.**.repository"}
)
// 設(shè)置掃描的mapper
@MapperScan(basePackages ="xx.xx.xx.test1.**.mapper", sqlSessionTemplateRef = "tes1SqlSessionTemplate")
public class AnalysisDataBaseConfig {
@Autowired
@Qualifier("tes1DataSource")
private DataSource analysisDataSource;
@Autowired
private JpaProperties jpaProperties;
@Autowired
private HibernateProperties properties;
// 以下是jpa的配置
/**
* 連接工廠
* @param builder 創(chuàng)建工具
* @return 結(jié)果
*/
@Bean(name = "tes1Factory")
public LocalContainerEntityManagerFactoryBean tes1Factory(EntityManagerFactoryBuilder builder) {
return builder
// 設(shè)置數(shù)據(jù)源
.dataSource(analysisDataSource)
//設(shè)置實(shí)體類所在位置.掃描所有帶有 @Entity 注解的類
.packages("xx.xx.xx.tes1.**.entity")
// Spring會(huì)將EntityManagerFactory注入到Repository之中.有了 EntityManagerFactory之后,
// Repository就能用它來創(chuàng)建 EntityManager 了,然后 EntityManager 就可以針對(duì)數(shù)據(jù)庫執(zhí)行操作
.persistenceUnit("tes1")
// 為了加載yml中jpa下hibernate的相關(guān)配置
.properties(properties.determineHibernateProperties(jpaProperties.getProperties(), new HibernateSettings()))
.build();
}
/**
* 配置事物管理器
*
* @param builder 創(chuàng)建工具
* @return 結(jié)果
*/
@Bean(name = "tes1Transaction")
PlatformTransactionManager tes1Transaction(EntityManagerFactoryBuilder builder) {
return new JpaTransactionManager(Objects.requireNonNull(analysisFactory(builder).getObject()));
}
// 以下是mybatis的配置
/**
* 配置sqlSessionFactory
* @return 結(jié)果
* @throws Exception 異常
*/
@Bean("tes1SqlSessionFactory")
public SqlSessionFactory tes1SqlSessionFactory() throws Exception {
MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
sqlSessionFactory.setDataSource(analysisDataSource);
sqlSessionFactory.setMapperLocations(new
//這里填寫你mybaits的xml文件存放的路徑
PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/*.xml"));
return sqlSessionFactory.getObject();
}
/**
* 配置sqlSessionTemplate
* @return 結(jié)果
*/
@Bean(name = "tes1SqlSessionTemplate")
public SqlSessionTemplate tes1SqlSessionTemplate() throws Exception {
return new SqlSessionTemplate(tes1SqlSessionFactory());
}
}
以此類推之后每增加一個(gè)數(shù)據(jù)庫源就循環(huán)上面的方法
注意?。。?!包一定要放對(duì)你配置的位置,否則不識(shí)別就會(huì)報(bào)錯(cuò)?。。。。?!
到此這篇關(guān)于SpringBoot+Jpa項(xiàng)目配置雙數(shù)據(jù)庫源的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)SpringBoot Jpa雙數(shù)據(jù)庫源內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java Swing樹狀組件JTree用法實(shí)例詳解
這篇文章主要介紹了Java Swing樹狀組件JTree用法,結(jié)合具體實(shí)例形式分析了Swing組件JTree構(gòu)成樹狀列表的節(jié)點(diǎn)設(shè)置與事件響應(yīng),以及自定義圖形節(jié)點(diǎn)的相關(guān)操作技巧,需要的朋友可以參考下2017-11-11
關(guān)于@ApiModel和@ApiModelProperty的使用
這篇文章主要介紹了關(guān)于@ApiModel和@ApiModelProperty的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
多模字符串匹配算法原理及Java實(shí)現(xiàn)代碼
這篇文章主要介紹了多模字符串匹配算法原理及Java實(shí)現(xiàn)代碼,涉及算法背景,原理,構(gòu)建過程簡(jiǎn)單介紹幾Java代碼實(shí)現(xiàn)等相關(guān)內(nèi)容,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11
Java中@DateTimeFormat和@JsonFormat注解介紹
@DateTimeFormat和@JsonFormat都是處理時(shí)間格式化問題的,把其他類型轉(zhuǎn)換成自己需要的時(shí)間類型,下面這篇文章主要給大家介紹了關(guān)于Java中@DateTimeFormat和@JsonFormat注解介紹的相關(guān)資料,需要的朋友可以參考下2022-11-11
Spring-Validation 后端數(shù)據(jù)校驗(yàn)的實(shí)現(xiàn)
這篇文章主要介紹了Spring-Validation 后端數(shù)據(jù)校驗(yàn)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
通過Java實(shí)現(xiàn)自己動(dòng)手寫ls命令
在前面的文章中,我們仔細(xì)的介紹了關(guān)于ls命令的使用和輸出結(jié)果,在本篇文章當(dāng)中我們用Java代碼自己實(shí)現(xiàn)ls命令,更加深入的了解ls命令2022-10-10
java中數(shù)組list map三者之間的互轉(zhuǎn)介紹
java中 數(shù)組 list map之間的互轉(zhuǎn)一張圖清晰呈現(xiàn)并附有代碼,不懂的朋友可以參考下2013-10-10

