Springboot mybatis plus druid多數(shù)據(jù)源解決方案 dynamic-datasource的使用詳解
依賴(lài)
<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#日志輸出到控制臺(tái)appender=com.baomidou.mybatisplus.extension.p6spy.StdoutLogger# 使用日志系統(tǒng)記錄 sql#appender=com.p6spy.engine.spy.appender.Slf4JLogger# 設(shè)置 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# 實(shí)際驅(qū)動(dòng)可多個(gè)#driverlist=org.h2.Driver# 是否開(kāi)啟慢SQL記錄outagedetection=true# 慢SQL記錄標(biāo)準(zhǔn) 2 秒outagedetectioninterval=5
配置文件application.yml
server: port: 8080spring: datasource: dynamic: p6spy: true # 默認(rèn)false,建議線(xiàn)上關(guān)閉。 primary: master #設(shè)置默認(rèn)的數(shù)據(jù)源或者數(shù)據(jù)源組,默認(rèn)值即為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 #最大連接池?cái)?shù)量 max-active: 20 # 最小連接池?cái)?shù)量 min-idle: 3 # 配置獲取連接等待超時(shí)的時(shí)間 單位毫秒 max-wait: 60000 # 配置間隔多久才進(jìn)行一次檢測(cè),檢測(cè)需要關(guān)閉的空閑連接,單位是毫秒 time-between-eviction-runs-millis: 60000 # 配置一個(gè)連接在池中最小生存的時(shí)間,單位是毫秒 min-evictable-idle-time-millis: 300000 # 測(cè)試連接 validation-query: select # 申請(qǐng)連接的時(shí)候檢測(cè),建議配置為true,不影響性能,并且保證安全性 test-while-idle: true # 獲取連接時(shí)執(zhí)行檢測(cè),建議關(guān)閉,影響性能 test-on-borrow: false # 歸還連接時(shí)執(zhí)行檢測(cè),建議關(guān)閉,影響性能 test-on-return: false # 打開(kāi)PSCache,并且指定每個(gè)連接上PSCache的大小 pool-prepared-statements: true # 配置監(jiān)控統(tǒng)計(jì)攔截器 防火墻 日志配置 # stat監(jiān)控?cái)?shù)據(jù)庫(kù)性能 # wall 用于防火墻 # 日志 slf4j logback # log4j # log4j2 # 配置多個(gè)英文逗號(hào)分隔 filters: stat,wall,slf4j max-pool-prepared-statement-per-connection-size: 20 # 合并多個(gè)DruidDataSource的監(jiān)控?cái)?shù)據(jù) use-global-data-source-stat: true # 通過(guò)connectProperties屬性來(lái)打開(kāi)mergeSql功能;慢SQL記錄 SQL優(yōu)化 connect-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500 # ===========監(jiān)控配置=========== # WebStatFilter配置,說(shuō)明請(qǐng)參考Druid Wiki,配置_配置WebStatFilter web-stat-filter: #是否啟用StatFilter默認(rèn)值false enabled: true # 攔截所有的 url-pattern: /* # 排除一些不必要的url,比如*:js,/jslib/*等等: exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico, /druid/*' # 關(guān)閉和開(kāi)啟session 統(tǒng)計(jì)功能 默認(rèn)關(guān)閉 # session-stat-enable: true # sessionStatMaxCount是1000個(gè) # session-stat-max-count: 1000 # 配置principalSessionName,使得druid能夠知道當(dāng)前的session的用戶(hù)是誰(shuí) # principal-session-name: # StatViewServlet配置,說(shuō)明請(qǐng)參考Druid Wiki,配置_StatViewServlet配置 stat-view-servlet: # 是否啟用StatViewServlet默認(rèn)值true enabled: true url-pattern: /druid/* # 界面上有一個(gè)重置功能 執(zhí)行這個(gè)操作之后,會(huì)導(dǎo)致所有計(jì)數(shù)器清零,重新計(jì)數(shù) 建議false reset-enable: false # 登錄的后臺(tái)的用戶(hù)名,密碼 login-username: admin login-password: adminmybatis-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 {}
啟動(dòng)類(lèi)上添加@SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)
@SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)public class ThreadApplication { public static void main(String[] args) { SpringApplication.run(ThreadApplication.class, args); }}
遇到的坑:記得要去掉DruidConfig這個(gè)文件,不然會(huì)報(bào)錯(cuò)
到此這篇關(guān)于Springboot mybatis plus druid多數(shù)據(jù)源解決方案 dynamic-datasource的使用的文章就介紹到這了,更多相關(guān)Springboot mybatis plus druid多數(shù)據(jù)源內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. python多線(xiàn)程和多進(jìn)程關(guān)系詳解2. 詳解瀏覽器的緩存機(jī)制3. Xml簡(jiǎn)介_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理4. Python 實(shí)現(xiàn)勞拉游戲的實(shí)例代碼(四連環(huán)、重力四子棋)5. 一款功能強(qiáng)大的markdown編輯器tui.editor使用示例詳解6. JSP之表單提交get和post的區(qū)別詳解及實(shí)例7. Python xlrd/xlwt 創(chuàng)建excel文件及常用操作8. 存儲(chǔ)于xml中需要的HTML轉(zhuǎn)義代碼9. .Net加密神器Eazfuscator.NET?2023.2?最新版使用教程10. ASP動(dòng)態(tài)網(wǎng)頁(yè)制作技術(shù)經(jīng)驗(yàn)分享
