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("helen@sina.com");
    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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值