MyBatis-Plus提供了内置的分析SQL执行时间的插件,我们只需要进行配置即可使用:
@Configurationpublic class MyBatisPlusConfig {/*** Profile注解用于设置 dev test 环境开启,保证我们的效率** @return new PerformanceInterceptor()*/@Bean@Profile({"dev", "test"})public PerformanceInterceptor performanceInterceptor() {PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();// 设置sql执行的最大时间(单位是ms),如果超过了则不执行performanceInterceptor.setMaxTime(100);// 是否格式化日志的sqlperformanceInterceptor.setFormat(true);return performanceInterceptor;}}
配置环境为dev或test环境:
spring:#配置为dev环境profiles:active: dev
使用效果:
如果SQL执行时间超过了设置的最大值,就会抛出异常:
