mybatis plus sql性能分析插件

github地址https://github.com/heng1234/mybatis_plus


基于 https://blog.csdn.net/qq_39313596/article/details/100862049 文章

建议性能分析插件只在开发环境和测试环境开启 有性能损耗

在MybatisPlusConfig 加入sql性能分析插件

 

一、 mybatis-plus自带的性能分析

 /**
     * SQL执行效率插件 性能分析插件
     */
    @Bean
    @Profile({"dev","test"})// 设置 dev test 环境开启
    public PerformanceInterceptor performanceInterceptor() {
        PerformanceInterceptor performanceInterceptor =  new PerformanceInterceptor();
        performanceInterceptor.setFormat(true);//格式化语句
        //performanceInterceptor.setMaxTime(5);//执行时间超过多少秒会抛出异常
        return  performanceInterceptor;
    }

执行sql打印

/**
     * 插入一行user
     * 执行时间50毫秒
     * Time:50 ms - ID:com.hlvy.mybatis_plus.mapper.UserMapper.selectById
     * Execute SQL:SELECT id,name,age,email,manager_id,created_time,update_time,version FROM User WHERE id=5 AND deleted=0
     */
    @Test
    public void insertUser() {
        System.out.println(("----- insertUser method test ------"));
        User user = new User();
        user.setName("恒果果");
        user.setAge(19);
        user.setEmail("xxx@qq.com");
        int row = userMapper.insert(user);
        System.out.println("插入成功"+row+"行");
    }

二、p6spy性能分析插件 该功能依赖 p6spy 组件,完美的输出打印 SQL 及执行时长 3.1.0 以上版本

 

  • p6spy 依赖引入

Maven:

        <!--p6spy 依赖引入-->
        <dependency>
            <groupId>p6spy</groupId>
            <artifactId>p6spy</artifactId>
            <version>3.8.2</version>
        </dependency>
        <dependency>

 

application.yml 配置修改

  #sql分析插件需要切换url及驱动具体看mybatis-plus官网
    url: jdbc:p6spy:mysql://127.0.0.1:3306/mytest?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT

    driver-class-name: com.p6spy.engine.spy.P6SpyDriver
server:
  port: 9091
spring:
  datasource:
    #url: jdbc:mysql://127.0.0.1:3306/mytest?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT
   #sql分析插件需要切换url及驱动具体看mybatis-plus官网
    url: jdbc:p6spy:mysql://127.0.0.1:3306/mytest?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT
    username: root
    password: 123456
   # driver-class-name: com.mysql.cj.jdbc.Driver
    driver-class-name: com.p6spy.engine.spy.P6SpyDriver
mybatis-plus:
  #MyBaits 别名包扫描路径,通过该属性可以给包中的类注册别名,注册后在 Mapper 对应的 XML 文件中可以直接使用类名,而不用使用全限定的类名(即 XML 中调用的时候不用包含包名)。
  type-aliases-package: com.hlvy.mybatis_plus.entity
  #该配置请和 typeAliasesPackage 一起使用,如果配置了该属性,则仅仅会扫描路径下以该类作为父类的域对象
  type-aliases-super-type: java.lang.Object
  #MyBatis Mapper 所对应的 XML 文件位置,如果您在 Mapper 中有自定义方法(XML 中有自定义实现),需要进行该配置,告诉 Mapper 所对应的 XML 文件位置。
  mapper-locations: classpath*:mybatis/*.xml
  #MyBatis 配置文件位置,如果您有单独的 MyBatis 配置,请将其路径配置到 configLocation 中。
  #config-location: classpath:mybatis-config.xml
  #启动时是否检查 MyBatis XML 文件的存在,默认不检查
  check-config-location: false
  global-config:
    db-config: #配置逻辑删除
      logic-delete-value: 1 # 逻辑已删除值(默认为 1)
      logic-not-delete-value: 0 # 逻辑未删除值(默认为 0)
      #id-type: none #全局策略
      #field-strategy: ignored #所有字段加入crud语句中 一般不这么设置 默认not_null
      #table-prefix: t_ #表的前缀
      #table-underline: true #表名是否使用下划线
    #configuration:#不能与config-location同时出现
    #map-underscore-to-camel-case: true
#设置日志 级别
logging:
  level:
    root: warn
    com.hlvy.mybatis_plus: trace
  pattern:
    console: '%P%m%n'

resource下的spy.properties 配置:

module.log=com.p6spy.engine.logging.P6LogFactory,com.p6spy.engine.outage.P6OutageFactory
# 自定义日志打印
logMessageFormat=com.baomidou.mybatisplus.extension.p6spy.P6SpyLogger
#日志输出到控制台
#appender=com.baomidou.mybatisplus.extension.p6spy.StdoutLogger
# 使用日志系统记录 sql
#appender=com.p6spy.engine.spy.appender.Slf4JLogger
# 设置 p6spy driver 代理
deregisterdrivers=true
# 取消JDBC URL前缀
useprefix=true
# 配置记录 Log 例外,可去掉的结果集有error,info,batch,debug,statement,commit,rollback,result,resultset.
excludecategories=info,debug,result,batch,resultset
# 日期格式
dateformat=yyyy-MM-dd HH:mm:ss
# 实际驱动可多个
#driverlist=org.h2.Driver
# 是否开启慢SQL记录
outagedetection=true
# 慢SQL记录标准 2 秒
outagedetectioninterval=2

#输出到文件中需要把输出到控制台注释掉
logfile=log.log

测试运行

  /**
     * 通用service
     * 查询所有user
     * 下面是性能分析插件打印的
     *  Consume Time:16 ms 2019-09-16 20:32:23
     *  Execute SQL:SELECT id,name,age,email,manager_id,created_time,update_time,version FROM User WHERE id=5 AND deleted=0
     */
    @Test
    public void testSelect() {
        User user = userService.getById(5);
        System.out.println(user.toString());
    }

 

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值