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

相关推荐
凯基迪科技8 分钟前
exe软件壳的分类----加密保护壳
java
wuxuanok20 分钟前
Web后端开发-分层解耦
java·笔记·后端·学习
315356691337 分钟前
ClipReader:一个剪贴板英语单词阅读器
前端·后端
ladymorgana39 分钟前
【Spring Boot】HikariCP 连接池 YAML 配置详解
spring boot·后端·mysql·连接池·hikaricp
kyle~1 小时前
C/C++字面量
java·c语言·c++
neoooo1 小时前
别慌,Java只有值传递——一次搞懂“为啥我改了它还不变”!
java·后端·spring
秋难降1 小时前
Python 知识 “八股”:给有 C 和 Java 基础的你😁😁😁
java·python·c
wuxuanok1 小时前
Web后端开发-请求响应
java·开发语言·笔记·学习
用户7785371836961 小时前
一力破万法:从0实现一个http代理池
后端·爬虫
livemetee1 小时前
spring-ai 1.0.0 (3)交互增强:Advisor 顾问模块
java