Springboot mybatis plus druid多數(shù)據(jù)源解決方案 dynamic-datasource的使用詳解
依賴
<dependency> <groupId>com.baomidou</groupId> <artifactId>dynamic-datasource-spring-boot-starter</artifactId> <version>2.5.0</version> </dependency> <dependency> <groupId>p6spy</groupId> <artifactId>p6spy</artifactId> <version>3.9.1</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.21</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.3.1.tmp</version> </dependency>
p6spy配置文件spy.properties,有性能影響,生產(chǎn)環(huán)境建議關(guān)閉
#3.2.1以上使用 modulelist=com.baomidou.mybatisplus.extension.p6spy.MybatisPlusLogFactory #3.2.1以下使用或者不配置 #modulelist=com.p6spy.engine.logging.P6LogFactory,com.p6spy.engine.outage.P6OutageFactory # 自定義日志打印 logMessageFormat=com.baomidou.mybatisplus.extension.p6spy.P6SpyLogger #日志輸出到控制臺 appender=com.baomidou.mybatisplus.extension.p6spy.StdoutLogger # 使用日志系統(tǒng)記錄 sql #appender=com.p6spy.engine.spy.appender.Slf4JLogger # 設置 p6spy driver 代理 deregisterdrivers=true # 取消JDBC URL前綴 useprefix=true # 配置記錄 Log 例外,可去掉的結(jié)果集有error,info,batch,debug,statement,commit,rollback,result,resultset. excludecategories=info,debug,result,commit,resultset # 日期格式 dateformat=yyyy-MM-dd HH:mm:ss # 實際驅(qū)動可多個 #driverlist=org.h2.Driver # 是否開啟慢SQL記錄 outagedetection=true # 慢SQL記錄標準 2 秒 outagedetectioninterval=5
配置文件application.yml
server:
port: 8080
spring:
datasource:
dynamic:
p6spy: true # 默認false,建議線上關(guān)閉。
primary: master #設置默認的數(shù)據(jù)源或者數(shù)據(jù)源組,默認值即為master
datasource:
master:
url: jdbc:mysql://localhost:3306/bdata?serverTimezone=GMT%2B8&characterEncoding=utf8&autoReconnect=true&rewriteBatchedStatements=true&allowMultiQueries=true
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
slave:
url: jdbc:mysql://localhost:3306/lmkstudy?serverTimezone=GMT%2B8&characterEncoding=utf8&autoReconnect=true&rewriteBatchedStatements=true&allowMultiQueries=true
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
druid:
password: root
username: root
# 初始化連接大小
initial-size: 5
#最大連接池數(shù)量
max-active: 20
# 最小連接池數(shù)量
min-idle: 3
# 配置獲取連接等待超時的時間 單位毫秒
max-wait: 60000
# 配置間隔多久才進行一次檢測,檢測需要關(guān)閉的空閑連接,單位是毫秒
time-between-eviction-runs-millis: 60000
# 配置一個連接在池中最小生存的時間,單位是毫秒
min-evictable-idle-time-millis: 300000
# 測試連接
validation-query: select
# 申請連接的時候檢測,建議配置為true,不影響性能,并且保證安全性
test-while-idle: true
# 獲取連接時執(zhí)行檢測,建議關(guān)閉,影響性能
test-on-borrow: false
# 歸還連接時執(zhí)行檢測,建議關(guān)閉,影響性能
test-on-return: false
# 打開PSCache,并且指定每個連接上PSCache的大小
pool-prepared-statements: true
# 配置監(jiān)控統(tǒng)計攔截器 防火墻 日志配置
# stat監(jiān)控數(shù)據(jù)庫性能
# wall 用于防火墻
# 日志 slf4j logback
# log4j
# log4j2
# 配置多個英文逗號分隔
filters: stat,wall,slf4j
max-pool-prepared-statement-per-connection-size: 20
# 合并多個DruidDataSource的監(jiān)控數(shù)據(jù)
use-global-data-source-stat: true
# 通過connectProperties屬性來打開mergeSql功能;慢SQL記錄 SQL優(yōu)化
connect-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
# ===========監(jiān)控配置===========
# WebStatFilter配置,說明請參考Druid Wiki,配置_配置WebStatFilter
web-stat-filter:
#是否啟用StatFilter默認值false
enabled: true
# 攔截所有的
url-pattern: /*
# 排除一些不必要的url,比如*:js,/jslib/*等等:
exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico, /druid/*"
# 關(guān)閉和開啟session 統(tǒng)計功能 默認關(guān)閉
# session-stat-enable: true
# sessionStatMaxCount是1000個
# session-stat-max-count: 1000
# 配置principalSessionName,使得druid能夠知道當前的session的用戶是誰
# principal-session-name:
# StatViewServlet配置,說明請參考Druid Wiki,配置_StatViewServlet配置
stat-view-servlet:
# 是否啟用StatViewServlet默認值true
enabled: true
url-pattern: /druid/*
# 界面上有一個重置功能 執(zhí)行這個操作之后,會導致所有計數(shù)器清零,重新計數(shù) 建議false
reset-enable: false
# 登錄的后臺的用戶名,密碼
login-username: admin
login-password: admin
mybatis-plus:
type-aliases-package: com.site.datasource.entity
mapper-locations: classpath*:mapper/*.xml
service層添加注解@DS(“slave”),選定數(shù)據(jù)源
@DS("slave")
@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}
啟動類上添加@SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)
@SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)
public class ThreadApplication {
public static void main(String[] args) {
SpringApplication.run(ThreadApplication.class, args);
}
}
遇到的坑:記得要去掉DruidConfig這個文件,不然會報錯
到此這篇關(guān)于Springboot mybatis plus druid多數(shù)據(jù)源解決方案 dynamic-datasource的使用的文章就介紹到這了,更多相關(guān)Springboot mybatis plus druid多數(shù)據(jù)源內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot集成Druid實現(xiàn)多數(shù)據(jù)源的兩種方式
- SpringBoot整合Mybatis-Plus+Druid實現(xiàn)多數(shù)據(jù)源配置功能
- springboot mybatis druid配置多數(shù)據(jù)源教程
- 關(guān)于springboot配置druid數(shù)據(jù)源不生效問題(踩坑記)
- 使用springboot+druid雙數(shù)據(jù)源動態(tài)配置操作
- springboot配置多數(shù)據(jù)源并集成Druid和mybatis的操作
- SpringBoot環(huán)境Druid數(shù)據(jù)源使用及特點
- SpringBoot整合Druid數(shù)據(jù)源過程詳解
- 通過springboot+mybatis+druid配置動態(tài)數(shù)據(jù)源
- springboot 動態(tài)數(shù)據(jù)源的實現(xiàn)方法(Mybatis+Druid)
- Spring Boot+Mybatis+Druid+PageHelper實現(xiàn)多數(shù)據(jù)源并分頁的方法
- 詳解Spring Boot整合Mybatis實現(xiàn) Druid多數(shù)據(jù)源配置
- spring使用xml方式整合Druid數(shù)據(jù)源連接池
相關(guān)文章
Java通過PowerMockito和Mokito進行單元測試的實現(xiàn)
PowerMockito和Mockito都是Java語言中的測試框架,用于進行單元測試和集成測試,本文就來詳細的介紹一下通過PowerMockito和Mokito進行單元測試,感興趣的可以了解一下2023-08-08
Spring中的FactoryBean與BeanFactory詳細解析
這篇文章主要介紹了Spring中的FactoryBean與BeanFactory詳細解析,在Spring框架中,FactoryBean和BeanFactory是兩個關(guān)鍵的接口,用于創(chuàng)建和管理對象實例,它們在Spring的IoC(Inversion of Control,控制反轉(zhuǎn))容器中發(fā)揮著重要的作用,需要的朋友可以參考下2023-11-11
java實現(xiàn)微信公眾平臺發(fā)送模板消息的示例代碼
這篇文章主要介紹了java實現(xiàn)微信公眾平臺發(fā)送模板消息的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-09-09
Java面向?qū)ο蠡A(chǔ)知識之數(shù)組和鏈表
這篇文章主要介紹了Java面向?qū)ο蟮闹當?shù)組和鏈表,文中有非常詳細的代碼示例,對正在學習java基礎(chǔ)的小伙伴們有很好的幫助,需要的朋友可以參考下2021-11-11
Java實現(xiàn)excel大數(shù)據(jù)量導入
這篇文章主要為大家詳細介紹了Java實現(xiàn)excel大數(shù)據(jù)量導入,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-08-08
Java連接Sql數(shù)據(jù)庫經(jīng)常用到的操作
這篇文章主要介紹了Java連接Sql數(shù)據(jù)庫經(jīng)常用到的操作的相關(guān)資料,需要的朋友可以參考下2016-02-02

