Maven依赖:
POM配置 Maven
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.16</version>
</dependency>
<!-- 多数据源 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.1.0</version>
</dependency>
yml 数据源配置:
本项目 使用newclass数据库,youxuewang数据库, newclass为主数据库.
yml配置
spring:
datasource:
dynamic:
primary: newclass #设置默认的数据源或者数据源组,默认值即为newclass
strict: false #设置严格模式,默认false不启动. 启动后在未匹配到指定数据源时候会抛出异常,不启动则使用默认数据源.
datasource:
newclass:
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
url: jdbc:xxxxxxxxxxxxxxxxxxx
username: xxxxxxxxxxx
password: xxxxxxxxxxxx
youxuewang:
name: sqlserver_youxuewang
#type: com.alibaba.druid.DruidDataSource #druid相关配置
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
url: jdbc:xxxxxxxxxxxxxxxxxxx
username: xxxxxxxxxxxxxxxxxxx
password: xxxxxxxxxxxxxxxxxxx
druid:
filters: stat
initial-size: 1 #配置初始化大小/最大/最小
min-idle: 1
max-active: 20 #获取连接等待超时时间
max-wait: 60000
time-between-eviction-runs-millis: 60000 #间隔多久进行一侧检测,检测需要的空闲连接
min-evictable-idle-time-millis: 30000 #一个连接在池中最小生存时间
validation-query: SELECT 'x' #检查数据库是否断开需要发送sql语句
test-while-idle: true #空闲的时候进行检测
test-on-borrow: false #是否检测池里连接的可用性,做了这个配置会降低性能
test-on-return: false #归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能
max-pool-prepared-statement-per-connection-size: 20
配置文件MybatisPlusConfig:
MybatisPlusConfig 展开原码
剩余是调用时,如果是非默认数据库 ,需要在类前注解 @DS(“数据库”)
展开原码
启动类需要填加注解:
@SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)