詳解Springboot之整合JDBCTemplate配置多數(shù)據(jù)源
一、前言
現(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)文章希望大家以后多多支持腳本之家!
- spring boot使用sharding jdbc的配置方式
- 詳解springboot采用多數(shù)據(jù)源對(duì)JdbcTemplate配置的方法
- springboot2.0.0配置多數(shù)據(jù)源出現(xiàn)jdbcUrl is required with driverClassName的錯(cuò)誤
- SpringBoot多數(shù)據(jù)源配置詳細(xì)教程(JdbcTemplate、mybatis)
- springboot+springJdbc+postgresql 實(shí)現(xiàn)多數(shù)據(jù)源的配置
- springboot實(shí)現(xiàn)以代碼的方式配置sharding-jdbc水平分表
- SpringBoot3+ShardingJDBC5.5.0 讀寫分離配置的實(shí)現(xiàn)
- SpringBoot?配置多個(gè)JdbcTemplate的實(shí)現(xiàn)步驟
- SpringBoot+MybatisPlus+jdbc連接池配置多數(shù)據(jù)源的實(shí)現(xiàn)
- Spring?JDBC配置與使用的實(shí)現(xiàn)
相關(guān)文章
Java紅黑樹的數(shù)據(jù)結(jié)構(gòu)與算法解析
紅黑樹問(wèn)題是各大計(jì)算機(jī)考研命題以及面試算法題目中的熱門,接下來(lái)我們?yōu)榇蠹覉D解紅黑樹的數(shù)據(jù)結(jié)構(gòu)與算法解析,需要的朋友可以參考下2021-08-08SpringBoot如何動(dòng)態(tài)改變?nèi)罩炯?jí)別
這篇文章主要介紹了SpringBoot如何動(dòng)態(tài)改變?nèi)罩炯?jí)別,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下2020-12-12Java簡(jiǎn)單計(jì)算兩個(gè)日期月數(shù)差的方法
這篇文章主要介紹了Java簡(jiǎn)單計(jì)算兩個(gè)日期月數(shù)差的方法,結(jié)合實(shí)例形式分析了java使用Calendar類進(jìn)行日期時(shí)間操作相關(guān)技巧,需要的朋友可以參考下2017-06-06教你用IDEA配置JUnit并進(jìn)行單元測(cè)試
今天教各位小伙伴怎么用IDEA配置JUnit并進(jìn)行單元測(cè)試,文中有非常詳細(xì)的圖文介紹及代碼示例,對(duì)正在學(xué)習(xí)IDEA的小伙伴有很好的幫助,需要的朋友可以參考下2021-05-05Java ThreadLocal的設(shè)計(jì)理念與作用
這篇文章主要介紹了Java ThreadLocal的設(shè)計(jì)理念與作用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03Java使用HttpClient實(shí)現(xiàn)圖片下載與本地保存功能
在當(dāng)今數(shù)字化時(shí)代,網(wǎng)絡(luò)資源的獲取與處理已成為軟件開發(fā)中的常見(jiàn)需求,其中,圖片作為網(wǎng)絡(luò)上最常見(jiàn)的資源之一,其下載與保存功能在許多應(yīng)用場(chǎng)景中都顯得尤為重要,本文將通過(guò)一個(gè)具體的Java實(shí)戰(zhàn)案例,詳細(xì)介紹如何使用Apache?HttpClient庫(kù)實(shí)現(xiàn)圖片的下載與本地保存功能2025-06-06