1、引入依赖
<!-- https://mvnrepository.com/artifact/com.baomidou/dynamic-datasource-spring-boot-starter -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>2.4.2</version>
</dependency>
2、配置application.properties
#server.port = 8091
#mysql
#删除多表
#allowMultiQueries=true
#换成 druid
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#设置默认的数据源或者数据源组,默认值即为master
spring.datasource.dynamic.primary=master
spring.datasource.dynamic.datasource.master.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.dynamic.datasource.master.url=jdbc:mysql://127.0.0.1:3306/wangmx_test1?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=CTT&allowMultiQueries=true
spring.datasource.dynamic.datasource.master.username=hopson
spring.datasource.dynamic.datasource.master.password=hopson
# slave_1
spring.datasource.dynamic.datasource.slave_1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.dynamic.datasource.slave_1.url=jdbc:mysql://127.0.0.1:3306/wangmx_test2?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=CTT&allowMultiQueries=true
spring.datasource.dynamic.datasource.slave_1.username=hopson
spring.datasource.dynamic.datasource.slave_1.password=hopson
# druid 下面为连接池的补充设置,应用到上面所有数据源中
# druid 初始化连接数大小,最小,最大
spring.datasource.dynamic.druid.initial-size=8
spring.datasource.dynamic.druid.min-idle=8
spring.datasource.dynamic.druid.max-active=30
# druid 配置获取连接等待超时的时间
spring.datasource.dynamic.druid.max-wait=30000
# druid 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
spring.datasource.dynamic.druid.time-between-eviction-runs-millis=60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
spring.datasource.dynamic.druid.min-evictable-idle-time-millis=300000
# druid 验证数据库连接的有效性
spring.datasource.dynamic.druid.validation-query=select 1
spring.datasource.dynamic.druid.test-while-idle=true
spring.datasource.dynamic.druid.test-on-borrow=false
spring.datasource.dynamic.druid.test-on-return=false
# druid 打开PSCache,并且指定每个连接上PSCache的大小
spring.datasource.dynamic.druid.pool-prepared-statements=true
spring.datasource.dynamic.druid.max-open-prepared-statements=20
spring.datasource.dynamic.druid.max-pool-prepared-statement-per-connection-size=20
# druid 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
spring.datasource.dynamic.druid.filters=stat,slf4j
# druid 状态监控
spring.datasource.dynamic.druid.filter.stat.enabled=true
spring.datasource.dynamic.druid.filter.stat.db-type=mysql
spring.datasource.dynamic.druid.filter.stat.log-slow-sql=true
spring.datasource.dynamic.druid.filter.stat.slow-sql-millis=2000
# druid 监控过滤器
spring.datasource.dynamic.druid.web-stat-filter.enabled=true
spring.datasource.dynamic.druid.web-stat-filter.exclusions="*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"
# druid 监控页面
spring.datasource.dynamic.druid.stat-view-servlet.enabled=true
spring.datasource.dynamic.druid.stat-view-servlet.url-pattern=/druid/*
spring.datasource.dynamic.druid.stat-view-servlet.reset-enable=false
spring.datasource.druid.stat-view-servlet.login-username=root
spring.datasource.druid.stat-view-servlet.login-password=root
spring.datasource.dynamic.druid.stat-view-servlet.allow=
mybatis-plus.mapper-locations=classpath*:/mybatis/*/**.xml
mybatis-plus.type-aliases-package=com.bims.hopson.*.entity
spring.jackson.serialization.indent_output=true
# redis 配置
#spring.redis.host=192.168.0.100
#spring.redis.host=127.0.0.1
#spring.redis.port=6379
#spring.redis.pool.max-idle=20
3、在启动类上加注解
@SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)
4、启动日志