MyBatis-Plus—性能分析插件

195 阅读1分钟

我们在平时的开发中,会遇到一些慢sql。测试! druid,,,,, 作用:性能分析拦截器,用于输出每条 SQL 语句及其执行时间 MP也提供性能分析插件,如果超过这个时间就停止运行!

步骤一、导入插件

/**
* SQL执行效率插件
*/
@Bean
@Profile({"dev","test"})// 设置 dev test 环境开启,保证我们的效率
public PerformanceInterceptor performanceInterceptor() {
    PerformanceInterceptor performanceInterceptor = new
        PerformanceInterceptor();
    performanceInterceptor.setMaxTime(100); // ms设置sql执行的最大时间,如果超过了则不
    执行
        performanceInterceptor.setFormat(true); // 是否格式化代码
    return performanceInterceptor;
}

记住,要在SpringBoot中配置环境为dev或者 test 环境才有效!

步骤二、测试使用!

@Test
void contextLoads() {
    // 参数是一个 Wrapper ,条件构造器,这里我们先不用 null
    // 查询全部用户
    List<User> users = userMapper.selectList(null);
    users.forEach(System.out::println);
}

使用性能分析插件,可以帮助我们提高效率!