Spring Boot MyBatis Plus Druid多数据源解决方案

分类:知识百科 日期: 点击:0

MyBatis Plus是一个基于MyBatis的轻量级框架,可以实现快速、简单的数据库访问。Druid是一个高效的数据库连接池,可以提供可靠的数据库性能。Spring Boot是一个快速、简单的开发框架,可以帮助开发者更快地完成项目开发。使用,可以实现快速、简单的多数据源访问。

使用步骤

  • 引入相关依赖
在项目中引入MyBatis Plus和Druid的相关依赖:

    org.mybatis.spring.boot
    mybatis-spring-boot-starter
    2.1.1



    com.alibaba
    druid-spring-boot-starter
    1.1.10

  • 在application.yml文件中配置数据源
在application.yml文件中配置数据源:
spring:
  datasource:
    druid:
      master:
        url: jdbc:mysql://localhost:3306/master
        username: root
        password: root
        driver-class-name: com.mysql.jdbc.Driver
        filters: stat
        max-active: 20
        initial-size: 1
        max-wait: 60000
        min-idle: 1
        time-between-eviction-runs-millis: 60000
        min-evictable-idle-time-millis: 300000
        validation-query: select 'x'
        test-while-idle: true
        test-on-borrow: false
        test-on-return: false
        pool-prepared-statements: true
        max-open-prepared-statements: 20
      slave:
        url: jdbc:mysql://localhost:3306/slave
        username: root
        password: root
        driver-class-name: com.mysql.jdbc.Driver
        filters: stat
        max-active: 20
        initial-size: 1
        max-wait: 60000
        min-idle: 1
        time-between-eviction-runs-millis: 60000
        min-evictable-idle-time-millis: 300000
        validation-query: select 'x'
        test-while-idle: true
        test-on-borrow: false
        test-on-return: false
        pool-prepared-statements: true
        max-open-prepared-statements: 20
  • 在配置类中配置多数据源
在配置类中配置多数据源:
@Configuration
public class DataSourceConfig {
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.druid.master")
    public DataSource masterDataSource() {
        return DruidDataSourceBuilder.create().build();
    }

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.druid.slave")
    public DataSource slaveDataSource() {
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(name = "dynamicDataSource")
    public DynamicDataSource dataSource(DataSource masterDataSource, DataSource slaveDataSource) {
        Map targetDataSources = new HashMap<>();
        targetDataSources.put(DBTypeEnum.MASTER, masterDataSource);
        targetDataSources.put(DBTypeEnum.SLAVE, slaveDataSource);

        DynamicDataSource dataSource = new DynamicDataSource();
        dataSource.setTargetDataSources(targetDataSources);
        dataSource.setDefaultTargetDataSource(masterDataSource);

        return dataSource;
    }
}
  • 在MyBatis Plus的配置类中配置多数据源
在MyBatis Plus的配置类中配置多数据源:
@Configuration
@MapperScan(basePackages = "com.example.mapper", sqlSessionTemplateRef = "dynamicSqlSessionTemplate")
public class MybatisPlusConfig {
    @Bean
    public SqlSessionFactory sqlSessionFactory(@Qualifier("dynamicDataSource") DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
        sqlSessionFactory.setDataSource(dataSource);
        return sqlSessionFactory.getObject();
    }

    @Bean
    public SqlSessionTemplate dynamicSqlSessionTemplate(@Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

可以帮助开发者实现快速、简单的多数据源访问,只需要按照上述步骤进行配置即可。

标签:

版权声明

1. 本站所有素材,仅限学习交流,仅展示部分内容,如需查看完整内容,请下载原文件。
2. 会员在本站下载的所有素材,只拥有使用权,著作权归原作者所有。
3. 所有素材,未经合法授权,请勿用于商业用途,会员不得以任何形式发布、传播、复制、转售该素材,否则一律封号处理。
4. 如果素材损害你的权益请联系客服QQ:77594475 处理。