“ 在平时我们使用 mybatis plus 的日志打印配置一般都是
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
这种方式打印出来的 sql 日志太长太繁琐,有时候太长也不是件好事,今日就安利一种非常清晰的 sql 日志打印。
”
今日安利好物:Mybatis Plus 的 PerformanceInterceptor
PerformanceInterceptor 是什么?
PerformanceInterceptor 是 mybatis plus 的一个性能分析拦截器,用于输出每条 SQL 语句及其执行时间。
PerformanceInterceptor 有什么作用?
性能分析拦截器,用于输出每条 SQL 语句及其执行时间
参数:maxTime SQL 执行最大时长,超过自动停止运行,有助于发现问题。
参数:format SQL是否格式化,默认false。
01
—
配置使用 PerformanceInterceptor 性能分析插件
我们只需要新增一个 mybatis plus 的配置类,注入 PerformanceInterceptor bean 就可以了
@Configurationpublic class MybatisPlusConfig {
/** * 打印 sql */ @Bean @Profile({
"dev","pro"})// 设置 dev pro 环境开启日志打印 public PerformanceInterceptor performanceInterceptor() {
PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor(); //格式化sql语句 Properties properties = new Properties(); properties.setProperty("format", "false"); performanceInterceptor.setProperties(properties); return performanceInterceptor; }}
配置好之后,请求一个带有查询和插入的方法,控制台会输出最终执行的 sql 和 执行时间
效果图:
02
—
关于 PerformanceInterceptor 的坑
关于 PerformanceInterceptor 版本问题
问题:3.2 无法使用此性能分析插件,导包时无法导入
原因:如果 mp 在3.1版本时使用是不会有问题的,但是如果你的 mp 是3.2的版本,就无法使用这个插件了,因为 mp 在3.2的版本已经移除了这个性能分析插件并推荐使用第三方插件。
3.1 源码