springboot項(xiàng)目連接多種數(shù)據(jù)庫該如何操作詳析
前言
在項(xiàng)目的開發(fā)中,經(jīng)常會遇到需要連接多個(gè)多種數(shù)據(jù)庫的情況,mysql、oracle等等,下面詳細(xì)講解如何在一個(gè)服務(wù)中進(jìn)行多種數(shù)據(jù)庫的配置。
第一步:
在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ù)庫鏈接驅(qū)動
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ù)庫鏈接驅(qū)動第二步:
創(chuàng)建多個(gè)配置類,以配置oracle和mysql兩個(gè)數(shù)據(jù)庫為例,可參考代碼進(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)會被自動映射到db1DataSource所創(chuàng)建的數(shù)據(jù)源實(shí)例中。通過DataSourceBuilder. create()創(chuàng)建一個(gè)新的數(shù)據(jù)源構(gòu)建器,并調(diào)用.build()方法來完成數(shù)據(jù)源實(shí)例的創(chuàng)建。 如果有多個(gè)相同類型的Bean,使用@Primary注解可以標(biāo)記出一個(gè)優(yōu)先(默認(rèn))使用的Bean。所以使用最多的數(shù)據(jù)庫可以使用@Primary注解。
2.配置MyBatis的SqlSessionFactory,它是MyBatis操作數(shù)據(jù)庫的核心組件,負(fù)責(zé)創(chuàng)建SqlSession對象,執(zhí)行SQL語句等。使用名稱為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),專門用于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會話執(zhí)行環(huán)境。
@Bean
@Primary
public SqlSessionTemplate db1SqlSessionTemplate(@Qualifier("db1SqlSessionFactory") SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}5.類注解@MapperScan
basePackages= "com.example.mapper":指定了Mapper接口所在的包路徑。Spring會掃描這個(gè)包及其子包下的所有接口,如果接口符合MyBatis Mapper的規(guī)范,Spring會自動生成代理對象來處理SQL調(diào)用。
sqlSessionTemplateRef = "db1SqlSessionTemplate":指定了與Mapper接口綁定的sqlSessionTemplate的名稱。在執(zhí)行Mapper接口的方法時(shí),Spring會使用這個(gè)指定的sqlSessionTemplate來管理SQL會話。這里的db1SqlSessionTemplate是之前通過@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ù)庫的交互即可。
總結(jié)
到此這篇關(guān)于springboot項(xiàng)目連接多種數(shù)據(jù)庫該如何操作的文章就介紹到這了,更多相關(guān)springboot連接多種數(shù)據(jù)庫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- springboot連接多個(gè)數(shù)據(jù)庫的實(shí)現(xiàn)方法
- SpringBoot配置Hikari數(shù)據(jù)庫連接池的詳細(xì)步驟
- IDEA中SpringBoot項(xiàng)目數(shù)據(jù)庫連接加密方法
- SpringBoot項(xiàng)目中連接Gauss數(shù)據(jù)庫
- 詳解如何在SpringBoot中配置MySQL數(shù)據(jù)庫的連接數(shù)
- SpringBoot配置多個(gè)數(shù)據(jù)源超簡單步驟(連接多個(gè)數(shù)據(jù)庫)
- 解決Springboot不能自動提交數(shù)據(jù)庫連接問題
相關(guān)文章
新手入門學(xué)習(xí)Spring Freemarker教程解析
這篇文章主要介紹了新手入門學(xué)習(xí)Freemarker教程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
解析Idea為什么不推薦使用@Autowired進(jìn)行Field注入
這篇文章主要介紹了Idea不推薦使用@Autowired進(jìn)行Field注入的原因,網(wǎng)上文章大部分都是介紹兩者的區(qū)別,沒有提到為什么,當(dāng)時(shí)想了好久想出了可能的原因,今天來總結(jié)一下2022-05-05
Java進(jìn)程內(nèi)緩存框架EhCache詳解
這篇文章主要介紹了Java進(jìn)程內(nèi)緩存框架EhCache,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2021-12-12
SpringBoot淺析緩存機(jī)制之Redis單機(jī)緩存應(yīng)用
在上文中我介紹了Spring Boot使用EhCache 2.x來作為緩存的實(shí)現(xiàn),本文接著介紹使用單機(jī)版的Redis作為緩存的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
springboot下使用shiro自定義filter的個(gè)人經(jīng)驗(yàn)分享
這篇文章主要介紹了springboot下使用shiro自定義filter的個(gè)人經(jīng)驗(yàn),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
深入探究SpringBoot中的Elasticsearch自動配置原理及用法
SpringBoot中的Elasticsearch自動配置為我們提供了一種快速集成Elasticsearch的方式,使我們可以在SpringBoot應(yīng)用程序中輕松地使用Elasticsearch,本文將介紹Spring Boot中的Elasticsearch自動配置的作用、原理和使用方法2023-07-07
Maven基礎(chǔ)之如何修改本地倉庫的默認(rèn)路徑
這篇文章主要介紹了Maven基礎(chǔ)之如何修改本地倉庫的默認(rèn)路徑問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05

