MP官方文档提供有多数据源配置方案,这里没有使用其方案,原因是项目不想引入过多依赖
提供一下官网的文档供各位选择多数据源支持 | MyBatis-Plus (baomidou.com)
配置文件
application.properties文件配置两个数据源信息,yaml文件自行修改格式
# db1 database
spring.datasource.db1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.db1.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.db1.url=jdbc:mysql://127.0.0.1:3306/db1?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false&rewriteBatchedStatements=true
spring.datasource.db1.username=root
spring.datasource.db1.password=123456
spring.datasource.db1.hikari.pool-name=db1
spring.datasource.db1.hikari.auto-commit=true
spring.datasource.db1.hikari.connection-timeout=30000
spring.datasource.db1.hikari.idle-timeout=600000
spring.datasource.db1.hikari.max-lifetime=1800000
spring.datasource.db1.hikari.maximum-pool-size=10
spring.datasource.db1.hikari.leak-detection-threshold=30000
# db2 database
spring.datasource.db2.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.db2.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.db2.url=jdbc:mysql://127.0.0.1:3306/db2?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false&rewriteBatchedStatements=true
spring.datasource.db2.username=root
spring.datasource.db2.password=123456
spring.datasource.db2.hikari.pool-name=db2
spring.datasource.db2.hikari.auto-commit=true
spring.datasource.db2.hikari.connection-timeout=30000
spring.datasource.db2.hikari.idle-timeout=600000
spring.datasource.db2.hikari.max-lifetime=1800000
spring.datasource.db2.hikari.maximum-pool-size=10
spring.datasource.db2.hikari.leak-detection-threshold=30000
创建DataSourceConfig类
我这里两个数据源写在一个文件里面,也可以拆分成两个。
注意mapper接口和xml的目录,不同的数据源在不同的包里面
因为整合的mybatis-plus,所以这里的SqlSessionFactory 为 MyBatis-Plus 的 SqlSessionFactory
com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean
java
@Configuration
@MapperScan(basePackages = {"com.xxx.mapper.db1"}, sqlSessionFactoryRef = "db1SqlSessionFactory")
@MapperScan(basePackages = {"com.xxx.mapper.db2"}, sqlSessionFactoryRef = "db2SqlSessionFactory")
public class DataSourceConfig {
/**
* db1数据源配置
* @return
*/
@Bean(name = "db1DataSourceProperties")
@Primary
@ConfigurationProperties("spring.datasource.db1")
public DataSourceProperties db1DataSourceProperties() {
return new DataSourceProperties();
}
@Primary
@Bean(name = "db1DataSource")
@Qualifier("db1DataSource")
@ConfigurationProperties(prefix = "spring.datasource.db1.hikari")
public HikariDataSource db1DataSource() {
return db1DataSourceProperties().initializeDataSourceBuilder().type(HikariDataSource.class).build();
}
@Primary
@Bean("db1DataSourceTransactionManager")
public DataSourceTransactionManager db1DataSourceTransactionManager(@Qualifier("db1DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Primary
@Bean("db1SqlSessionFactory")
public SqlSessionFactory db1SqlSessionFactory(@Qualifier("db1DataSource") DataSource dataSource) throws Exception {
MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
sqlSessionFactory.setDataSource(dataSource);
Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath:mapper/db1/*.xml");
sqlSessionFactory.setMapperLocations(resources);
// 插件对象
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
//分页插件
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
sqlSessionFactory.setPlugins(mybatisPlusInterceptor);
return sqlSessionFactory.getObject();
}
/**
* db2数据源配置
* @return
*/
@Bean(name = "db2DataSourceProperties")
@ConfigurationProperties("spring.datasource.db2")
public DataSourceProperties db2DataSourceProperties() {
return new DataSourceProperties();
}
@Bean(name = "db2DataSource")
@Qualifier("db2DataSource")
@ConfigurationProperties(prefix="spring.datasource.db2.hikari")
public DataSource db2DataSource() {
return db2DataSourceProperties().initializeDataSourceBuilder().type(HikariDataSource.class).build();
}
@Bean("db2DataSourceTransactionManager")
public DataSourceTransactionManager db2DataSourceTransactionManager(@Qualifier("db2DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean("db2SqlSessionFactory")
public SqlSessionFactory db2SqlSessionFactory(@Qualifier("db2DataSource") DataSource dataSource) throws Exception {
MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
sqlSessionFactory.setDataSource(dataSource);
Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath:mapper/db2/*.xml");
sqlSessionFactory.setMapperLocations(resources);
// 插件对象
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
//分页插件
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
sqlSessionFactory.setPlugins(mybatisPlusInterceptor);
return sqlSessionFactory.getObject();
}
}