8. MyBatisPlus性能分析

MyBatisPlus性能分析-开发环境使用

  • 性能分析拦截器,用于输出每条 SQL 语句及其执行时间。
  • SQL 性能执行分析,开发环境使用,超过指定时间,停止运行。有助于发现问题

1. 配置插件

1.1 参数说明

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

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

1.2 在 MybatisPlusConfig 中配置
  • public PerformanceInterceptor performanceInterceptor();
/**
 * @Date 2020/4/10 22:53
 * @Version 10.21
 * @Author DuanChaojie
 */
@Configuration
@EnableTransactionManagement
@MapperScan("cn.justweb.mybatisplus.mapper")
public class MyBatisPlusConfig {

	
    // .......
    
    /**
     * SQL 执行性能分析插件
     * 开发环境使用,线上不推荐。 maxTime 指的是 sql 最大执行时长
     */
    @Bean
    @Profile({"dev","test"})// 设置 dev test 环境开启
    public PerformanceInterceptor performanceInterceptor(){
        PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
        // ms 超过此时间则sql不执行
        performanceInterceptor.setMaxTime(500);
        performanceInterceptor.setFormat(true);
        return performanceInterceptor;
    }
}
1.3 Spring Boot 中设置dev环境
  • 可以针对各环境新建不同的配置文件application-dev.propertiesapplication-test.propertiesapplication-prod.properties也可以自定义环境名称:如test1、test2
#环境设置:dev、test、prod
spring.profiles.active=dev

2. 测试

2.1 常规测试
    /**
     * 测试 性能分析插件
     */
    @Test
    public void testPerformance() {
        User user = new User();
        user.setName("我是Helen");
        user.setEmail("helen@sina.com");
        user.setAge(18);
        
        userMapper.insert(user);
    }

输出:

在这里插入图片描述

maxTime 改小之后再次进行测试

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

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

输出:

在这里插入图片描述

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值