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

参考链接

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

Spring Boot Reference Documentation

相关推荐
路在脚下@1 小时前
spring boot的配置文件属性注入到类的静态属性
java·spring boot·sql
啦啦右一1 小时前
Spring Boot | (一)Spring开发环境构建
spring boot·后端·spring
森屿Serien1 小时前
Spring Boot常用注解
java·spring boot·后端
苹果醋32 小时前
React源码02 - 基础知识 React API 一览
java·运维·spring boot·mysql·nginx
盛派网络小助手3 小时前
微信 SDK 更新 Sample,NCF 文档和模板更新,更多更新日志,欢迎解锁
开发语言·人工智能·后端·架构·c#
∝请叫*我简单先生3 小时前
java如何使用poi-tl在word模板里渲染多张图片
java·后端·poi-tl
荆州克莱4 小时前
mysql中局部变量_MySQL中变量的总结
spring boot·spring·spring cloud·css3·技术
zquwei4 小时前
SpringCloudGateway+Nacos注册与转发Netty+WebSocket
java·网络·分布式·后端·websocket·网络协议·spring
dessler4 小时前
Docker-run命令详细讲解
linux·运维·后端·docker