詳解Springboot之整合JDBCTemplate配置多數據源
更新時間:2021年04月20日 11:01:56 作者:程序員孫大圣
這篇文章主要介紹了詳解Springboot之整合JDBCTemplate配置多數據源,文中有非常詳細的代碼示例,對正在學習java的小伙伴們有很好的幫助,需要的朋友可以參考下
一、前言
現在在我們的項目中,使用多數據源已經是很常見的,下面,這里總結一下springboot整合jdbcTemplate配置多數據源的代碼示例,以方便以后直接使用.
二、配置文件
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
三、數據源配置類
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();
}
}
到此這篇關于詳解Springboot之整合JDBCTemplate配置多數據源的文章就介紹到這了,更多相關springboot整合JDBCTemplate內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
- spring boot使用sharding jdbc的配置方式
- 詳解springboot采用多數據源對JdbcTemplate配置的方法
- springboot2.0.0配置多數據源出現jdbcUrl is required with driverClassName的錯誤
- SpringBoot多數據源配置詳細教程(JdbcTemplate、mybatis)
- springboot+springJdbc+postgresql 實現多數據源的配置
- springboot實現以代碼的方式配置sharding-jdbc水平分表
- SpringBoot3+ShardingJDBC5.5.0 讀寫分離配置的實現
- SpringBoot?配置多個JdbcTemplate的實現步驟
- SpringBoot+MybatisPlus+jdbc連接池配置多數據源的實現
- Spring?JDBC配置與使用的實現

