Mybatis-plus性能分析插件—PerformanceInterceptor

 Mybatis-plus自带SQL执行性能分析插件,配置如下

@Configuration
public class MyBatiesPlusConfiguration {

    /**
     * 分页插件,自动识别数据库类型
     * 多租户,请参考官网【插件扩展】
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        // 开启 PageHelper 的支持
        paginationInterceptor.setDialectType("mysql");
        paginationInterceptor.setLocalPage(true);
        return paginationInterceptor;
    }

     /**
     * MyBatiesPlus sql执行性能分析
     * @return
      */
    @Bean
    public PerformanceInterceptor performanceInterceptor() {
        PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
        //格式化sql语句
        Properties properties = new Properties();
        properties.setProperty("format", "false");
        performanceInterceptor.setProperties(properties);
        return performanceInterceptor;
    }
}

程序执行结果

执行接口时会把SQL打印出来,包括SQL执行参数,并打印具体SQL执行时间

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
MyBatis-Plus是一个在MyBatis基础上进行封装的增强工具包。它提供了很多实用的功能,包括性能分析插件性能分析插件MyBatis-Plus提供的一个功能强大的插件,可以帮助开发者定位并优化SQL语句的性能问题。它可以记录执行的SQL语句、执行时间等信息,并将这些信息打印出来,以便开发者进行分析和优化。 使用性能分析插件很简单,只需要在配置文件中添加插件配置即可。以下是一个示例: ```java @Configuration public class MyBatisConfig { @Autowired private SqlSessionFactory sqlSessionFactory; @Bean public PerformanceInterceptor performanceInterceptor() { return new PerformanceInterceptor(); } @Bean public SqlSessionFactory sqlSessionFactory() throws Exception { MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean(); sessionFactory.setDataSource(dataSource); sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/*.xml")); sessionFactory.setPlugins(new Interceptor[]{performanceInterceptor()}); return sessionFactory.getObject(); } } ``` 在上述示例中,通过创建一个PerformanceInterceptor对象并将其添加到SqlSessionFactory中的插件列表中,即可启用性能分析插件。 启用性能分析插件后,每次执行SQL语句时,插件都会记录相关的性能信息,并将其打印出来。开发者可以根据这些信息来定位和优化潜在的性能问题。 需要注意的是,性能分析插件在生产环境中不建议长期启用,因为它会对系统性能产生一定的影响。一般情况下,可以在开发和调试阶段使用该插件,以帮助快速定位和解决性能问题。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值