详解Springboot之整合JDBCTemplate配置多数据源
作者:程序员孙大圣 发布时间:2023-05-03 13:40:43
标签:springboot,JDBCTemplate,多数据源
一、前言
现在在我们的项目中,使用多数据源已经是很常见的,下面,这里总结一下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();
}
}
来源:https://blog.csdn.net/sunshunli/article/details/115663502


猜你喜欢
- 今日遇到一个问题:springboot需要获取到一个自定义名称文件夹下的静态资源(图片等),并且文件夹的路径不在classPath下面,而是
- 首先设定TextView的clickable属性为true。可以在布局文件中进行设定,比如:<TextViewandroid:id=&
- 本文实例为大家分享了SpringMVC框架实现图片上传与下载的具体代码,供大家参考,具体内容如下1、新建一个Maven webapp项目,引
- 随着C#的发展,该语言内容不断丰富,开发变得更加方便快捷,C# 的锋利尽显无疑。C# 语言从诞生起就是强类型语言,这一性质到今天不曾改变,我
- 没有借助任何第三方库,完全基于JAVA Socket实现一个最小化的HTTP文件下载客户端。完整的演示如何通过Socket实现下载文件的HT
- class ftp{ private string host = null; &n
- Android安装apk文件并适配Android 7.0详解首先在AndroidManifest.xml文件,activity同级节点注册p
- 代码如下: 实现1: BasicEditor.java package swt_jface.demo5; import java.io.Bu
- springboot jackson配置项目中使用的json是jackson。这个呢是spring boot自带的,一开始是用阿里的fast
- 一般来说, 泛型的作用就类似一个占位符, 或者说是一个参数, 可以让我们把类型像参数一样进行传递, 尽可能地复用代码。我有个朋友, 在使用的
- 一、闭包的定义。有很多不同的人都对闭包过进行了定义,这里收集了一些。# 是引用了自由变量的函数。这个函数通常被定义在另一个外部函数中,并且引
- 系列文章已完成,目录如下:jdk-logging log4j logback日志系统实现机制原理介绍commons-lo
- 前言这篇博客学习下Mybatis操作中使用Redis做缓存。这里其实主要学习几个注解:@CachePut、@Cacheable、@Cache
- 在程序中,进行类型转换是常见的事,C#支持基本的强制类型转换方法,例如:Object obj1 = new NewType();NewTyp
- 一、前言拷贝这个词想必大家都很熟悉,在工作中经常需要拷贝一份文件作为副本。拷贝的好处也很明显,相较于新建来说,可以节省很大的工作量。在Jav
- JWT简介Json Web Token(JWT):JSON网络令牌,是为了在网络应用环境间传递声明而制定的一种基于JSON的开放标准((RF
- 这篇文章主要介绍了springboot乱码问题解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋
- 定义最短路问题的定义为:下图左侧是一幅带权有向图,以顶点 0 为起点到各个顶点的最短路径形成的最短路径树如下图右侧所示:带权有向图的实现在实
- 通过继承Thread类并实现run方法创建一个线程// 定义一个Thread类,相当于一个线程的模板class MyThread01 ext
- 前言在开发过程中经常会遇到比较排序的问题,比如说对集合数组的排序等情况,基本类型都提供了默认的比较算法,如string提供了按字母进行排序,