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

SpringBoot中的雙數(shù)據(jù)源配置過程

 更新時間:2026年01月10日 15:23:36   作者:DXIANGH  
文章介紹了如何配置Oracle和PostgreSQL數(shù)據(jù)源,并使用JdbcTemplate進(jìn)行數(shù)據(jù)庫操作,重點包括數(shù)據(jù)源配置、JdbcTemplate配置以及不同數(shù)據(jù)源的包路徑設(shè)置

1.引入使用的數(shù)據(jù)源類型(mysql oracle sqlserver等)依賴

本文配置為oracel和postgresql

        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.3</version>
        </dependency>
         <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
        </dependency>
        

2.yml中配置數(shù)據(jù)源

3.數(shù)據(jù)源的配合

package com.cbd.findig.data.process.config;

import org.springframework.beans.factory.annotation.Qualifier;
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;

/**
 * 多數(shù)據(jù)源配置
 */
@Configuration
public class DataSourceConfig {

    //主數(shù)據(jù)源配置 ds1數(shù)據(jù)源
    @Primary
    @Bean(name = "ds1DataSourceProperties")
    @ConfigurationProperties(prefix = "spring.datasource.ds1")
    public DataSourceProperties ds1DataSourceProperties() {
        return new DataSourceProperties();
    }

    //主數(shù)據(jù)源 ds1數(shù)據(jù)源
    @Primary
    @Bean(name = "ds1DataSource")
    public DataSource ds1DataSource(@Qualifier("ds1DataSourceProperties") DataSourceProperties dataSourceProperties) {
        return dataSourceProperties.initializeDataSourceBuilder().build();
    }

    //第二個ds2數(shù)據(jù)源配置
    @Bean(name = "ds2DataSourceProperties")
    @ConfigurationProperties(prefix = "spring.datasource.ds2")
    public DataSourceProperties ds2DataSourceProperties() {
        return new DataSourceProperties();
    }

    //第二個ds2數(shù)據(jù)源
    @Bean("ds2DataSource")
    public DataSource ds2DataSource(@Qualifier("ds2DataSourceProperties") DataSourceProperties dataSourceProperties) {
        return dataSourceProperties.initializeDataSourceBuilder().build();
    }

}

4.JdbcTemplate的配置

package com.cbd.findig.data.process.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

/**
 * JdbcTemplate多數(shù)據(jù)源配置
 * 依賴于數(shù)據(jù)源配置
 *
 * @see DataSourceConfig
 */
@Configuration
public class JdbcTemplateDataSourceConfig {

    //JdbcTemplate主數(shù)據(jù)源ds1數(shù)據(jù)源
    @Primary
    @Bean(name = "ds1JdbcTemplate")
    public JdbcTemplate ds1JdbcTemplate(@Qualifier("ds1DataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    //JdbcTemplate第二個ds2數(shù)據(jù)源
    @Bean(name = "ds2JdbcTemplate")
    public JdbcTemplate ds2JdbcTemplate(@Qualifier("ds2DataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
}

5.分別配置兩個數(shù)據(jù)源的使用的包路徑

(注意換為自己的包路徑)

package com.cbd.findig.data.process.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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * Mybatis主數(shù)據(jù)源ds1配置
 * 多數(shù)據(jù)源配置依賴數(shù)據(jù)源配置
 * @see  DataSourceConfig
 */
@Configuration
@MapperScan(basePackages ="com.cbd.findig.data.process.mapper.**", sqlSessionTemplateRef  = "ds1SqlSessionTemplate")
public class MybatisPlusConfig4ds1 {

    //主數(shù)據(jù)源 ds1數(shù)據(jù)源
    @Primary
    @Bean("ds1SqlSessionFactory")
    public SqlSessionFactory ds1SqlSessionFactory(@Qualifier("ds1DataSource") DataSource dataSource) throws Exception {
       SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(dataSource);
        sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().
                        getResources("classpath*:com/cbd/findig/data/process/mapper/**/*.xml"));
        return sqlSessionFactory.getObject();
    }

    @Primary
    @Bean(name = "ds1TransactionManager")
    public DataSourceTransactionManager ds1TransactionManager(@Qualifier("ds1DataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Primary
    @Bean(name = "ds1SqlSessionTemplate")
    public SqlSessionTemplate ds1SqlSessionTemplate(@Qualifier("ds1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}
package com.cbd.findig.data.process.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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * Mybatis  第二個ds2數(shù)據(jù)源配置
 * 多數(shù)據(jù)源配置依賴數(shù)據(jù)源配置
 * @see  DataSourceConfig
 */
@Configuration
@MapperScan(basePackages ="com.cbd.findig.data.process.mapperds2.**", sqlSessionTemplateRef  = "ds2SqlSessionTemplate")
public class MybatisPlusConfig4ds2 {

    //ds2數(shù)據(jù)源
    @Bean("ds2SqlSessionFactory")
    public SqlSessionFactory ds2SqlSessionFactory(@Qualifier("ds2DataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(dataSource);
        sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().
                getResources("classpath*:com/cbd/findig/data/process/mapperds2/**/*.xml"));
        return sqlSessionFactory.getObject();
    }

//事務(wù)支持
    @Bean(name = "ds2TransactionManager")
    public DataSourceTransactionManager ds2TransactionManager(@Qualifier("ds2DataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "ds2SqlSessionTemplate")
    public SqlSessionTemplate ds2SqlSessionTemplate(@Qualifier("ds2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • java Timer 定時每天凌晨1點執(zhí)行任務(wù)

    java Timer 定時每天凌晨1點執(zhí)行任務(wù)

    這篇文章主要介紹了java Timer 定時每天凌晨1點執(zhí)行任務(wù)的代碼,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-09-09
  • Java使用觀察者模式實現(xiàn)氣象局高溫預(yù)警功能示例

    Java使用觀察者模式實現(xiàn)氣象局高溫預(yù)警功能示例

    這篇文章主要介紹了Java使用觀察者模式實現(xiàn)氣象局高溫預(yù)警功能,結(jié)合完整實例形式分析了java觀察者模式實現(xiàn)氣象局高溫預(yù)警的相關(guān)接口定義、使用、功能操作技巧,并總結(jié)了其設(shè)計原則與適用場合,具有一定參考借鑒價值,需要的朋友可以參考下
    2018-04-04
  • SpringBoot學(xué)習(xí)之Json數(shù)據(jù)交互的方法

    SpringBoot學(xué)習(xí)之Json數(shù)據(jù)交互的方法

    這篇文章主要介紹了SpringBoot學(xué)習(xí)之Json數(shù)據(jù)交互的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-12-12
  • ireport數(shù)據(jù)表格報表的簡單使用

    ireport數(shù)據(jù)表格報表的簡單使用

    這篇文章給大家介紹了如何畫一個報表模板,這里介紹下畫表格需要用到的組件,文中通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2021-10-10
  • Java 8 動態(tài)類型語言Lambda表達(dá)式實現(xiàn)原理解析

    Java 8 動態(tài)類型語言Lambda表達(dá)式實現(xiàn)原理解析

    Java 8支持動態(tài)語言,看到了很酷的Lambda表達(dá)式,對一直以靜態(tài)類型語言自居的Java,讓人看到了Java虛擬機(jī)可以支持動態(tài)語言的目標(biāo)。接下來通過本文給大家介紹Java 8 動態(tài)類型語言Lambda表達(dá)式實現(xiàn)原理分析,需要的朋友可以參考下
    2017-02-02
  • Spring?Cloud?Alibaba?Nacos服務(wù)治理平臺服務(wù)注冊、RestTemplate實現(xiàn)微服務(wù)之間訪問負(fù)載均衡訪問的問題

    Spring?Cloud?Alibaba?Nacos服務(wù)治理平臺服務(wù)注冊、RestTemplate實現(xiàn)微服務(wù)之間訪

    這篇文章主要介紹了Spring?Cloud?Alibaba:Nacos服務(wù)治理平臺,服務(wù)注冊、RestTemplate實現(xiàn)微服務(wù)之間訪問,負(fù)載均衡訪問,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • 基于Spring Boot應(yīng)用ApplicationEvent案例場景

    基于Spring Boot應(yīng)用ApplicationEvent案例場景

    這篇文章主要介紹了基于Spring Boot應(yīng)用ApplicationEvent,利用Spring的機(jī)制發(fā)布ApplicationEvent和監(jiān)聽ApplicationEvent,需要的朋友可以參考下
    2023-03-03
  • java多線程編程之從線程返回數(shù)據(jù)的兩種方法

    java多線程編程之從線程返回數(shù)據(jù)的兩種方法

    從線程中返回數(shù)據(jù)和向線程傳遞數(shù)據(jù)類似。也可以通過類成員以及回調(diào)函數(shù)來返回數(shù)據(jù)。但類成員在返回數(shù)據(jù)和傳遞數(shù)據(jù)時有一些區(qū)別,下面讓我們來看看它們區(qū)別在哪
    2014-01-01
  • java設(shè)計模式之裝飾器模式(Decorator)

    java設(shè)計模式之裝飾器模式(Decorator)

    這篇文章主要為大家詳細(xì)介紹了java設(shè)計模式之裝飾器模式Decorator,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • IDEA開發(fā)并部署運行WEB項目全過程

    IDEA開發(fā)并部署運行WEB項目全過程

    文章介紹了WEB項目標(biāo)準(zhǔn)結(jié)構(gòu)及部署方法,涵蓋目錄劃分(如WEB-INF、classes、lib)、核心文件(web.xml、index.html),以及三種部署方式:直接放置webapps、war包部署、自定義路徑配置,同時說明了IDEA中如何關(guān)聯(lián)Tomcat、配置項目結(jié)構(gòu)及部署原理
    2025-07-07

最新評論

庐江县| 定襄县| 凤城市| 深圳市| 竹溪县| 治县。| 敦化市| 宣汉县| 株洲市| 永吉县| 乃东县| 安平县| 招远市| 买车| 民权县| 博客| 东山县| 新营市| 西华县| 两当县| 桃源县| 洛川县| 洪湖市| 平塘县| 台南市| 南通市| 马鞍山市| 五指山市| 思茅市| 乃东县| 新营市| 新昌县| 兴宁市| 古蔺县| 雅江县| 哈密市| 旅游| 杭锦旗| 池州市| 莱阳市| 灌云县|