如果想要看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