【Dynamic-datasource】Springboot多数据源整合

引入依赖:


    com.baomidou
    dynamic-datasource-spring-boot-starter
    3.5.0

整体pom文件:



    4.0.0

    
        org.springframework.boot
        spring-boot-starter-parent
        2.7.2
        
    

    com.example
    demo
    0.0.1-SNAPSHOT
    2023_demo
    2023_demo

    
        11
    

    

        
        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-freemarker
        
        

        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.4.1
        

        
            com.baomidou
            mybatis-plus-generator
            3.4.1
        

        
            org.apache.velocity
            velocity-engine-core
            2.3
        
        

        
        
            com.baomidou
            dynamic-datasource-spring-boot-starter
            3.5.0
        
        

        
        
            org.springframework.boot
            spring-boot-starter-aop
        
        

        
        
            com.alibaba
            druid-spring-boot-starter
            1.1.9
        
        

        
        
            mysql
            mysql-connector-java
            runtime
        
        

        
        
            org.springdoc
            springdoc-openapi-ui
            1.6.11
        

        
            com.github.xiaoymin
            knife4j-springdoc-ui
            3.0.3
        
        

        
        
            org.projectlombok
            lombok
            true
        

        
            cn.hutool
            hutool-all
            5.5.1
        

        
            junit
            junit
            4.12
            test
        

        
        
            com.alibaba
            easyexcel
            2.2.6
        

        
            com.google.guava
            guava
            17.0
        

        
            com.alibaba
            fastjson
            1.2.68
        
        

        
        
            e-iceblue
            spire.pdf.free
            5.1.0
        

        
            e-iceblue
            spire.doc
            11.8.1
        
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            
        
    

    
        
            aliyun-repo
            aliyun
            http://maven.aliyun.com/nexus/content/groups/public/
        

        
            com.e-iceblue
            https://repo.e-iceblue.cn/repository/maven-public/
        
    



yml文件:

spring:
  profiles:
    active: dev
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    time-zone: GMT+8

logging:
  level:
    com.example.demo.system.mapper: DEBUG
    com.example.demo.bi.mapper: DEBUG

mybatis-plus:
  mapper-locations: classpath*:mapper/**/*.xml  #指定MyBatis映射的SQL文件目录
  global-config:
    db-config:
      #主键类型  AUTO:"数据库ID自增", INPUT:"用户输入ID",ID_WORKER:"全局唯一ID (数字类型唯一ID)", UUID:"全局唯一ID UUID";
      id-type: auto
      #驼峰下划线转换
      table-underline: true
  configuration:
    map-underscore-to-camel-case: true
    cache-enabled: false

-dev.yml如下:

server:
  port: 18080

spring:
  datasource:
      dynamic:
        primary: master  #设置默认的数据源或者数据源组,默认值即为master
        strict: false    #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源
        druid:
          filters: stat,slf4j
          #配置初始化大小、最小、最大线程数
          initial-size: 10
          min-idle: 10
          #CPU核数+1,也可以大些但不要超过20,数据库加锁时连接过多性能下降
          max-active: 20
          #最大等待时间
          max-wait: 60000
          #两次尝试间隔时间
          #    timeBetweenEvictionRunsMillis: 600000
          #保持长连接
          keep-alive: true
          #配置一个连接在池中最大空间时间,单位是毫秒
          min-evictable-idle-time-millis: 600000
          #空闲时测试连接
          testWhileIdle: true
          #测试用SQL
          validation-query: select 1 from dual
        datasource:
          master:
            type: com.alibaba.druid.pool.DruidDataSource
            driver-class-name: com.mysql.cj.jdbc.Driver
            url: jdbc:mysql://localhost:3306/local?serverTimezone=GMT%2B8&characterEncoding=utf-8
            username: root
            password: 123456
          angel:
            type: com.alibaba.druid.pool.DruidDataSource
            driver-class-name: com.mysql.cj.jdbc.Driver
            url: jdbc:mysql://localhost:3306/angel?serverTimezone=GMT%2B8&characterEncoding=utf-8
            username: root
            password: 123456

springdoc:
  swagger-ui:
    enabled: true
    doc-expansion: none
  api-docs:
    enabled: true

在serivceImpl实现类上添加注解:@DS

【Dynamic-datasource】Springboot多数据源整合_第1张图片

启动类上添加:

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})

启动类如下:

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class},
        scanBasePackages={"com.example.demo.*"})
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

你可能感兴趣的:(spring,boot,java,spring)