SpringBoot 中实现 MyBatis 多数据源配置(主从分离)

       在现代应用程序中,数据库会根据业务用途进行分库,因此后端程序需要连接多个数据库,以满足不同的业务需求。Spring Boot 提供了便捷的方式来配置 MyBatis 的多数据源。本文将为读者介绍如何在 Spring Boot 中实现 MyBatis 的多数据源配置。

1、项目依赖

<dependencies>
    <!-- Spring Boot Starter -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>

    <!-- MyBatis Starter -->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.0.6</version>
    </dependency>

    <!-- 数据库驱动 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.22</version>
    </dependency>
</dependencies>

2、配置文件

spring:
  datasource:
    db1:
      url: jdbc:mysql://localhost:3306/db1
      username: root1
      password: password1
      driver-class-name: com.mysql.cj.jdbc.Driver
    db2:
      url: jdbc:mysql://localhost:3306/db2
      username: root2
      password: password2
      driver-class-name: com.mysql.cj.jdbc.Driver
      

# 配置mybatis的实体包路径,以及xml存放路径
mybatis:
  type-aliases-package: com.xxx.pojo
  mapper-locations: classpath:mapper/db1/*.xml,classpath:mapper/db2/*.xm

3、配置数据源

@Configuration
public class DataSourceConfig {
    @Bean(name = "master")
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.db1")
    public DataSource primaryDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "slave")
    @ConfigurationProperties(prefix = "spring.datasource.db2")
    public DataSource secondDataSource() {
        return DataSourceBuilder.create().build();
    }
}

@Primary 注解指定db1为默认数据源。

配置指定mapper接口路径和xml文件路径。

MyBatisConfigPrimary.java


@Configuration
@MapperScan(basePackages = "cn.xxx.xxx.mapper.db1", sqlSessionFactoryRef = "primarySqlSessionFactory")
public class MyBatisConfigPrimary {
    @Bean(name = "primarySqlSessionFactory")
    @Primary
    public SqlSessionFactory primarySqlSessionFactory(@Qualifier("master") DataSource datasource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(datasource);
        bean.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/db1/*.xml"));
        return bean.getObject();// 设置mybatis的xml所在位置
    }
}

MyBatisConfigSecondary.java

@Configuration
@MapperScan(basePackages = "cn.xxx.xxx.mapper.db2", sqlSessionFactoryRef = "secondarySqlSessionFactory")
public class MyBatisConfigSecondary {
    @Bean(name = "secondarySqlSessionFactory")
    public SqlSessionFactory secondarySqlSessionFactory(@Qualifier("slave") DataSource datasource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(datasource);
        bean.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/db2/*.xml"));
        return bean.getObject();
    }
}

4、创建Mapper接口和XML文件

   根据数据源的配置,我们在 src/main/resources/mapper/db1 和 src/main/resources/mapper/db2 下分别创建 XML 文件,在cn.xxx.xxx.mapper.db1 和 cn.xxx.xxx.mapper.db2 定义相应的 Mapper 接口。

5、使用

@Service
public class MyService {

    @Autowired
    private Db1Mapper db1Mapper;

    @Autowired
    private Db2Mapper db2Mapper;

    public void execute() {
        List<Table> dataFromDb1 = db1Mapper.queryAll();

    }
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

晴天飛 雪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值