引入依赖:
- <dependency>
- <groupId>com.baomidougroupId>
- <artifactId>dynamic-datasource-spring-boot-starterartifactId>
- <version>3.5.0version>
- dependency>
整体pom文件:
- "1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0modelVersion>
-
- <parent>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-parentartifactId>
- <version>2.7.2version>
- <relativePath/>
- parent>
-
- <groupId>com.examplegroupId>
- <artifactId>demoartifactId>
- <version>0.0.1-SNAPSHOTversion>
- <name>2023_demoname>
- <description>2023_demodescription>
-
- <properties>
- <java.version>11java.version>
- properties>
-
- <dependencies>
-
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starterartifactId>
- dependency>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-testartifactId>
- <scope>testscope>
- dependency>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- dependency>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-freemarkerartifactId>
- dependency>
-
-
-
- <dependency>
- <groupId>com.baomidougroupId>
- <artifactId>mybatis-plus-boot-starterartifactId>
- <version>3.4.1version>
- dependency>
-
- <dependency>
- <groupId>com.baomidougroupId>
- <artifactId>mybatis-plus-generatorartifactId>
- <version>3.4.1version>
- dependency>
-
- <dependency>
- <groupId>org.apache.velocitygroupId>
- <artifactId>velocity-engine-coreartifactId>
- <version>2.3version>
- dependency>
-
-
-
- <dependency>
- <groupId>com.baomidougroupId>
- <artifactId>dynamic-datasource-spring-boot-starterartifactId>
- <version>3.5.0version>
- dependency>
-
-
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-aopartifactId>
- dependency>
-
-
-
- <dependency>
- <groupId>com.alibabagroupId>
- <artifactId>druid-spring-boot-starterartifactId>
- <version>1.1.9version>
- dependency>
-
-
-
- <dependency>
- <groupId>mysqlgroupId>
- <artifactId>mysql-connector-javaartifactId>
- <scope>runtimescope>
- dependency>
-
-
-
- <dependency>
- <groupId>org.springdocgroupId>
- <artifactId>springdoc-openapi-uiartifactId>
- <version>1.6.11version>
- dependency>
-
- <dependency>
- <groupId>com.github.xiaoymingroupId>
- <artifactId>knife4j-springdoc-uiartifactId>
- <version>3.0.3version>
- dependency>
-
-
-
- <dependency>
- <groupId>org.projectlombokgroupId>
- <artifactId>lombokartifactId>
- <optional>trueoptional>
- dependency>
-
- <dependency>
- <groupId>cn.hutoolgroupId>
- <artifactId>hutool-allartifactId>
- <version>5.5.1version>
- dependency>
-
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <version>4.12version>
- <scope>testscope>
- dependency>
-
-
- <dependency>
- <groupId>com.alibabagroupId>
- <artifactId>easyexcelartifactId>
- <version>2.2.6version>
- dependency>
-
- <dependency>
- <groupId>com.google.guavagroupId>
- <artifactId>guavaartifactId>
- <version>17.0version>
- dependency>
-
- <dependency>
- <groupId>com.alibabagroupId>
- <artifactId>fastjsonartifactId>
- <version>1.2.68version>
- dependency>
-
-
-
- <dependency>
- <groupId>e-icebluegroupId>
- <artifactId>spire.pdf.freeartifactId>
- <version>5.1.0version>
- dependency>
-
- <dependency>
- <groupId>e-icebluegroupId>
- <artifactId>spire.docartifactId>
- <version>11.8.1version>
- dependency>
-
- dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-maven-pluginartifactId>
- <configuration>
- <excludes>
- <exclude>
- <groupId>org.projectlombokgroupId>
- <artifactId>lombokartifactId>
- exclude>
- excludes>
- configuration>
- plugin>
- plugins>
- build>
-
- <repositories>
- <repository>
- <id>aliyun-repoid>
- <name>aliyunname>
- <url>http://maven.aliyun.com/nexus/content/groups/public/url>
- repository>
-
- <repository>
- <id>com.e-iceblueid>
- <url>https://repo.e-iceblue.cn/repository/maven-public/url>
- repository>
- repositories>
-
-
- project>
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
启动类上添加:
@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);
- }
- }