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.ymlapplication.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项目中实现动态数据源切换。在实际项目中,我们还可以根据业务需求对动态数据源进行更多的定制和优化。

相关推荐
一只叫煤球的猫6 小时前
写代码很6,面试秒变菜鸟?不卖课,面试官视角走心探讨
前端·后端·面试
bobz9657 小时前
tcp/ip 中的多路复用
后端
bobz9657 小时前
tls ingress 简单记录
后端
皮皮林5518 小时前
IDEA 源码阅读利器,你居然还不会?
java·intellij idea
你的人类朋友8 小时前
什么是OpenSSL
后端·安全·程序员
bobz9658 小时前
mcp 直接操作浏览器
后端
前端小张同学11 小时前
服务器部署 gitlab 占用空间太大怎么办,优化思路。
后端
databook11 小时前
Manim实现闪光轨迹特效
后端·python·动效
武子康12 小时前
大数据-98 Spark 从 DStream 到 Structured Streaming:Spark 实时计算的演进
大数据·后端·spark
该用户已不存在12 小时前
6个值得收藏的.NET ORM 框架
前端·后端·.net