springboot項(xiàng)目連接多種數(shù)據(jù)庫(kù)該如何操作詳析
前言
在項(xiàng)目的開(kāi)發(fā)中,經(jīng)常會(huì)遇到需要連接多個(gè)多種數(shù)據(jù)庫(kù)的情況,mysql、oracle等等,下面詳細(xì)講解如何在一個(gè)服務(wù)中進(jìn)行多種數(shù)據(jù)庫(kù)的配置。
第一步:
在yml配置文件中配置多個(gè)數(shù)據(jù)源,如下,根據(jù)實(shí)際情況更改自己的配置即可。
spring:
datasource:
# 配置多個(gè)數(shù)據(jù)源
primary:
type: com.alibaba.druid.pool.DruidDataSource
jdbc-url: jdbc:oracle:thin:@171.28.7.55:1521:example
username: root
password: root
driver-class-name: oracle.jdbc.OracleDriver #數(shù)據(jù)庫(kù)鏈接驅(qū)動(dòng)
secondary:
type: com.alibaba.druid.pool.DruidDataSource
jdbc-url: jdbc:mysql://127.0.0.1:3306/exinfo?useUnicode=true&characterEncoding=utf-8
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver #數(shù)據(jù)庫(kù)鏈接驅(qū)動(dòng)第二步:
創(chuàng)建多個(gè)配置類,以配置oracle和mysql兩個(gè)數(shù)據(jù)庫(kù)為例,可參考代碼進(jìn)行延展。

1.在配置類中需要進(jìn)行數(shù)據(jù)源配置
@Bean
@ConfigurationProperties(prefix = "spring.datasource.primary")
@Primary
public DataSource db1DataSource() {
return DataSourceBuilder.create().build();
}@ConfigurationProperties(prefix = "spring.datasource.primary")用于綁定yml中的第一個(gè)數(shù)據(jù)源配置,這些配置項(xiàng)會(huì)被自動(dòng)映射到db1DataSource所創(chuàng)建的數(shù)據(jù)源實(shí)例中。通過(guò)DataSourceBuilder. create()創(chuàng)建一個(gè)新的數(shù)據(jù)源構(gòu)建器,并調(diào)用.build()方法來(lái)完成數(shù)據(jù)源實(shí)例的創(chuàng)建。 如果有多個(gè)相同類型的Bean,使用@Primary注解可以標(biāo)記出一個(gè)優(yōu)先(默認(rèn))使用的Bean。所以使用最多的數(shù)據(jù)庫(kù)可以使用@Primary注解。
2.配置MyBatis的SqlSessionFactory,它是MyBatis操作數(shù)據(jù)庫(kù)的核心組件,負(fù)責(zé)創(chuàng)建SqlSession對(duì)象,執(zhí)行SQL語(yǔ)句等。使用名稱為db1DataSource的數(shù)據(jù)源Bean作為構(gòu)造SqlSessionFactory的依賴。
@Bean
@Primary
public SqlSessionFactory db1SqlSessionFactory(@Qualifier("db1DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
//bean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);//設(shè)置下劃線轉(zhuǎn)駝峰式
return bean.getObject();
}3.配置事務(wù)管理器(DataSourceTransactionManager),它基于數(shù)據(jù)源(DataSource)的事務(wù)管理實(shí)現(xiàn),專門(mén)用于JDBC事務(wù)處理。注解明確指定使用名為db1DataSource的數(shù)據(jù)源Bean。
@Bean
@Primary
public DataSourceTransactionManager db1TransactionManager(@Qualifier("db1DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}4.配置SqlSessionTemplate實(shí)例,它是MyBatis與Spring集成的關(guān)鍵組件,提供了線程安全的SQL會(huì)話執(zhí)行環(huán)境。
@Bean
@Primary
public SqlSessionTemplate db1SqlSessionTemplate(@Qualifier("db1SqlSessionFactory") SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}5.類注解@MapperScan
basePackages= "com.example.mapper":指定了Mapper接口所在的包路徑。Spring會(huì)掃描這個(gè)包及其子包下的所有接口,如果接口符合MyBatis Mapper的規(guī)范,Spring會(huì)自動(dòng)生成代理對(duì)象來(lái)處理SQL調(diào)用。
sqlSessionTemplateRef = "db1SqlSessionTemplate":指定了與Mapper接口綁定的sqlSessionTemplate的名稱。在執(zhí)行Mapper接口的方法時(shí),Spring會(huì)使用這個(gè)指定的sqlSessionTemplate來(lái)管理SQL會(huì)話。這里的db1SqlSessionTemplate是之前通過(guò)@Bean方法定義的sqlSessionTemplate Beam的名稱。
DataSourcePrimaryConfig完整代碼如下:
package com.example.config;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = "com.example.mapper", sqlSessionTemplateRef = "db1SqlSessionTemplate")
public class DataSourcePrimaryConfig {
@Bean
@ConfigurationProperties(prefix = "spring.datasource.primary")
@Primary
public DataSource db1DataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@Primary
public SqlSessionFactory db1SqlSessionFactory(@Qualifier("db1DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
//bean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);//設(shè)置下劃線轉(zhuǎn)駝峰式
return bean.getObject();
}
@Bean
@Primary
public DataSourceTransactionManager db1TransactionManager(@Qualifier("db1DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean
@Primary
public SqlSessionTemplate db1SqlSessionTemplate(@Qualifier("db1SqlSessionFactory") SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
}DataSourceSecondaryConfig代碼如下:
package com.example.config;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = "com.example.mapper2", sqlSessionTemplateRef = "db2SqlSessionTemplate")
public class DataSourceSecondaryConfig {
@Bean
@ConfigurationProperties(prefix = "spring.datasource.secondary")
public DataSource db2DataSource() {
return DataSourceBuilder.create().build();
}
@Bean
public SqlSessionFactory db2SqlSessionFactory(@Qualifier("db2DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);//設(shè)置下劃線轉(zhuǎn)駝峰式
return bean.getObject();
}
@Bean
public DataSourceTransactionManager db2TransactionManager(@Qualifier("db2DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean
public SqlSessionTemplate db2SqlSessionTemplate(@Qualifier("db2SqlSessionFactory") SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
}第三步:
根據(jù)配置文件,創(chuàng)建兩個(gè)mapper包如下:

在不同的mapper包下進(jìn)行不同數(shù)據(jù)庫(kù)的交互即可。
總結(jié)
到此這篇關(guān)于springboot項(xiàng)目連接多種數(shù)據(jù)庫(kù)該如何操作的文章就介紹到這了,更多相關(guān)springboot連接多種數(shù)據(jù)庫(kù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springboot連接多個(gè)數(shù)據(jù)庫(kù)的實(shí)現(xiàn)方法
- SpringBoot配置Hikari數(shù)據(jù)庫(kù)連接池的詳細(xì)步驟
- IDEA中SpringBoot項(xiàng)目數(shù)據(jù)庫(kù)連接加密方法
- SpringBoot項(xiàng)目中連接Gauss數(shù)據(jù)庫(kù)
- 詳解如何在SpringBoot中配置MySQL數(shù)據(jù)庫(kù)的連接數(shù)
- SpringBoot配置多個(gè)數(shù)據(jù)源超簡(jiǎn)單步驟(連接多個(gè)數(shù)據(jù)庫(kù))
- 解決Springboot不能自動(dòng)提交數(shù)據(jù)庫(kù)連接問(wèn)題
相關(guān)文章
SpringBoot整合RabbitMQ消息隊(duì)列的完整步驟
這篇文章主要給大家介紹了關(guān)于SpringBoot整合RabbitMQ消息隊(duì)列的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
Java Spring WEB應(yīng)用實(shí)例化如何實(shí)現(xiàn)
這篇文章主要介紹了Java Spring WEB應(yīng)用實(shí)例化如何實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12
mybatisplus自動(dòng)填充屬性值的實(shí)現(xiàn)步驟
MyBatis-Plus提供自動(dòng)填充的功能,幫助自定設(shè)置這些字段的值,提升開(kāi)發(fā)效率,本文就來(lái)介紹一下如何使用,感興趣的可以了解一下2023-12-12
Java中的SynchronousQueue阻塞隊(duì)列及使用場(chǎng)景解析
這篇文章主要介紹了Java中的SynchronousQueue阻塞隊(duì)列及使用場(chǎng)景解析,SynchronousQueue 是 Java 中的一個(gè)特殊的阻塞隊(duì)列,它的主要特點(diǎn)是它的容量為0,這意味著 SynchronousQueue不會(huì)存儲(chǔ)任何元素,需要的朋友可以參考下2023-12-12
用3個(gè)實(shí)例從原理到實(shí)戰(zhàn)講清楚Log4j史詩(shī)級(jí)漏洞
最近應(yīng)該很多人都在關(guān)注著一個(gè)漏洞Apache Log4j 2遠(yuǎn)程代碼執(zhí)行,該漏洞一旦被攻擊者利用會(huì)造成嚴(yán)重危害,這篇文章主要給大家介紹了關(guān)于如何用3個(gè)實(shí)例從原理到實(shí)戰(zhàn)講清楚Log4j史詩(shī)級(jí)漏洞的相關(guān)資料,需要的朋友可以參考下2021-12-12
Spring中Bean創(chuàng)建完后打印語(yǔ)句的兩種方法
這篇文章主要介紹了Spring中Bean創(chuàng)建完后打印語(yǔ)句的兩種方法,一個(gè)是實(shí)現(xiàn)InitializingBean接口,另一個(gè)使用@Bean注解和initMethod屬性,通過(guò)代碼示例介紹的非常詳細(xì),感興趣的小伙伴可以參考閱讀2023-07-07
Spring Cloud重試機(jī)制與各組件的重試總結(jié)
這篇文章主要給大家介紹了關(guān)于Spring Cloud中重試機(jī)制與各組件的重試的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11

