【记录】SpringBoot + Mybatis Plus + Druid 配置多数据源

SpringBoot + Mybatis Plus + Druid 配置多数据源

环境说明

我这里使用mysql 和clickhouse两种数据库来举例

导包

<dependency>
            <groupId>com.baomidougroupId>
            <artifactId>mybatis-plus-boot-starterartifactId>
            <version>3.4.1version>
        dependency>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
        dependency>
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druid-spring-boot-starterartifactId>
            <version>1.2.4version>
        dependency>

        <dependency>
            <groupId>ru.yandex.clickhousegroupId>
            <artifactId>clickhouse-jdbcartifactId>
            <version>0.2.4version>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starterartifactId>
        dependency>

        <dependency>
            <groupId>com.alibaba.ottergroupId>
            <artifactId>canal.clientartifactId>
            <version>1.0.25version>
        dependency>

项目目录准备

【记录】SpringBoot + Mybatis Plus + Druid 配置多数据源_第1张图片

一、两种配置方法

  1. 手动配置法 (这种只适用于Mybatis,Mybatis Plus的JPA用不了)

    • 在主启动类上排除自动配置类

      @SpringBootApplication(exclude = {
              DataSourceAutoConfiguration.class
      })
      
    • 配置application.yml

      spring:
        datasource:
          mysql:
            driver-class-name: com.mysql.cj.jdbc.Driver
            url: jdbc:mysql://host:3306/database
            username: root
            password: 123456
          click:
            url: jdbc:clickhouse://localhost:8123/java_test
            driver-class-name: ru.yandex.clickhouse.ClickHouseDriver
      
    • 手动配置DataSource

      @Configuration
      @EnableConfigurationProperties(MyClickHouseProperties.class)
      public class DataSourceConfig {
      
          @Resource
          private MyClickHouseProperties myClickHouseProperties;
      
          @Bean(name = "mysql")
          @ConfigurationProperties(prefix = "spring.datasource.mysql")
          public DataSource mysqlDataSource() {
              return DataSourceBuilder.create().build();
          }
      
      
          @Bean(name = "clickhouse")
          @ConfigurationProperties(prefix = "spring.datasource.click")
          public DataSource clickHouseDataSource() {
              return DataSourceBuilder.create().build();
          }
      }
      
    • 配置SqlSessionFactory,并扫描对应的包

      @Configuration
      @MapperScan(basePackages = {"com.meb.canal.mapper.mysql"}, sqlSessionFactoryRef = "mysqlSqlSessionFactory")
      public class MysqlSqlSessionFactoryConfig {
      
          @Resource
          @Qualifier("mysql")
          private DataSource dataSource;
      
          @Bean
          public SqlSessionFactory mysqlSqlSessionFactory() throws Exception{
              SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
              factoryBean.setDataSource(dataSource);
              factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/mysql/*.xml"));
              return factoryBean.getObject();
          }
      
          @Bean
          public SqlSessionTemplate sqlSessionTemplateDb1() throws Exception {
              return new SqlSessionTemplate(mysqlSqlSessionFactory());
          }
      
      
      }
      
      
      
      @Configuration
      @MapperScan(basePackages = {"com.meb.canal.mapper.click"}, sqlSessionFactoryRef = "clickSqlSessionFactory")
      public class ClickHouseSqlSessionFactoryConfig {
      
          @Resource
          @Qualifier("clickhouse")
          private DataSource dataSource;
      
          @Bean("clickSqlSessionFactory")
          public SqlSessionFactory clickSqlSessionFactory() throws Exception{
              SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
              factoryBean.setDataSource(dataSource);
              factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/click/*.xml"));
              return factoryBean.getObject();
          }
      
          @Bean
          public SqlSessionTemplate sqlSessionTemplateDb1() throws Exception {
              return new SqlSessionTemplate(clickSqlSessionFactory());
          }
      
      
      }
      
  2. 使用Mybatis Plus官方的多数据源方案(https://baomidou.com/guide/dynamic-datasource.html)

导新包

<dependency>
  <groupId>com.baomidougroupId>
  <artifactId>dynamic-datasource-spring-boot-starterartifactId>
  <version>3.2.1version>
dependency>

配置application.yml

spring:
  datasource:
    dynamic:
      primary: mysql # 默认
      datasource:
        mysql:
          driver-class-name: com.mysql.cj.jdbc.Driver
          url: jdbc:mysql://host:3306/database
          username: root
          password: 123456
        click:
          url: jdbc:clickhouse://localhost:8123/java_test
          driver-class-name: ru.yandex.clickhouse.ClickHouseDriver
          

mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    map-underscore-to-camel-case: false
  mapper-locations: classpath:mapper/*.xml

使用 - 在具体的service上使用@DS注解来切换数据源

【记录】SpringBoot + Mybatis Plus + Druid 配置多数据源_第2张图片

注意

  • 错误信息: Failed to configure a DataSource: ‘url’ attribute is not specified and no embedded datasource could be configured.

【记录】SpringBoot + Mybatis Plus + Druid 配置多数据源_第3张图片

如果使用了Druid,或者导了Druid包的话,还需要在主启动类上排除它的自动配置类

@SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)
@EnableCanalClient
public class CanalApplication {

    public static void main(String[] args) {
        SpringApplication.run(CanalApplication.class, args);
    }

}

你可能感兴趣的:(笔记,mybatis,mysql,spring,boot)