SpringBoot 配置 MyBatis 多数据源

项目里经常会有多数据源的场景,之前没来得及了解配置细节。
趁机补一补。

再此记录一次 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环境:假如自己配置遇到阻塞,可以当作参考,减少时间消耗。

jump to github-demo

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值