詳解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)文章希望大家以后多多支持腳本之家!
相關(guān)文章
配置hadoop環(huán)境mapreduce連接不上hdfs解決
這篇文章主要為大家介紹了配置hadoop環(huán)境mapreduce連接不上hdfs解決方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10spring boot過(guò)濾器FilterRegistrationBean實(shí)現(xiàn)方式
這篇文章主要介紹了spring boot過(guò)濾器FilterRegistrationBean實(shí)現(xiàn)方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10springboot 基于Tomcat容器的自啟動(dòng)流程分析
這篇文章主要介紹了springboot 基于Tomcat容器的自啟動(dòng)流程分析,Spring通過(guò)注解導(dǎo)入Bean大體可分為四種方式,我們主要來(lái)說(shuō)Import的兩種實(shí)現(xiàn)方法,需要的朋友可以參考下2020-02-02Java?基于Hutool實(shí)現(xiàn)DES加解密示例詳解
這篇文章主要介紹了Java基于Hutool實(shí)現(xiàn)DES加解密,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08