SpringBoot结合Mybatis实现动态数据源切换

前言

在现代的分布式系统中,我们经常需要处理大量的数据和请求。为了提高系统的可扩展性和性能,我们需要使用动态数据源来管理不同的数据库连接。本文将介绍如何在SpringBoot项目中结合Mybatis实现动态数据源切换。

引入依赖

首先,我们需要在项目的pom.xml文件中添加SpringBoot、Mybatis和数据库驱动的依赖。以MySQL为例:

xml 复制代码
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.1.4</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
</dependencies>

配置数据源

在application.yml或application.properties文件中配置多个数据源,例如:

yaml 复制代码
spring:
  datasource:
    dynamic:
      primary: db1 # 设置默认数据源为db1
      datasource:
        db1:
          url: jdbc:mysql://localhost:3306/db1?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
          username: root
          password: 123456
          driver-class-name: com.mysql.cj.jdbc.Driver
        db2:
          url: jdbc:mysql://localhost:3306/db2?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
          username: root
          password: 123456
          driver-class-name: com.mysql.cj.jdbc.Driver

创建动态数据源类

创建一个动态数据源类,继承AbstractRoutingDataSource,重写determineCurrentLookupKey方法,根据请求参数或其他条件判断使用哪个数据源。例如:

java 复制代码
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
import java.util.*;

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

    @Bean(name = "primaryDataSource")
    @Primary // 设置为主数据源,当没有指定数据源时使用该数据源
    public DataSource primaryDataSource() {
        return DataSourceBuilder.create().build();
    }
}

创建事务管理器和SqlSessionFactoryBean

创建一个事务管理器,使用动态数据源作为事务管理器的数据源。同时,创建一个SqlSessionFactoryBean,设置数据源和映射文件的位置。例如:

java 复制代码
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.aop.framework.autoproxy.EnableAspectJAutoProxy;
import org.springframework.context.annotation.*;
import org.springframework.jdbc.datasource.*;
import javax.sql.DataSource;
import java.util.*;

@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true) // @EnableTransactionManagement注解会自动启用基于注解的事务管理功能,无需额外配置。同时,它会自动启用基于XML的事务管理功能,无需额外配置。因此,只需要在主配置类上添加@EnableTransactionManagement注解即可。如果需要禁用基于XML的事务管理功能,可以在@EnableTransactionManagement注解中添加mode属性并设置为AdviceMode.ASPECTJ,如下所示:@EnableTransactionManagement(mode = AdviceMode.ASPECTJ)。此外,如果需要禁用基于注解的事务管理功能,可以在@EnableTransactionManagement注解中添加proxyTargetClass属性并设置为true,如下所示:@EnableTransactionManagement(proxyTargetClass = true)。但是,这种方式会导致一些潜在的问题,因此不建议使用。

使用动态数据源

在需要进行数据源切换的地方,注入DynamicDataSource,然后调用其determineCurrentLookupKey方法获取当前数据源的名称,最后使用SqlSessionFactory获取对应的SqlSessionFactory实例。例如:

java 复制代码
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.sql.DataSource;
import java.util.*;

@Service
public class UserService {
    @Autowired
    private DataSource dynamicDataSource;

    public void getUserById(int id) {
        Map<Object, Object> context = new HashMap<>();
        context.put("id", id);
        String currentDataSource = dynamicDataSource.determineCurrentLookupKey();
        SqlSessionFactory sqlSessionFactory = (SqlSessionFactory) dynamicDataSource.getConnection(currentDataSource);
        // 使用sqlSessionFactory进行数据库操作...
    }
}

总结

我们可以在SpringBoot项目中实现动态数据源切换。在实际项目中,我们还可以根据业务需求对动态数据源进行更多的定制和优化。

相关推荐
智购科技无人售货机工厂店10 分钟前
自动售货机刷脸支付集成实战:活体检测与合规门槛~YH
java·服务器·前端·科技·零售
Nazarick1 小时前
力扣LeetCode热题75题(1)
java·算法·leetcode·力扣
黑色的白兔No11 小时前
若依平台cloud版本搭建
java·spring cloud·微服务·nacos·vue·若依
syf0611132 小时前
【Java】synchronized 关键字详解:从字节码到对象头与锁升级
java·开发语言
笑鸿的学习笔记3 小时前
C++笔记之信号量和引用计数的概念是否等价?
java·c++·笔记
IManiy3 小时前
AgentScope Java 2.0(四)上下文、状态与中间件
java
冰美式只要糖3 小时前
同一个 Seed,为什么插入另一条请求会改变采样结果?
后端
Xmi3 小时前
LLM 多步任务总是漏步骤?一次 Prompt Chaining 改造的完整记录
后端
sean_wang123 小时前
Rust 轻量级虚拟机 smolvm 实战:200ms 冷启动、OCI 镜像直跑、单文件打包
开发语言·后端·rust·大鲸华章
badhope4 小时前
3 个 Agent 并行发文章:我用 SQLite 任务表止住重复
后端·python·ai编程