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

star2017 1年前 ⋅ 1833 阅读

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

https://blog.csdn.net/u013294097/article/details/86274427

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



如果觉得本文对您有所帮助,欢迎您扫码下图所示的支付宝和微信支付二维码对本文进行随意打赏。您的支持将鼓励我继续创作

更多内容请访问:IT源点

相关文章推荐

全部评论: 0

    我有话说: