本文是《MyBatis初级实战》系列的第四篇,一个springboot应用同时操作两个数据库的场景,在平时也会遇到,今天要实战的就是通过druid配置两个数据源,让一个springboot应用同时使用这两个数据源;
多数据源配置的基本思路
-
首先要明确的是:数据源是通过配置类实现的,因此要去掉springboot中和数据源相关的自动装配;
-
最核心的问题有两个,第一个是确定表和数据源的关系,这个关系是在SqlSessionFactory实例中确立的,代码如下所示:
@Bean(name = “secondSqlSessionFactory”)
public SqlSessionFactory sqlSessionFactory(@Qualifier(“secondDataSource”) DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(“classpath:mappers/second/**/*Mapper.xml”));
return bean.getObject();
}
- 第二个核心问题是包扫描,即指定的mapper接口要使用指定的sqlSessionTemplat,这个关系在SqlSessionTemplate配置类中(相当于旧版的xml配置bean),如下图所示:
-
从上述代码可见,如果上层的业务代码想操作secondDataSource这个数据源的表,只要把对应的*Mapper.xml文件和Mapper接口文件对应的目录下即可;
-
整个配置的关键步骤如下图所示:
实战概览
本次实战的内容如下:
-
一共有两个数据库:mybatis和mybatis_second;
-
mybatis中有名为user的表,mybatis_second中有名为address的表;
-
新建名为druidtwosource的springboot应用,里面有两个controller,可以分别对user、address这两个表进行操作;
-
编写单元测试用例,通过调用controller接口验证应用功能正常;
-
启动springboot应用,通过swagger验证功能正常;
-
进入druid监控页面;
源码下载
- 如果您不想编码,可以在GitHub下载所有源码,地址和链接信息如下表所示(https://github.com/zq2599/blog_demos):
| 名称 | 链接 | 备注 |
| :-- | :-- | :-- |
| 项目主页 | https://github.com/zq2599/blog_demos | 该项目在GitHub上的主页 |
| git仓库地址(https) | https://github.com/zq2599/blog_demos.git | 该项目源码的仓库地址,https协议 |
| git仓库地址(ssh) | git@github.com:zq2599/blog_demos.git | 该项目源码的仓库地址,ssh协议 |
- 这个git项目中有多个文件夹,本章的应用在mybatis文件夹下,如下图红框所示:
创建数据库和表
- 创建名为mybatis的数据库,建表语句如下:
DROP TABLE IF EXISTS user
;
CREATE TABLE user
(
id
int(32) NOT NULL AUTO_INCREMENT,
name
varchar(32) NOT NULL,
age
int(32) NOT NULL,
PRIMARY KEY (id
)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
- 创建名为mybatis_second的数据库,建表语句如下:
DROP TABLE IF EXISTS address
;
CREATE TABLE address
(
id
int(32) NOT NULL AUTO_INCREMENT,
city
varchar(32) NOT NULL,
street
varchar(32) NOT NULL,
PRIMARY KEY (id
)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
编码
- 前文《MyBatis初级实战之一:Spring Boot集成》创建了父工程mybatis,本文继续在此工程中新增子工程,名为druidtwosource,先提前看整个子工程文件结构,如下图,要注意的是红框1中的mapper接口,以及红框2中的mapper映射文件,这两处都按照数据库的不同放入各自文件夹:
- druidtwosource工程的pom.xml内容如下:
<project xmlns=“http://maven.apache.org/POM/4.0.0” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=“http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd”>
4.0.0
com.bolingcavalry
mybatis
1.0-SNAPSHOT
…/pom.xml
com.bolingcavalry
druidtwosource
0.0.1-SNAPSHOT
druidtwosource
Demo project for Mybatis Druid (two datasource) in Spring Boot
<java.version>1.8</java.version>
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
io.springfox
springfox-swagger2
io.springfox
springfox-swagger-ui
com.alibaba
druid-spring-boot-starter
junit
junit
test
com.google.code.gson
gson
org.springframework.boot
spring-boot-maven-plugin
- 配置文件application.yml,可见这里面有first和second两个数据源配置,而druid的web-stat-filter和stat-view-servlet这两个配置是公用的: