MybatisPlus性能分析插件

性能分析

性能分析拦截器,用于输出每条 SQL 语句及其执行时间

SQL 性能执行分析,开发环境使用,超过指定时间,停止运行。有助于发现问题

配置插件

(1)参数说明

参数:maxTime: SQL 执行最大时长,超过自动停止运行,有助于发现问题。

参数:format: SQL是否格式化,默认false。

(2)在 MybatisPlusConfig 中配置

/**
 * SQL 执行性能分析插件
 * 开发环境使用,线上不推荐。 maxTime 指的是 sql 最大执行时长
 *
 * 三种环境
 *      * dev:开发环境
 *      * test:测试环境
 *      * prod:生产环境
 */
@Bean
@Profile({"dev","test"})// 设置 dev test 环境开启
public PerformanceInterceptor performanceInterceptor() {
	PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
	performanceInterceptor.setMaxTime(500);//ms,超过此处设置的ms则sql不执行
	performanceInterceptor.setFormat(true);
	return performanceInterceptor;
}

(3)Spring Boot 中设置dev环境

#环境设置:dev、test、prod
spring.profiles.active=dev

可以针对各环境新建不同的配置文件application-dev.propertiesapplication-test.propertiesapplication-prod.properties

也可以自定义环境名称:如test1、test2

测试

(1)常规测试

/**
 * 测试 性能分析插件
 */
@Test
public void testPerformance() {

    User user = new User();
    user.setName("我是Helen");
    user.setEmail("[email protected]");
    user.setAge(18);
    userMapper.insert(user);
}

(2)将maxTime 改小之后再次进行测试

performanceInterceptor.setMaxTime(5);//ms,超过此处设置的ms不执行

如果执行时间过长,则抛出异常:The SQL execution time is too large, 

其它

如果想进行复杂条件查询,那么需要使用条件构造器 Wapper,涉及到如下方法

1、delete

2、selectOne

3、selectCount

4、selectList

5、selectMaps

6、selectObjs

7、update

 

你可能感兴趣的:(MybatisPlus性能分析插件)