项目里经常会有多数据源的场景,之前没来得及了解配置细节。
趁机补一补。再此记录一次 SpringBoot 配置MyBatis 多数据源的完整流程。
pom配置
关键依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
项目配置文件
application.properties关键配置
# 数据源1
spring.datasource.current.jdbc-url=jdbc:mysql://ip1:3306/task
spring.datasource.current.username=root
spring.datasource.current.password=root
spring.datasource.current.driver-class-name=com.mysql.cj.jdbc.Driver
# 数据源2
spring.datasource.other.jdbc-url=jdbc:mysql://ip2/task
spring.datasource.other.username=root
spring.datasource.other.password=root
spring.datasource.other.driver-class-name=com.mysql.cj.jdbc.Driver
多数据源配置(关键)
配置各个数据源,注意扫描包配置basePackages 以及配置文件读取的不同点。
主数据源
@Configuration
@MapperScan(basePackages = "com.example.demo.dao.current", sqlSessionFactoryRef = "currentSqlSessionFactory")
public class DataSourceConfigCurrent {
// 将这个对象放入Spring容器中
@Bean(name = "currentDataSource")
// 表示这个数据源是默认数据源
@Primary
// 读取application.properties中的配置参数映射成为一个对象
// prefix表示参数的前缀
@ConfigurationProperties(prefix = "spring.datasource.current")
public DataSource getDateSource1()
{
return DataSourceBuilder.create().build();
}
@Bean(name = "currentSqlSessionFactory")
// 表示这个数据源是默认数据源
@Primary
// @Qualifier表示查找Spring容器中名字为currentDataSource的对象
public SqlSessionFactory currentSqlSessionFactory(@Qualifier("currentDataSource") DataSource datasource)
throws Exception
{
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(datasource);
bean.setMapperLocations(
// 设置mybatis的xml所在位置, 注意是 xml 所在位置。
new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/current/*.xml"));
return bean.getObject();
}
@Bean("currentSqlSessionTemplate")
// 表示这个数据源是默认数据源
@Primary
public SqlSessionTemplate test1SqlSessionTemplate(
@Qualifier("currentSqlSessionFactory") SqlSessionFactory sessionFactory)
{
return new SqlSessionTemplate(sessionFactory);
}
}
其他数据源
配置更多数据源也是如法炮制。
@Configuration
@MapperScan(basePackages = "com.example.demo.dao.other", sqlSessionFactoryRef = "otherSqlSessionFactory")
public class DataSourceConfigOther {
@Bean(name = "otherDataSource")
@ConfigurationProperties(prefix = "spring.datasource.other")
public DataSource getDateSource2()
{
return DataSourceBuilder.create().build();
}
@Bean(name = "otherSqlSessionFactory")
public SqlSessionFactory test2SqlSessionFactory(@Qualifier("otherDataSource") DataSource datasource)
throws Exception
{
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(datasource);
bean.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/other/*.xml"));
return bean.getObject();
}
@Bean("otherSqlSessionTemplate")
public SqlSessionTemplate test2SqlSessionTemplate(
@Qualifier("otherSqlSessionFactory") SqlSessionFactory sessionFactory)
{
return new SqlSessionTemplate(sessionFactory);
}
}
项目结构预览
其他物料
多数据源配置效果测试
主要是测试一下多数据源有没有生效。可以基于不同数据源的数据差异进行分析。
提供思路参考(根据自己的想法来就行)
案例SQL脚本
CREATE TABLE `t_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
Demo 地址
提供个人demo环境:假如自己配置遇到阻塞,可以当作参考,减少时间消耗。