目标:
懂得使用 Mybatis-Plus 对 你的SQL语句 进行性能分析
实现步骤:
1、编写性能分析插件2、配置SpringBoot的开发环境
一、为什么需要对sql进行性能分析 ?
在日常开发中,我们写完 sql 后需要对 sql 进行测试,当发现测试时间很长时,证明我们不行🐕 证明我们需要对 sql 进行优化了
所以说,Mybatis-Plus 提供了性能分析插件,如果 sql 的执行时间超过 就停止运行,并返回错误
综上所述,性能分析拦截器,就是用于输出每条 sql 语句的执行时间的插件
二、实现性能分析案例
1、编写性能分析插件
/*性能分析插件*/
@Bean
@Profile(value = {"test"}) // 设置 test环境下 会开启SQL性能分析
public PerformanceInterceptor performanceInterceptor(){
return new PerformanceInterceptor();
}
2、 配置SpringBoot的环境为 测试环境(Test)
# 配置环境
spring.profiles.active=test
3、执行测试一波,看结果

4、接下来自定义我们的性能分析条件
/*性能分析插件*/
@Bean
@Profile(value = {"test"}) // 设置 test环境下 会开启SQL性能分析
public PerformanceInterceptor performanceInterceptor(){
PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
performanceInterceptor.setMaxTime(10); // 单位 为毫秒,如果sql执行时间超过这个时间,报错
performanceInterceptor.setFormat(true); // 是否格式化代码,它会在控制台格式化sql
return performanceInterceptor;
}
参数含义:

5、再执行测试一波,看结果


本文介绍了如何利用Mybatis-Plus的性能分析插件进行SQL性能优化。通过编写性能分析插件并配置SpringBoot测试环境,当SQL执行时间超过设定阈值时,系统将停止运行并提示错误。此外,还展示了如何自定义性能分析条件,如最大执行时间和是否格式化SQL。性能分析有助于提升应用的性能和效率。
523

被折叠的 条评论
为什么被折叠?



