参考:
mybatis-plus官网: https://baomidou.com/
项目地址: https://github.com/baomidou/dynamic-datasource-spring-boot-starter
1、application.yml配置
server:
port: 8083
servlet:
context-path: /
tomcat:
max-threads: 500 #最大线程数
max-connections: 10000 #最大连接数
jetty:
max-http-post-size:
spring:
application:
name: dynamicds
servlet:
multipart:
max-file-size: 20MB
max-request-size: 20MB
#devtools配置
devtools:
restart:
exclude: static/**,resouces/**
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
datasource:
#username: root
#password: 123456
#url: jdbc:mysql://localhost:3306/rocketdemo?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8
#配置hikari连接池
hikari:
minimum-idle: 5 #最小连接数
maximum-pool-size: 10 #池中最大连接数
connection-timeout: 20000 #连接超时时间
idle-timeout: 30000 # 空闲等待时间 ms
max-lifetime: 1800000 #30分钟
#动态数据源配置
dynamic:
#主数据源
primary: business
datasource:
#数据源1
business:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3336/cbcloud_business?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8
username: root
password: 123456
#数据源2
clinic:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3336/cb_clinic?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8
username: root
password: 123456
#mybatsplus 配置
mybatis-plus:
config-location: classpath:SqlMapConfig.xml
2、pom.xml配置
<!--引入mybatis-plus starter依赖 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.6</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>2.5.4</version>
</dependency>
3、配置DBConstans常量类
public class DBConstants {
public static final String DS_BUSINESS="business";
public static final String DS_CLINIC="clinic";
}
4、service、mapper层,标记@DS注解使用指定的数据源(不指定则使用默认的数据源)
@Service
@DS(DBConstants.DS_CLINIC)
public class ClinicService extends ServiceImpl<ClinicMapper,Clinic> {
}
@DS(DBConstants.DS_CLINIC)
public interface ClinicMapper extends BaseMapper<Clinic> {
}


阅读排行


Copyright © 叮叮声的奶酪 版权所有
备案号:鄂ICP备17018671号-1