Spring Mybatis 多数据源配置
大约 2 分钟
Spring Mybatis 多数据源配置
项目结构
Maven 依赖
基本的 spring boot 和 mybatis 配置, web 用来测试
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
配置文件
spring.datasource 路径并非必须,可以自定义。 db1, db2 分别为两个数据源
spring:
datasource:
db1:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
jdbc-url: jdbc:mysql://localhost:3306/test
db2:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
jdbc-url: jdbc:mysql://localhost:3306/test-secondary
Mybatis 配置
两份配置文件并没有结构上的差异,只需要指定哪些 dao 使用哪个 sqlSessionFactory, sqlSessionFactory 再指定 要使用哪个数据源和 xml 文件位置
数据源1
package cn.linkot.config;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
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.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
@Configuration
@MapperScan(
basePackages = "cn.linkot.dao.db1",
sqlSessionFactoryRef = "db1SqlSessionFactory"
)
public class MybatisDB1 {
@Bean(name = "ds1")
@ConfigurationProperties(prefix = "spring.datasource.db1")
public DataSource dataSource(){
return DataSourceBuilder.create().build();
}
@Bean(name = "db1SqlSessionFactory")
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
factory.setDataSource(dataSource());
factory.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/db1/**/*.xml"));
return factory.getObject();
}
}
数据源2
package cn.linkot.config;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
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.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
@Configuration
@MapperScan(
basePackages = "cn.linkot.dao.db2",
sqlSessionFactoryRef = "db2SqlSessionFactory"
)
public class MybatisDB2 {
@Bean(name = "ds2")
@ConfigurationProperties(prefix = "spring.datasource.db2")
public DataSource dataSource(){
return DataSourceBuilder.create().build();
}
@Bean(name = "db2SqlSessionFactory")
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
factory.setDataSource(dataSource());
factory.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/db2/**/*.xml"));
return factory.getObject();
}
}
DataSource
这里使用 @ConfigurationProperties(prefix = "spring.datasource.db1")
指定使用的配置,与配置文件部分的路径一致
SqlSessionFactory
factory.setDataSource(dataSource());
指定了要使用的数据源 factory.setMapperLocations
指定了 xml 映射文件的路径
@MapperScan
basePackages
为 dao 文件的路径 sqlSessionFactoryRef
为 sqlSessionFactory 的 beanName
测试
测试
测试接口
package cn.linkot.controller;
import cn.linkot.dao.db1.DB1UserDao;
import cn.linkot.dao.db2.DB2UserDao;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@AllArgsConstructor
public class TestController {
private DB1UserDao db1UserDao;
private DB2UserDao db2UserDao;
@GetMapping("db1")
public Object db1(){
return db1UserDao.queryAll(null);
}
@GetMapping("db2")
public Object db2(){
return db2UserDao.queryAll(null);
}
}
结果
/db1
[
{
"id": 3,
"name": "操作测试",
"status": 0
},
{
"id": 4,
"name": "张三",
"status": 1
}
]
/db2
[
{
"id": 1,
"name": "zs",
"status": null
},
{
"id": 2,
"name": "ls",
"status": null
}
]
区别
相比较单数据源的自动配置,多数据源需要指定多个 sqlSessionFactory 使用不同的数据源,mapperLocations 再指定 xml 文件的位置