在现代应用程序中,数据库会根据业务用途进行分库,因此后端程序需要连接多个数据库,以满足不同的业务需求。Spring Boot 提供了便捷的方式来配置 MyBatis 的多数据源。本文将为读者介绍如何在 Spring Boot 中实现 MyBatis 的多数据源配置。
1、项目依赖
<dependencies>
<!-- Spring Boot Starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- MyBatis Starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.6</version>
</dependency>
<!-- 数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
</dependencies>
2、配置文件
spring:
datasource:
db1:
url: jdbc:mysql://localhost:3306/db1
username: root1
password: password1
driver-class-name: com.mysql.cj.jdbc.Driver
db2:
url: jdbc:mysql://localhost:3306/db2
username: root2
password: password2
driver-class-name: com.mysql.cj.jdbc.Driver
# 配置mybatis的实体包路径,以及xml存放路径
mybatis:
type-aliases-package: com.xxx.pojo
mapper-locations: classpath:mapper/db1/*.xml,classpath:mapper/db2/*.xm
3、配置数据源
@Configuration
public class DataSourceConfig {
@Bean(name = "master")
@Primary
@ConfigurationProperties(prefix = "spring.datasource.db1")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "slave")
@ConfigurationProperties(prefix = "spring.datasource.db2")
public DataSource secondDataSource() {
return DataSourceBuilder.create().build();
}
}
@Primary 注解指定db1为默认数据源。
配置指定mapper接口路径和xml文件路径。
MyBatisConfigPrimary.java
@Configuration
@MapperScan(basePackages = "cn.xxx.xxx.mapper.db1", sqlSessionFactoryRef = "primarySqlSessionFactory")
public class MyBatisConfigPrimary {
@Bean(name = "primarySqlSessionFactory")
@Primary
public SqlSessionFactory primarySqlSessionFactory(@Qualifier("master") DataSource datasource)
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(datasource);
bean.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/db1/*.xml"));
return bean.getObject();// 设置mybatis的xml所在位置
}
}
MyBatisConfigSecondary.java
@Configuration
@MapperScan(basePackages = "cn.xxx.xxx.mapper.db2", sqlSessionFactoryRef = "secondarySqlSessionFactory")
public class MyBatisConfigSecondary {
@Bean(name = "secondarySqlSessionFactory")
public SqlSessionFactory secondarySqlSessionFactory(@Qualifier("slave") DataSource datasource)
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(datasource);
bean.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/db2/*.xml"));
return bean.getObject();
}
}
4、创建Mapper接口和XML文件
根据数据源的配置,我们在 src/main/resources/mapper/db1 和 src/main/resources/mapper/db2 下分别创建 XML 文件,在cn.xxx.xxx.mapper.db1 和 cn.xxx.xxx.mapper.db2 定义相应的 Mapper 接口。
5、使用
@Service
public class MyService {
@Autowired
private Db1Mapper db1Mapper;
@Autowired
private Db2Mapper db2Mapper;
public void execute() {
List<Table> dataFromDb1 = db1Mapper.queryAll();
}
}