Spring Boot 2.x + Mybatis Plus 多数据源配置

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
@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();
    }
}

参考链接

Spring Boot 2.7.5 HikariCP 连接池多数据源配置_hikari多数据源配置-CSDN博客

Spring Boot Reference Documentation

  • 9
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值