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

詳解Springboot之整合JDBCTemplate配置多數(shù)據(jù)源

 更新時(shí)間:2021年04月20日 11:01:56   作者:程序員孫大圣  
這篇文章主要介紹了詳解Springboot之整合JDBCTemplate配置多數(shù)據(jù)源,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有很好的幫助,需要的朋友可以參考下

一、前言

現(xiàn)在在我們的項(xiàng)目中,使用多數(shù)據(jù)源已經(jīng)是很常見(jiàn)的,下面,這里總結(jié)一下springboot整合jdbcTemplate配置多數(shù)據(jù)源的代碼示例,以方便以后直接使用.

二、配置文件

spring:
  datasource:
    datasourceone:
      driverClassName: com.mysql.jdbc.Driver
      url: jdbc:mysql://localhost:3306/eesy?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false
      username: root
      password: root
    dataSourcetwo:
      driverClassName: com.mysql.jdbc.Driver
      url: jdbc:mysql://localhost:3306/eesy?serverTimezone=UTC&characterEncoding=utf8&useUnicode=true&useSSL=false
      username: root
      password: root

三、數(shù)據(jù)源配置類

package com.ssl.datasource.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 org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
 
import javax.sql.DataSource;
 
@Configuration
public class DataSourceOne {
 
    @Bean("name-template-one")
    public NamedParameterJdbcTemplate namedParameterJdbcTemplate(@Qualifier("datasource-one") DataSource dataSource){
        return new NamedParameterJdbcTemplate(dataSource);
    }
 
    @Bean("template-one")
    public JdbcTemplate jdbcTemplate(@Qualifier("datasource-one") DataSource dataSource){
        return new JdbcTemplate(dataSource);
    }
 
    @Bean("datasource-one")
    public DataSource dataSource(@Qualifier("jdbc-config-one") DataSourceProperties dataSourceProperties){
        return dataSourceProperties.initializeDataSourceBuilder().build();
    }
 
    @Primary
    @Bean("jdbc-config-one")
    @ConfigurationProperties(prefix = "spring.datasource.datasourceone")
    public DataSourceProperties properties(){
        return new DataSourceProperties();
    }
}
package com.ssl.datasource.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 org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
 
import javax.sql.DataSource;
 
@Configuration
public class DataSourceTwo {
 
    @Bean("name-template-two")
    public NamedParameterJdbcTemplate namedParameterJdbcTemplate(@Qualifier("datasource-two") DataSource dataSource){
        return new NamedParameterJdbcTemplate(dataSource);
    }
 
    @Bean("template-two")
    public JdbcTemplate jdbcTemplate(@Qualifier("datasource-two") DataSource dataSource){
        return new JdbcTemplate(dataSource);
    }
 
    @Bean("datasource-two")
    public DataSource dataSource(@Qualifier("jdbc-config-two") DataSourceProperties dataSourceProperties){
        return dataSourceProperties.initializeDataSourceBuilder().build();
    }
 
    @Bean("jdbc-config-two")
    @ConfigurationProperties(prefix = "spring.datasource.datasourcetwo")
    public DataSourceProperties properties(){
        return new DataSourceProperties();
    }
}

到此這篇關(guān)于詳解Springboot之整合JDBCTemplate配置多數(shù)據(jù)源的文章就介紹到這了,更多相關(guān)springboot整合JDBCTemplate內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 配置hadoop環(huán)境mapreduce連接不上hdfs解決

    配置hadoop環(huán)境mapreduce連接不上hdfs解決

    這篇文章主要為大家介紹了配置hadoop環(huán)境mapreduce連接不上hdfs解決方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10
  • 解讀easyexcel中的常用注解

    解讀easyexcel中的常用注解

    這篇文章主要介紹了關(guān)于easyexcel中的常用注解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • Java集合遍歷實(shí)現(xiàn)方法及泛型通配

    Java集合遍歷實(shí)現(xiàn)方法及泛型通配

    這篇文章主要介紹了Java集合遍歷實(shí)現(xiàn)方法及泛型通配,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-07-07
  • Java枚舉學(xué)習(xí)之定義和基本特性詳解

    Java枚舉學(xué)習(xí)之定義和基本特性詳解

    枚舉是JAVA?5.0后增加的一個(gè)重要類型??梢杂脕?lái)表示一組取值范圍固定的變量。本文將通過(guò)示例為大家詳細(xì)講解枚舉的定義和基本特性,感興趣的可以了解一下
    2022-08-08
  • Hashmap非線程安全關(guān)于hash值沖突處理

    Hashmap非線程安全關(guān)于hash值沖突處理

    這篇文章主要為大家介紹了Hashmap非線程安全關(guān)于hash值沖突的處理,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-04-04
  • spring boot過(guò)濾器FilterRegistrationBean實(shí)現(xiàn)方式

    spring boot過(guò)濾器FilterRegistrationBean實(shí)現(xiàn)方式

    這篇文章主要介紹了spring boot過(guò)濾器FilterRegistrationBean實(shí)現(xiàn)方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • Spring集成Redis詳解代碼示例

    Spring集成Redis詳解代碼示例

    這篇文章主要介紹了Spring集成Redis詳解代碼示例,介紹了Eclipse工程結(jié)構(gòu),POM依賴,Spring配置,Redis配置信息以及Java代碼等相關(guān)內(nèi)容,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-11-11
  • springboot 基于Tomcat容器的自啟動(dòng)流程分析

    springboot 基于Tomcat容器的自啟動(dòng)流程分析

    這篇文章主要介紹了springboot 基于Tomcat容器的自啟動(dòng)流程分析,Spring通過(guò)注解導(dǎo)入Bean大體可分為四種方式,我們主要來(lái)說(shuō)Import的兩種實(shí)現(xiàn)方法,需要的朋友可以參考下
    2020-02-02
  • Java?基于Hutool實(shí)現(xiàn)DES加解密示例詳解

    Java?基于Hutool實(shí)現(xiàn)DES加解密示例詳解

    這篇文章主要介紹了Java基于Hutool實(shí)現(xiàn)DES加解密,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-08-08
  • Spring Security基本原理詳解

    Spring Security基本原理詳解

    這篇文章主要介紹了Spring Security基本原理詳解,springsecurity底層實(shí)現(xiàn)為一條過(guò)濾器鏈,就是用戶請(qǐng)求進(jìn)來(lái),判斷有沒(méi)有請(qǐng)求的權(quán)限,拋出異常,重定向跳轉(zhuǎn),需要的朋友可以參考下
    2023-05-05

最新評(píng)論