springboot结合p6spy进行SQL监控

embedded/2024/9/24 4:26:17/

1.学习p6spy的相关链接

英文文档:Integrating P6Spy — p6spy 3.9.2-SNAPSHOT documentationicon-default.png?t=O83Ahttps://p6spy.readthedocs.io/en/latest/integration.html

github链接:GitHub - p6spy/p6spy: P6Spy is a framework that enables database data to be seamlessly intercepted and logged with no code changes to the application.icon-default.png?t=O83Ahttps://github.com/p6spy/p6spy

mybatis-plus相关介绍:

SQL分析与打印 | MyBatis-Plus (baomidou.com)icon-default.png?t=O83Ahttps://baomidou.com/guides/p6spy/

2.p6spy简介

2.1p6spy是什么

P6Spy 是一款开源的 Java 框架,是一个强大的数据库监控工具。可以帮助开发者监控并分析应用程序与数据库之间的交互行为。

2.2p6spy能做什么

  • 像往常一样正常使用mybatisplus或mybatisflex,p6spy在幕后帮我们记录所有的SQL及其执行时间。
  • 通过配置进行慢SQL检测
  • 自定义输出SQL相关性能的日志格式
  • 对日志输出进行控制,可输出到控制台或者文件系统

3.p6spy整合springboot

1.在pom.xml文件引入依赖:

   <dependency><groupId>p6spy</groupId><artifactId>p6spy</artifactId><version>3.9.1</version></dependency>

2.在application.yml文件修改数据库相关配置:

配置之前:

spring:datasource:driver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://localhost:3306/mybatis-flex?username: rootpassword: 123456

修改配置后:

spring:datasource:driver-class-name: com.p6spy.engine.spy.P6SpyDriverurl: jdbc:p6spy:mysql://localhost:3306/mybatis-flex?username: rootpassword: 123456

3.在application.yml同级创建spy.properties文件,内容如下:

module.log=com.p6spy.engine.logging.P6LogFactory,com.p6spy.engine.outage.P6OutageFactory
# 使用日志系统记录sql
appender=com.p6spy.engine.spy.appender.Slf4JLogger
## 配置记录Log例外
excludecategories=info,debug,result,batc,resultset
# 设置使用p6spy driver来做代理
deregisterdrivers=true
# 日期格式
dateformat=yyyy-MM-dd HH:mm:ss
# 实际驱动
driverlist=com.mysql.jdbc.Driver
# 是否开启慢SQL记录
outagedetection=true
# 慢SQL记录标准 秒
outagedetectioninterval=2
logMessageFormat=com.p6spy.engine.spy.appender.CustomLineFormat
customLogMessageFormat=%(currentTime) | SQL耗时: %(executionTime) ms | 连接信息: %(category)-%(connectionId) | 执行语句: %(sql)

4.启动后查询数据库中的数据,控制台会自动打印内容如下:


http://www.ppmy.cn/embedded/115900.html

相关文章

[第一章]java快速入门

本专栏内容为&#xff1a;java学习专栏 &#x1f493;博主csdn个人主页&#xff1a;小小unicorn ⏩专栏分类&#xff1a;java &#x1f69a;代码仓库&#xff1a;小小unicorn的代码仓库&#x1f69a; &#x1f339;&#x1f339;&#x1f339;关注我带你学习编程知识 目录 hell…

苍穹外卖学习笔记(九)

由于前面已经有系统学习Redis的文章&#xff0c;这里不再详细书写Redis的入门知识(数据结构、常用命令) 一. Java中操作Redis Redis的Java客户端 常见的几种&#xff1a; JedisLettuceSpring Data Redis(建议) Spring Data Redis使用方式 操作步骤&#xff1a; 导入Spri…

Cilium + ebpf 系列文章-ebpf-tool(三)

本篇文章: 主要是在介绍和演示用户空间侧ebpf调试工具-ebpf-tool 一、项目地址 从 GitHub 获取 bpftool 虽然 bpftool 主要包含在 Linux 内核源代码中,但你也可以从 GitHub 上的镜像仓库中获取。 克隆 bpftool 仓库: git clone https://github.com/libbpf/bpftool.git cd bp…

基于LSTM的文本摘要生成实战教程

基于LSTM的文本摘要生成实战教程 文本摘要生成是自然语言处理&#xff08;NLP&#xff09;中的一个重要任务。其目标是将长篇文章或文档自动生成简洁的摘要&#xff0c;而保证保留原文的关键信息。近年来&#xff0c;基于深度学习的模型&#xff0c;如LSTM&#xff08;长短期记…

Springboot的三层架构

package com.wzb.ThreeLevelsExercise20240919;public class Exercise {// 内聚&#xff1a;内聚是指一个模块或内部各元素的紧密程度。高内聚则是一个模块或类中的所有功能都是紧密相关的&#xff0c;专注于完成单一任务// 高内聚的好处&#xff1a;// 1.易于维护&#xff1a;…

四十一、完成内容添加功能(使用go测试方法)

目录 一、添加model 二、完成相关dao 三、使用测试类进行测试 1、把光标防止要测试的方法上&#xff0c;右击并选择 2、自动会生成一个以dao文件加_test命名的文件 3、在其中完善方法并完成测试 四、完成content_create_handle 一、添加model 按数据库字段以及字段格式完…

力扣 LCR 020 回文子串 -Python

题目链接&#xff1a;LCR 020. 回文子串 - 力扣&#xff08;LeetCode&#xff09; 题目描述&#xff1a; 给定一个字符串 s &#xff0c;请计算这个字符串中有多少个回文子字符串。 具有不同开始位置或结束位置的子串&#xff0c;即使是由相同的字符组成&#xff0c;也会被视…

网页通知设计灵感:CSS 和 JS 的 8 大创意实现

文章目录 前言正文1.霓虹灯风格的通知系统2.垂直时间轴通知3.动画徽章通知4.项目式通知5.多种状态通知&#xff1a;成功、错误、警告6.信息、警告、提示组件7.扁平化风格通知8.社交媒体风格弹出通知 总结 前言 网页通知如今已成为电商、社交平台等网站的常见功能&#xff0c;它…