欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

前言

在項目的開發(fā)中,經(jīng)常會遇到需要連接多個多種數(shù)據(jù)庫的情況,mysql、oracle等等,下面詳細(xì)講解如何在一個服務(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注解可以標(biāo)記出一個優(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ù)管理實現(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)文章

  • 一文詳解Spring是怎么讀取配置Xml文件的

    一文詳解Spring是怎么讀取配置Xml文件的

    這篇文章主要介紹了一文詳解Spring是怎么讀取配置Xml文件的,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下
    2022-08-08
  • SpringBoot整合RabbitMQ消息隊列的完整步驟

    SpringBoot整合RabbitMQ消息隊列的完整步驟

    這篇文章主要給大家介紹了關(guān)于SpringBoot整合RabbitMQ消息隊列的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05
  • Java Spring WEB應(yīng)用實例化如何實現(xiàn)

    Java Spring WEB應(yīng)用實例化如何實現(xiàn)

    這篇文章主要介紹了Java Spring WEB應(yīng)用實例化如何實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-12-12
  • mybatisplus自動填充屬性值的實現(xiàn)步驟

    mybatisplus自動填充屬性值的實現(xiàn)步驟

    MyBatis-Plus提供自動填充的功能,幫助自定設(shè)置這些字段的值,提升開發(fā)效率,本文就來介紹一下如何使用,感興趣的可以了解一下
    2023-12-12
  • RocketMQ 延時級別配置方式

    RocketMQ 延時級別配置方式

    這篇文章主要介紹了RocketMQ 延時級別配置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Java中的SynchronousQueue阻塞隊列及使用場景解析

    Java中的SynchronousQueue阻塞隊列及使用場景解析

    這篇文章主要介紹了Java中的SynchronousQueue阻塞隊列及使用場景解析,SynchronousQueue 是 Java 中的一個特殊的阻塞隊列,它的主要特點是它的容量為0,這意味著 SynchronousQueue不會存儲任何元素,需要的朋友可以參考下
    2023-12-12
  • java輸入數(shù)字,輸出倒序的實例

    java輸入數(shù)字,輸出倒序的實例

    這篇文章主要介紹了java輸入數(shù)字,輸出倒序的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • 用3個實例從原理到實戰(zhàn)講清楚Log4j史詩級漏洞

    用3個實例從原理到實戰(zhàn)講清楚Log4j史詩級漏洞

    最近應(yīng)該很多人都在關(guān)注著一個漏洞Apache Log4j 2遠(yuǎn)程代碼執(zhí)行,該漏洞一旦被攻擊者利用會造成嚴(yán)重危害,這篇文章主要給大家介紹了關(guān)于如何用3個實例從原理到實戰(zhàn)講清楚Log4j史詩級漏洞的相關(guān)資料,需要的朋友可以參考下
    2021-12-12
  • Spring中Bean創(chuàng)建完后打印語句的兩種方法

    Spring中Bean創(chuàng)建完后打印語句的兩種方法

    這篇文章主要介紹了Spring中Bean創(chuàng)建完后打印語句的兩種方法,一個是實現(xiàn)InitializingBean接口,另一個使用@Bean注解和initMethod屬性,通過代碼示例介紹的非常詳細(xì),感興趣的小伙伴可以參考閱讀
    2023-07-07
  • Spring Cloud重試機制與各組件的重試總結(jié)

    Spring Cloud重試機制與各組件的重試總結(jié)

    這篇文章主要給大家介紹了關(guān)于Spring Cloud中重試機制與各組件的重試的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-11-11

最新評論