最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springboot項目連接多種數(shù)據(jù)庫該如何操作詳析

 更新時間:2024年08月01日 10:01:56   作者:不柔情  
在Spring Boot應(yīng)用中連接多個數(shù)據(jù)庫或數(shù)據(jù)源可以使用多種方式,下面這篇文章主要給大家介紹了關(guān)于springboot項目連接多種數(shù)據(jù)庫該如何操作的相關(guān)資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下

前言

在項目的開發(fā)中,經(jīng)常會遇到需要連接多個多種數(shù)據(jù)庫的情況,mysql、oracle等等,下面詳細講解如何在一個服務(wù)中進行多種數(shù)據(jù)庫的配置。

第一步:

在yml配置文件中配置多個數(shù)據(jù)源,如下,根據(jù)實際情況更改自己的配置即可。

spring:
  datasource:
    # 配置多個數(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)建多個配置類,以配置oracle和mysql兩個數(shù)據(jù)庫為例,可參考代碼進行延展。

1.在配置類中需要進行數(shù)據(jù)源配置

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.primary")
    @Primary
    public DataSource db1DataSource() {
        return DataSourceBuilder.create().build();
    }
@ConfigurationProperties(prefix = "spring.datasource.primary")用于綁定yml中的第一個數(shù)據(jù)源配置,這些配置項會被自動映射到db1DataSource所創(chuàng)建的數(shù)據(jù)源實例中。通過DataSourceBuilder. create()創(chuàng)建一個新的數(shù)據(jù)源構(gòu)建器,并調(diào)用.build()方法來完成數(shù)據(jù)源實例的創(chuàng)建。

如果有多個相同類型的Bean,使用@Primary注解可以標記出一個優(yōu)先(默認)使用的Bean。所以使用最多的數(shù)據(jù)庫可以使用@Primary注解。

2.配置MyBatis的SqlSessionFactory,它是MyBatis操作數(shù)據(jù)庫的核心組件,負責(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ù)管理實現(xiàn),專門用于JDBC事務(wù)處理。注解明確指定使用名為db1DataSource的數(shù)據(jù)源Bean。

    @Bean
    @Primary
    public DataSourceTransactionManager db1TransactionManager(@Qualifier("db1DataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

4.配置SqlSessionTemplate實例,它是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會掃描這個包及其子包下的所有接口,如果接口符合MyBatis Mapper的規(guī)范,Spring會自動生成代理對象來處理SQL調(diào)用。
  • sqlSessionTemplateRef = "db1SqlSessionTemplate":指定了與Mapper接口綁定的sqlSessionTemplate的名稱。在執(zhí)行Mapper接口的方法時,Spring會使用這個指定的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)建兩個mapper包如下:

在不同的mapper包下進行不同數(shù)據(jù)庫的交互即可。

總結(jié)

到此這篇關(guān)于springboot項目連接多種數(shù)據(jù)庫該如何操作的文章就介紹到這了,更多相關(guān)springboot連接多種數(shù)據(jù)庫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 新手入門學(xué)習(xí)Spring Freemarker教程解析

    新手入門學(xué)習(xí)Spring Freemarker教程解析

    這篇文章主要介紹了新手入門學(xué)習(xí)Freemarker教程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-10-10
  • JAVA Static關(guān)鍵字的用法

    JAVA Static關(guān)鍵字的用法

    這篇文章主要介紹了JAVA Static關(guān)鍵字的用法,文中講解非常細致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • 解析Idea為什么不推薦使用@Autowired進行Field注入

    解析Idea為什么不推薦使用@Autowired進行Field注入

    這篇文章主要介紹了Idea不推薦使用@Autowired進行Field注入的原因,網(wǎng)上文章大部分都是介紹兩者的區(qū)別,沒有提到為什么,當(dāng)時想了好久想出了可能的原因,今天來總結(jié)一下
    2022-05-05
  • Java進程內(nèi)緩存框架EhCache詳解

    Java進程內(nèi)緩存框架EhCache詳解

    這篇文章主要介紹了Java進程內(nèi)緩存框架EhCache,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2021-12-12
  • SpringBoot淺析緩存機制之Redis單機緩存應(yīng)用

    SpringBoot淺析緩存機制之Redis單機緩存應(yīng)用

    在上文中我介紹了Spring Boot使用EhCache 2.x來作為緩存的實現(xiàn),本文接著介紹使用單機版的Redis作為緩存的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • springboot下使用shiro自定義filter的個人經(jīng)驗分享

    springboot下使用shiro自定義filter的個人經(jīng)驗分享

    這篇文章主要介紹了springboot下使用shiro自定義filter的個人經(jīng)驗,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • 深入探究SpringBoot中的Elasticsearch自動配置原理及用法

    深入探究SpringBoot中的Elasticsearch自動配置原理及用法

    SpringBoot中的Elasticsearch自動配置為我們提供了一種快速集成Elasticsearch的方式,使我們可以在SpringBoot應(yīng)用程序中輕松地使用Elasticsearch,本文將介紹Spring Boot中的Elasticsearch自動配置的作用、原理和使用方法
    2023-07-07
  • Maven基礎(chǔ)之如何修改本地倉庫的默認路徑

    Maven基礎(chǔ)之如何修改本地倉庫的默認路徑

    這篇文章主要介紹了Maven基礎(chǔ)之如何修改本地倉庫的默認路徑問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • Spring 框架中的 SseEmitter 使用原理解析

    Spring 框架中的 SseEmitter 使用原理解析

    SseEmitter是Spring MVC提供的一個類,用于實現(xiàn)基于HTTP的服務(wù)器單向推送(Server-Sent Events),本文給大家介紹Spring框架中的 SseEmitter使用詳解,感興趣的朋友跟隨小編一起看看吧
    2026-02-02
  • Java下載https文件并上傳阿里云oss服務(wù)器

    Java下載https文件并上傳阿里云oss服務(wù)器

    這篇文章主要介紹了Java下載https文件并上傳到阿里云oss服務(wù)器,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-01-01

最新評論

白河县| 清徐县| 宁南县| 兴安盟| 山西省| 东安县| 砀山县| 辉南县| 白朗县| 信阳市| 临泽县| 新余市| 绥阳县| 舒兰市| 宜昌市| 云龙县| 响水县| 樟树市| 洛浦县| 彩票| 凤冈县| 夏河县| 富裕县| 兰溪市| 博兴县| 临颍县| 安顺市| 大足县| 和政县| 陇西县| 深泽县| 定西市| 罗定市| 平远县| 兴和县| 汝南县| 岢岚县| 龙里县| 焦作市| 和静县| 新化县|