SpringBoot集成P6Spy實(shí)現(xiàn)SQL日志的記錄詳解
P6Spy簡介
P6Spy是一個(gè)可以用來在應(yīng)用程序中攔截和修改數(shù)據(jù)操作語句的開源框架。
通過P6Spy可以對SQL語句進(jìn)行攔截,相當(dāng)于一個(gè)SQL語句的記錄器,這樣我們可以用它來作相關(guān)的分析,比如性能分析。
應(yīng)用場景
pom
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.20</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>p6spy</groupId>
<artifactId>p6spy</artifactId>
<version>3.8.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
</dependency>
</dependencies>application.yml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
#driver-class-name: com.p6spy.engine.spy.P6SpyDriver
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://localhost:3306/course_db?serverTimezone=GMT%2B8
# url: jdbc:p6spy:mysql://localhost:3306/course_db?serverTimezone=GMT%2B8
username: root
password: root# 打開mybatis-plus的sql日志輸出
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
entity
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("course_1")
public class Course {
@TableField("cid")
private Long cId;
private String cName;
private Integer userId;
private String cStatus;
}
Mapper
public interface CourseMapper extends BaseMapper<Course> {
}
啟動類
@SpringBootApplication
@MapperScan(basePackages = "cn.zysheep.mapper")
public class ShardingjdbcdemoApplication {
public static void main(String[] args) {
SpringApplication.run(ShardingjdbcdemoApplication.class, args);
}
}測試類
@SpringBootTest
@Slf4j
class ShardingjdbcdemoApplicationTests {
@Autowired
private CourseMapper courseMapper;
@SneakyThrows
@Test
void findCourse() {
courseMapper.selectList(null).forEach(System.out::println);
}
}
mybatis-plus也可以打印輸出的sql日志,但是不是我們想要的效果,如何來控制想要的sql日志輸出,可以使用P6Spy開源產(chǎn)品。

P6Spy入門使用
spy.properties
resources目錄添加配置文件,類似log4j.xml,記錄配置信息
module.log=com.p6spy.engine.logging.P6LogFactory,com.p6spy.engine.outage.P6OutageFactory
# sql日志打印輸出
# 1、logMessageFormat=com.p6spy.engine.spy.appender.SingleLineFormat
# 2、logMessageFormat=com.p6spy.engine.spy.appender.CustomLineFormat
# customLogMessageFormat=%(currentTime) | SQL use time: %(executionTime) ms | connect info: %(category)-%(connectionId) | execute sql: %(sql)
# 3、自定義日志打印(全限定類名)
logMessageFormat=cn.zysheep.config.P6SPYConfig
# 使用日志系統(tǒng)記錄sql
appender=com.p6spy.engine.spy.appender.Slf4JLogger
## 配置記錄Log例外
excludecategories=info,debug,result,batc,resultset
# 設(shè)置使用p6spy driver來做代理
deregisterdrivers=true
# 日期格式
dateformat=yyyy-MM-dd HH:mm:ss
# 實(shí)際驅(qū)動
driverlist=com.mysql.cj.jdbc.Driver
# 是否開啟慢SQL記錄
outagedetection=true
# 慢SQL記錄標(biāo)準(zhǔn) 秒
outagedetectioninterval=2
P6Spy有內(nèi)置的SQL輸出格式,如上配置文件。這里我們使用自定義SQL日志打印
P6SPYConfig
public class P6SPYConfig implements MessageFormattingStrategy {
@Override
public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql, String url) {
Map<String, Object> message = new LinkedHashMap<>(8);
String newPrepared = prepared.replace(" ", "").replace("\n", " ");
message.put("prepared", newPrepared);
String newSql = sql.replace(" ", "").replace("\n", " ");
message.put("sql", newSql);
return JSONObject.toJSONString(message, true);
}
}application.yml
spring:
datasource:
# driver-class-name: com.mysql.cj.jdbc.Driver
driver-class-name: com.p6spy.engine.spy.P6SpyDriver
type: com.alibaba.druid.pool.DruidDataSource
# url: jdbc:mysql://localhost:3306/course_db?serverTimezone=GMT%2B8
url: jdbc:p6spy:mysql://localhost:3306/course_db?serverTimezone=GMT%2B8
username: root
password: root# 打開mybatis-plus的sql日志輸出
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
測試類不變

到此這篇關(guān)于SpringBoot集成P6Spy實(shí)現(xiàn)SQL日志的記錄詳解的文章就介紹到這了,更多相關(guān)SpringBoot集成P6Spy內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺談SpringBoot內(nèi)嵌Tomcat的實(shí)現(xiàn)原理解析
這篇文章主要介紹了淺談SpringBoot內(nèi)嵌Tomcat的實(shí)現(xiàn)原理解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
SpringBoot+WebSocket+Netty實(shí)現(xiàn)消息推送的示例代碼
這篇文章主要介紹了SpringBoot+WebSocket+Netty實(shí)現(xiàn)消息推送的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
IDEA解決@Slf4j中l(wèi)og報(bào)紅問題
在IntelliJ IDEA中使用log.info()時(shí),如果出現(xiàn)錯(cuò)誤,通常是因?yàn)槿鄙貺ombok插件,以下是解決方法:打開IntelliJ IDEA,進(jìn)入設(shè)置(File > Settings 或者 Ctrl+Alt+S),在Plugins部分點(diǎn)擊Browse repositories,搜索Lombok并安裝,安裝完成后,問題通常可以解決2024-12-12
集群環(huán)境中使用ehcache_動力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了集群環(huán)境中使用ehcache的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
Java+Selenium實(shí)現(xiàn)文件上傳下載功能詳解
這篇文章主要介紹了java代碼如何利用selenium操作瀏覽器上傳和下載文件功能,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以參考一下2023-01-01
springboot基于keytool實(shí)現(xiàn)https的雙向認(rèn)證示例教程
這篇文章主要介紹了springboot基于keytool實(shí)現(xiàn)https的雙向認(rèn)證,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06
Java實(shí)現(xiàn)支付寶之第三方支付寶即時(shí)到賬支付功能
這篇文章主要介紹了Java實(shí)現(xiàn)支付寶之第三方支付寶即時(shí)到賬支付功能的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07
Springboot 2.x中server.servlet.context-path的運(yùn)用詳解
這篇文章主要介紹了Springboot 2.x中server.servlet.context-path的運(yùn)用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
Java線性結(jié)構(gòu)中的雙向鏈表實(shí)現(xiàn)原理
這篇文章將給大家詳細(xì)講解雙向鏈表的內(nèi)容,尤其是會通過代碼來進(jìn)行鏈表的操作,文中的代碼示例介紹的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下2023-07-07
mybatis if test 不為空字符串且不為null的問題
這篇文章主要介紹了mybatis if test 不為空字符串且不為null的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03

