Spring Boot 集成 MySQL 和使用 MyBatis-Plus 实现多数据源配置的过程涉及几个关键步骤,可以分为配置多数据源、整合 MyBatis-Plus 以及如何在业务中切换数据源等部分。

前置工作

  1. 依赖配置:首先确保你的 pom.xml 或者 build.gradle 中已经包含了Spring Boot,MyBatis-Plus,以及MySQL驱动相关的依赖。
  2. application.yml:在application.yml文件中配置两个或更多的数据源属性。

配置多数据源

在 Spring Boot 项目中配置多数据源,通常涉及以下三个步骤:

  1. 定义数据源配置:你需要定义每个数据源的配置类,声明为 @Configuration 并标注 @MapperScan 以扫描不同的 Mapper 接口。
@Configuration
@MapperScan(basePackages = "com.example.module1.mapper", sqlSessionTemplateRef  = "sqlSessionTemplate1")
public class DataSource1Config {

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

    // 省略 SqlSessionFactory 和 SqlSessionTemplate 的配置
}

@Configuration
@MapperScan(basePackages = "com.example.module2.mapper", sqlSessionTemplateRef  = "sqlSessionTemplate2")
public class DataSource2Config {

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

    // 省略 SqlSessionFactory 和 SqlSessionTemplate 的配置
}
  1. 配置 SqlSessionFactory:为每个数据源配置 SqlSessionFactory,区分不同的数据源。
@Bean(name = "sqlSessionFactory1")
public SqlSessionFactory sqlSessionFactory1(@Qualifier("dataSource1") DataSource dataSource)
        throws Exception {
    SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    bean.setDataSource(dataSource);
    // 省略其他配置项
    return bean.getObject();
}

@Bean(name = "sqlSessionFactory2")
public SqlSessionFactory sqlSessionFactory2(@Qualifier("dataSource2") DataSource dataSource)
        throws Exception {
    SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    bean.setDataSource(dataSource);
    // 省略其他配置项
    return bean.getObject();
}
  1. 配置 SqlSessionTemplate:SqlSessionTemplate 是 Mybatis 提供的一个执行 SQL 的模板,它包装了 SqlSession 的操作。
@Bean(name = "sqlSessionTemplate1")
public SqlSessionTemplate sqlSessionTemplate1(@Qualifier("sqlSessionFactory1") SqlSessionFactory sqlSessionFactory) throws Exception {
    return new SqlSessionTemplate(sqlSessionFactory);
}

@Bean(name = "sqlSessionTemplate2")
public SqlSessionTemplate sqlSessionTemplate2(@Qualifier("sqlSessionFactory2") SqlSessionFactory sqlSessionFactory) throws Exception {
    return new SqlSessionTemplate(sqlSessionFactory);
}

整合 MyBatis-Plus

MyBatis-Plus 的整合可以通过引入它的启动器依赖来实现,它将自动配合 Spring Boot 的自动配置。确保在你的 mappers 接口上使用 @Mapper 或者在配置类上使用 @MapperScan 注解。

业务中切换数据源

在实际的业务代码中,你可能需要动态地切换不同的数据源。可以使用 AbstractRoutingDataSource实现动态数据源的路由。

public class DynamicDataSource extends AbstractRoutingDataSource {
    @Override
    protected Object determineCurrentLookupKey() {
        return DataSourceContextHolder.getDB();
    }
}

你可以通过 AOP 切面、注解或请求拦截等方式,在每次数据库操作前设置当前线程的数据源标识,这样 DynamicDataSource 便可按这个标识来选择实际的数据源。

配置完成后,在你的业务逻辑中,可以按需切换数据源,MyBatis-Plus 会按照配置执行相应的数据源操作。

结语

以上步骤涵盖了如何在 Spring Boot 应用中集成 MySQL 以及配置 MyBatis-Plus 来支持多个数据源。整合后,可以高效地处理多个数据库的并存情况,有效提升应用的灵活性和可维护性。在实施过程中,确保每个配置部分正确无误,避免数据源搭配错误,这可能导致数据操作的失败或者数据安全问题。

云服务器/高防CDN推荐

蓝易云国内/海外高防云服务器推荐


海外免备案云服务器链接:www.tsyvps.com

蓝易云安全企业级高防CDN:www.tsycdn.com

持有增值电信营业许可证:B1-20222080【资质齐全】

蓝易云香港五网CN2 GIA/GT精品网络服务器。拒绝绕路,拒绝不稳定。

最后修改:2024 年 03 月 13 日
如果觉得我的文章对你有用,请随意赞赏