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


    引入依赖:

    1. <dependency>
    2. <groupId>com.baomidougroupId>
    3. <artifactId>dynamic-datasource-spring-boot-starterartifactId>
    4. <version>3.5.0version>
    5. dependency>

    整体pom文件:

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <modelVersion>4.0.0modelVersion>
    5. <parent>
    6. <groupId>org.springframework.bootgroupId>
    7. <artifactId>spring-boot-starter-parentartifactId>
    8. <version>2.7.2version>
    9. <relativePath/>
    10. parent>
    11. <groupId>com.examplegroupId>
    12. <artifactId>demoartifactId>
    13. <version>0.0.1-SNAPSHOTversion>
    14. <name>2023_demoname>
    15. <description>2023_demodescription>
    16. <properties>
    17. <java.version>11java.version>
    18. properties>
    19. <dependencies>
    20. <dependency>
    21. <groupId>org.springframework.bootgroupId>
    22. <artifactId>spring-boot-starterartifactId>
    23. dependency>
    24. <dependency>
    25. <groupId>org.springframework.bootgroupId>
    26. <artifactId>spring-boot-starter-testartifactId>
    27. <scope>testscope>
    28. dependency>
    29. <dependency>
    30. <groupId>org.springframework.bootgroupId>
    31. <artifactId>spring-boot-starter-webartifactId>
    32. dependency>
    33. <dependency>
    34. <groupId>org.springframework.bootgroupId>
    35. <artifactId>spring-boot-starter-freemarkerartifactId>
    36. dependency>
    37. <dependency>
    38. <groupId>com.baomidougroupId>
    39. <artifactId>mybatis-plus-boot-starterartifactId>
    40. <version>3.4.1version>
    41. dependency>
    42. <dependency>
    43. <groupId>com.baomidougroupId>
    44. <artifactId>mybatis-plus-generatorartifactId>
    45. <version>3.4.1version>
    46. dependency>
    47. <dependency>
    48. <groupId>org.apache.velocitygroupId>
    49. <artifactId>velocity-engine-coreartifactId>
    50. <version>2.3version>
    51. dependency>
    52. <dependency>
    53. <groupId>com.baomidougroupId>
    54. <artifactId>dynamic-datasource-spring-boot-starterartifactId>
    55. <version>3.5.0version>
    56. dependency>
    57. <dependency>
    58. <groupId>org.springframework.bootgroupId>
    59. <artifactId>spring-boot-starter-aopartifactId>
    60. dependency>
    61. <dependency>
    62. <groupId>com.alibabagroupId>
    63. <artifactId>druid-spring-boot-starterartifactId>
    64. <version>1.1.9version>
    65. dependency>
    66. <dependency>
    67. <groupId>mysqlgroupId>
    68. <artifactId>mysql-connector-javaartifactId>
    69. <scope>runtimescope>
    70. dependency>
    71. <dependency>
    72. <groupId>org.springdocgroupId>
    73. <artifactId>springdoc-openapi-uiartifactId>
    74. <version>1.6.11version>
    75. dependency>
    76. <dependency>
    77. <groupId>com.github.xiaoymingroupId>
    78. <artifactId>knife4j-springdoc-uiartifactId>
    79. <version>3.0.3version>
    80. dependency>
    81. <dependency>
    82. <groupId>org.projectlombokgroupId>
    83. <artifactId>lombokartifactId>
    84. <optional>trueoptional>
    85. dependency>
    86. <dependency>
    87. <groupId>cn.hutoolgroupId>
    88. <artifactId>hutool-allartifactId>
    89. <version>5.5.1version>
    90. dependency>
    91. <dependency>
    92. <groupId>junitgroupId>
    93. <artifactId>junitartifactId>
    94. <version>4.12version>
    95. <scope>testscope>
    96. dependency>
    97. <dependency>
    98. <groupId>com.alibabagroupId>
    99. <artifactId>easyexcelartifactId>
    100. <version>2.2.6version>
    101. dependency>
    102. <dependency>
    103. <groupId>com.google.guavagroupId>
    104. <artifactId>guavaartifactId>
    105. <version>17.0version>
    106. dependency>
    107. <dependency>
    108. <groupId>com.alibabagroupId>
    109. <artifactId>fastjsonartifactId>
    110. <version>1.2.68version>
    111. dependency>
    112. <dependency>
    113. <groupId>e-icebluegroupId>
    114. <artifactId>spire.pdf.freeartifactId>
    115. <version>5.1.0version>
    116. dependency>
    117. <dependency>
    118. <groupId>e-icebluegroupId>
    119. <artifactId>spire.docartifactId>
    120. <version>11.8.1version>
    121. dependency>
    122. dependencies>
    123. <build>
    124. <plugins>
    125. <plugin>
    126. <groupId>org.springframework.bootgroupId>
    127. <artifactId>spring-boot-maven-pluginartifactId>
    128. <configuration>
    129. <excludes>
    130. <exclude>
    131. <groupId>org.projectlombokgroupId>
    132. <artifactId>lombokartifactId>
    133. exclude>
    134. excludes>
    135. configuration>
    136. plugin>
    137. plugins>
    138. build>
    139. <repositories>
    140. <repository>
    141. <id>aliyun-repoid>
    142. <name>aliyunname>
    143. <url>http://maven.aliyun.com/nexus/content/groups/public/url>
    144. repository>
    145. <repository>
    146. <id>com.e-iceblueid>
    147. <url>https://repo.e-iceblue.cn/repository/maven-public/url>
    148. repository>
    149. repositories>
    150. project>

    yml文件:

    1. spring:
    2. profiles:
    3. active: dev
    4. jackson:
    5. date-format: yyyy-MM-dd HH:mm:ss
    6. time-zone: GMT+8
    7. logging:
    8. level:
    9. com.example.demo.system.mapper: DEBUG
    10. com.example.demo.bi.mapper: DEBUG
    11. mybatis-plus:
    12. mapper-locations: classpath*:mapper/**/*.xml #指定MyBatis映射的SQL文件目录
    13. global-config:
    14. db-config:
    15. #主键类型 AUTO:"数据库ID自增", INPUT:"用户输入ID",ID_WORKER:"全局唯一ID (数字类型唯一ID)", UUID:"全局唯一ID UUID";
    16. id-type: auto
    17. #驼峰下划线转换
    18. table-underline: true
    19. configuration:
    20. map-underscore-to-camel-case: true
    21. cache-enabled: false

    -dev.yml如下:

    1. server:
    2. port: 18080
    3. spring:
    4. datasource:
    5. dynamic:
    6. primary: master #设置默认的数据源或者数据源组,默认值即为master
    7. strict: false #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源
    8. druid:
    9. filters: stat,slf4j
    10. #配置初始化大小、最小、最大线程数
    11. initial-size: 10
    12. min-idle: 10
    13. #CPU核数+1,也可以大些但不要超过20,数据库加锁时连接过多性能下降
    14. max-active: 20
    15. #最大等待时间
    16. max-wait: 60000
    17. #两次尝试间隔时间
    18. # timeBetweenEvictionRunsMillis: 600000
    19. #保持长连接
    20. keep-alive: true
    21. #配置一个连接在池中最大空间时间,单位是毫秒
    22. min-evictable-idle-time-millis: 600000
    23. #空闲时测试连接
    24. testWhileIdle: true
    25. #测试用SQL
    26. validation-query: select 1 from dual
    27. datasource:
    28. master:
    29. type: com.alibaba.druid.pool.DruidDataSource
    30. driver-class-name: com.mysql.cj.jdbc.Driver
    31. url: jdbc:mysql://localhost:3306/local?serverTimezone=GMT%2B8&characterEncoding=utf-8
    32. username: root
    33. password: 123456
    34. angel:
    35. type: com.alibaba.druid.pool.DruidDataSource
    36. driver-class-name: com.mysql.cj.jdbc.Driver
    37. url: jdbc:mysql://localhost:3306/angel?serverTimezone=GMT%2B8&characterEncoding=utf-8
    38. username: root
    39. password: 123456
    40. springdoc:
    41. swagger-ui:
    42. enabled: true
    43. doc-expansion: none
    44. api-docs:
    45. enabled: true

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

    启动类上添加:

    @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})

    启动类如下:

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

  • 相关阅读:
    Flowable多实例加签减签功能正式上线
    【小程序】的架构和配置文件
    SpringBoot整合dubbo(二)
    c++多态的使用
    Redis缓存详解(黑马-未完结)
    最短路问题之Dijkstra算法 洛谷 单源最短路径
    Explain信息中Extra字段解释
    解析赋值的用途
    【LeetCode: 207.课程表:拓扑排序+图】
    上周热点回顾(8.8-8.14)
  • 原文地址:https://blog.csdn.net/niuchenliang524/article/details/134457501