性能分析插件
我们在平常的开发中,会遇到一些慢SQL。MP提供的性能分析插件,如果超过这个时间就会停止运行!
作用:用于输出每条sql语句及其执行的时间
1、导入插件
@Configuration
@MapperScan("com.cm.mapper")
@EnableTransactionManagement // 增加事务管理
public class MybatisPlusConfig {
/**
* sql执行效率插件
* @return
*/
@Bean
@Profile({"dev","test"})
public PerformanceInterceptor performanceInterceptor() {
PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
// 在工作中,不允许用户等待
performanceInterceptor.setMaxTime(1); // ms 设置sql执行的最大s时间,如果超过了则不执行
performanceInterceptor.setFormat(true); // 是否进行格式化
return new PerformanceInterceptor();
}
}
记住,要在springboot中配置环境为dev或者test环境
## 设置开发环境
spring.profiles.active=dev
2、测试使用
查询测试
// 继承了BaseMapper,所有的方法都来自父类,我们也可以编写自己的扩展方法
@Test
void contextLoads() {
List<User> users = userMapper.selectList(null);
users.forEach(System.out::println);
}