一、为什么需要多数据源
在项目中,可能会出现需要查询不同数据库的表的情况,所以这就需要配置多数据源。原则上可以配置无数个数据源,但是需要硬件的支持(内存等)。
二、整合多数据源的两种方式
- 分包,根据不同的业务划分不同包名作为区分
- 根据注解的方式,自定义注解
三、具体实现
本人才疏学浅,这里就先实现第一种方式——分包,开发以及技术版本环境:eclipse、springboot2.0、jdk1.8
1.项目结构
根据不同的业务,我这里划分test01、test02两个包
2.数据库设计
3.pom文件依赖
<dependencies>
<!-- jdbcTemplate 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- mysql 依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!-- 测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- springboot-web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Lombok插件 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
4.配置文件中新增两个数据源
在配置文件中自定义数据源,spring.datasource.test1.****、spring.datasource.test2.****、前半段是自定义的
###datasource1
spring.datasource.test1.driver-class-name = com.mysql.jdbc.Driver
spring.datasource.test1.jdbc-url = jdbc:mysql://localhost:3306/test01?useUnicode=true&characterEncoding=utf-8
spring.datasource.test1.username = root
spring.datasource.test1.password = root
###datasource2
spring.datasource.test2.driver-class-name = com.mysql.jdbc.Driver
spring.datasource.test2.jdbc-url = jdbc:mysql://localhost:3306/test02?useUnicode=true&characterEncoding=utf-8
spring.datasource.test2.username = root
spring.datasource.test2.password = root
5.配置数据源
因为在配置文件中定义了两个数据源,但是spring boot是无法识别的,所以就需要写两个配置文件去配置数据源的相关内容
DataSource1Config.java
package com.zhu.dataSource;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
//DataSource01
@Configuration // 注册到springboot容器中
@MapperScan(basePackages = "com.zhu.test01.mapper", sqlSessionFactoryRef = "test1SqlSessionFactory")//basePackages的地址要写全
public class DataSource1Config {
/**
* 功能描述:(配置test1数据库)
* @return
*/
@Bean(name = "test1DataSource")
@ConfigurationProperties(prefix = "spring.datasource.test1")
@Primary
public DataSource testDataSource() {
return DataSourceBuilder.create().build();
}
/**
* 功能描述:(test1 sql会话工厂)
* @param dataSource
* @return
* @throws Exception
*/
@Bean(name = "test1SqlSessionFactory")
@Primary
public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource)
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
// bean.setMapperLocations(
// new
// PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/test1/*.xml"));
return bean.getObject();
}
/**
* 功能描述:(test1 事物管理)
* @param dataSource
* @return
*/
@Bean(name = "test1TransactionManager")
@Primary
public DataSourceTransactionManager testTransactionManager(@Qualifier("test1DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "test1SqlSessionTemplate")
@Primary
public SqlSessionTemplate testSqlSessionTemplate(
@Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
DataSource2Config.jave
package com.zhu.dataSource;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
//DataSource2
@Configuration // 注册到springboot容器中
@MapperScan(basePackages = "com.zhu.test02.mapper", sqlSessionFactoryRef = "test2SqlSessionFactory")
public class DataSource2Config {
/**
* 功能描述:(配置test2数据库)
* @return
*/
@Bean(name = "test2DataSource")
@ConfigurationProperties(prefix = "spring.datasource.test2")
public DataSource testDataSource() {
return DataSourceBuilder.create().build();
}
/**
* 功能描述:(test2 sql会话工厂)
* @param dataSource
* @return
* @throws Exception
*/
@Bean(name = "test2SqlSessionFactory")
public SqlSessionFactory testSqlSessionFactory(@Qualifier("test2DataSource") DataSource dataSource)
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
// bean.setMapperLocations(
// new
// PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/test1/*.xml"));
return bean.getObject();
}
/**
* 功能描述:(test2 事物管理)
* @param dataSource
* @return
*/
@Bean(name = "test2TransactionManager")
public DataSourceTransactionManager testTransactionManager(@Qualifier("test2DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "test2SqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(
@Qualifier("test2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
@Primary:指定主数据源,spring boot2.0以下版本必须加上这个注解,否则会报错,2.0可以不需要指定
@MapperScan(basePackages = "com.zhu.test01.mapper", sqlSessionFactoryRef = "test1SqlSessionFactory")
@MapperScan(basePackages = "com.zhu.test02.mapper", sqlSessionFactoryRef = "test2SqlSessionFactory")
包扫描,告诉spring boot我们的mapper层的位置。
6.spring boot整合多数据源demo下载
https://download.csdn.net/download/qq_22774179/11504826