首页 > 其他分享 >spring的jdbcTemplate的多数据源的配置,springboot的jdbcTemplate的多数据源的配置

spring的jdbcTemplate的多数据源的配置,springboot的jdbcTemplate的多数据源的配置

时间:2023-01-06 14:33:44浏览次数:37  
标签:spring 数据源 springframework jdbcTemplate context org import


如果想要看springboot配置mybatis的多数据源,请参看本人博客:

1、springboot中配置

案例中使用的数据源是阿里巴巴的druid,其他数据源是一样的

1)创建两个数据源

yml中配置两个不同的前缀的数据源

spring:
datasource:
#第一个数据源
username: root
password: root
url: jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowMultiQueries=true
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
#第二个数据源
secondary:
#第一个数据源
username: root
password: root
url: jdbc:mysql://localhost:3306/mysql2?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowMultiQueries=true
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true

数据源1:

package com.cyjz.config;

import com.alibaba.druid.pool.DruidDataSource;
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 javax.sql.DataSource;


@Configuration
public class DataSourceOneConfig {


@Bean
@ConfigurationProperties(prefix = "spring.datasource")
@Primary //设置主数据源1
public DataSource dataSource(){
return new DruidDataSource();
}
}

数据源2:

package com.cyjz.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;


@Configuration
public class DataSourceTwoConfig {

@Bean(name = "dataSourceTwo")
@ConfigurationProperties(prefix = "spring.datasource.secondary")
public DataSource DataSourceOne(){
return new DruidDataSource();
}
}

2)创建一个JdbcTemplate的bean

package com.cyjz.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

@Configuration
public class JDBCTemplate {

@Bean(name = "jdbcTemplate")
public JdbcTemplate jdbcTemplate(@Qualifier("dataSource") DataSource dataSource){
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(dataSource);
return jdbcTemplate;
}

@Bean(name = "jdbcTemplate2")
public JdbcTemplate jdbcTemplate2(@Qualifier("dataSourceTwo") DataSource dataSource){
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(dataSource);
return jdbcTemplate;
}

}

3)调用的时候

@Autowired
@Qualifier("jdbcTemplate")
private JdbcTemplate jdbcTemplate;

@Autowired
@Qualifier("jdbcTemplate2")
private JdbcTemplate jdbcTemplate2;

注意@Qualifier,引用的就是bean的名字

2.spring项目中配置jdbcTemplate多数据源

1)配置文件:

jdbc.url=jdbc\:mysql\://localhost\:3306/mysql?useUnicode\=true&characterEncoding\=utf8&autoReconnect\=true&useSSL\=false
jdbc.username=root
jdbc.password=mysql
jdbc.driverClassName=com.mysql.jdbc.Driver

jdbc2.url=jdbc\:mysql\://localhost\:3306/mysql?useUnicode\=true&characterEncoding\=utf8&autoReconnect\=true&useSSL\=false
jdbc2.username=root
jdbc2.password=mysql
jdbc2.driverClassName=com.mysql.jdbc.Driver

2)spring的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">

<!-- 加载config下面的jdbc.properties文件 -->
<context:property-placeholder location="classpath:config/jdbc.properties" />

<!-- 配置多数据源1DataSource -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource#DruidDataSource()">
<property name="driver" value="${jdbc.driverName}"/>
<property name="driverUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>

<!-- 配置多数据源sdconddataSource -->
<bean id="seconddataSource" class="com.alibaba.druid.pool.DruidDataSource#DruidDataSource()">
<property name="driver" value="${jdbc2.driverName}"/>
<property name="driverUrl" value="${jdbc2.url}"/>
<property name="user" value="${jdbc2.username}"/>
<property name="password" value="${jdbc2.password}"/>
</bean>

<!-- jdbc关联数据源 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="jdbcTemplate2" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="seconddataSource"/>
</bean>

</beans>

3)调用同springboot

 

 

标签:spring,数据源,springframework,jdbcTemplate,context,org,import
From: https://blog.51cto.com/u_15932265/5993539

相关文章

  • spock做post请求get请求,在springboot环境下使用gradle构建工具的demo,IDEA的开发工具
    1、创建一个springboot项目,基于gradle的创建1)new一个project2)选择springinitializr3)选择gradleproject,然后next4)选择一个web,然后next,然后finish2.打开build.gradle,设......
  • SpringBoot Aop 日志记录
    文章目录​​前言​​​​一、Aop​​​​1.基本概念​​​​2.术语理解​​​​3.通知类型​​​​二、SpringBootAop整合​​​​1.引入依赖​​​​2.注解​​​​3.切......
  • Spring之AOP
    AOP是什么?AOP:在软件业,AOP为AspectOrientedProgramming的缩写,意为:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软......
  • Spring(一)
    Spring1.介绍1.1概述Spring是最受欢迎的企业级Java应用程序开发框架,数以百万的来自世界各地的开发人员使用Spring框架来创建性能好、易于测试、可重用的代码。Spr......
  • Springboot整合策略模式概念->使用场景->优缺点->企业级实战
    一、前言策略模式可能是在工作中使用最多的,也是在面试中最常提到的,代码重构和优化的必备!小编之前也是一直说,其实没有真正的实战;最近有了机会实战了一下,来分享一下使用心......
  • 使用React动画库——react-spring
    使用React动画库——react-spring虚拟J关注IP属地:浙江0.722019.11.0816:28:04字数644阅读16,174为了让后台系统视觉体验更好,决定增加过渡动画效果。React官......
  • SpringBoot过滤器/拦截器
    不同点项过滤器拦截器使用场景对请求/响应进行修改、判断等。一般用于过滤参数、登录权限验证、资源访问权限控制、敏感词汇过滤、字符编码转换。在service或者一个方法前......
  • SpringCloud 2020.x.x工程bootstrap引导配置不生效的解决方案
      关注公众号风色年代(itfantasycc)500GJava微服务资料合集送上~注意:2020版本以后,添加spring-cloud-context是没有用的,因为官方重构了bootstrap引导配置的加载方式需要增......
  • springMVC常用注解介绍
    @Controller注解一个类表示控制器,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model......
  • springboot使用redis实现计数限流
    lua脚本resources下创建文件redis/AccessLimit.lua内容为:locallimitSecond=tonumber(ARGV[1])locallimitMaxCount=tonumber(ARGV[2])localnum=tonumber(......