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

相关推荐
float_六七1 小时前
IntelliJ IDEA双击Ctrl的妙用
java·ide·intellij-idea
能摆一天是一天3 小时前
JAVA stream().flatMap()
java·windows
颜如玉3 小时前
🤲🏻🤲🏻🤲🏻临时重定向一定要能重定向🤲🏻🤲🏻🤲🏻
java·http·源码
程序员爱钓鱼4 小时前
Go语言实战案例 — 工具开发篇:实现一个图片批量压缩工具
后端·google·go
程序员的世界你不懂5 小时前
【Flask】测试平台开发,新增说明书编写和展示功能 第二十三篇
java·前端·数据库
星空寻流年5 小时前
设计模式第一章(建造者模式)
java·设计模式·建造者模式
gb42152876 小时前
java中将租户ID包装为JSQLParser的StringValue表达式对象,JSQLParser指的是?
java·开发语言·python
曾经的三心草6 小时前
Python2-工具安装使用-anaconda-jupyter-PyCharm-Matplotlib
android·java·服务器
Metaphor6926 小时前
Java 高效处理 Word 文档:查找并替换文本的全面指南
java·经验分享·word
ChinaRainbowSea6 小时前
7. LangChain4j + 记忆缓存详细说明
java·数据库·redis·后端·缓存·langchain·ai编程