java利用mybatis拦截器统计sql执行时间示例

 更新时间:2014年03月23日 09:30:24   作者:  
这篇文章主要介绍了java利用mybatis拦截器统计sql执行时间示例,该拦截器拦截mybatis的query和update操作,能统计sql执行时间

可以根据执行时间打印sql语句,打印的sql语句是带参数的,可以拷贝到查询分析器什么的直接运行

复制代码 代码如下:

package mybatis;

import java.text.DateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Properties;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.ParameterMapping;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.type.TypeHandlerRegistry;

@Intercepts({
  @Signature(type = Executor.class, method = "update", args = { MappedStatement.class, Object.class }),
  @Signature(type = Executor.class, method = "query", args = { MappedStatement.class, Object.class,
    RowBounds.class, ResultHandler.class }) })
public class MybatisInterceptor implements Interceptor {

 private Properties properties;

 public Object intercept(Invocation invocation) throws Throwable {
  MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
  Object parameter = null;
  if (invocation.getArgs().length > 1) {
   parameter = invocation.getArgs()[1];
  }
  String sqlId = mappedStatement.getId();
  BoundSql boundSql = mappedStatement.getBoundSql(parameter);
  Configuration configuration = mappedStatement.getConfiguration();
  Object returnValue = null;
  long start = System.currentTimeMillis();
  returnValue = invocation.proceed();
  long end = System.currentTimeMillis();
  long time = (end - start);
  if (time > 1) {
   String sql = getSql(configuration, boundSql, sqlId, time);
   System.err.println(sql);
  }
  return returnValue;
 }

 public static String getSql(Configuration configuration, BoundSql boundSql, String sqlId, long time) {
  String sql = showSql(configuration, boundSql);
  StringBuilder str = new StringBuilder(100);
  str.append(sqlId);
  str.append(":");
  str.append(sql);
  str.append(":");
  str.append(time);
  str.append("ms");
  return str.toString();
 }

 private static String getParameterValue(Object obj) {
  String value = null;
  if (obj instanceof String) {
   value = "'" + obj.toString() + "'";
  } else if (obj instanceof Date) {
   DateFormat formatter = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.CHINA);
   value = "'" + formatter.format(new Date()) + "'";
  } else {
   if (obj != null) {
    value = obj.toString();
   } else {
    value = "";
   }

  }
  return value;
 }

 public static String showSql(Configuration configuration, BoundSql boundSql) {
  Object parameterObject = boundSql.getParameterObject();
  List<ParameterMapping> parameterMappings = boundSql.getParameterMappings();
  String sql = boundSql.getSql().replaceAll("[\\s]+", " ");
  if (parameterMappings.size() > 0 && parameterObject != null) {
   TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
   if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) {
    sql = sql.replaceFirst("\\?", getParameterValue(parameterObject));

   } else {
    MetaObject metaObject = configuration.newMetaObject(parameterObject);
    for (ParameterMapping parameterMapping : parameterMappings) {
     String propertyName = parameterMapping.getProperty();
     if (metaObject.hasGetter(propertyName)) {
      Object obj = metaObject.getValue(propertyName);
      sql = sql.replaceFirst("\\?", getParameterValue(obj));
     } else if (boundSql.hasAdditionalParameter(propertyName)) {
      Object obj = boundSql.getAdditionalParameter(propertyName);
      sql = sql.replaceFirst("\\?", getParameterValue(obj));
     }
    }
   }
  }
  return sql;
 }

 public Object plugin(Object target) {
  return Plugin.wrap(target, this);
 }

 public void setProperties(Properties properties0) {
  this.properties = properties0;
 }
}

相关文章

  • SpringBoot工程下Lombok的应用教程详解

    SpringBoot工程下Lombok的应用教程详解

    这篇文章主要给大家介绍了关于SpringBoot工程下Lombok应用的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11
  • SpringBoot定时任务详解与案例代码

    SpringBoot定时任务详解与案例代码

    SpringBoot是一个流行的Java开发框架,它提供了许多便捷的特性来简化开发过程,其中之一就是定时任务的支持,让开发人员可以轻松地在应用程序中执行定时任务,本文将详细介绍如何在Spring Boot中使用定时任务,并提供相关的代码示例
    2023-06-06
  • springboot项目以jar包运行的操作方法

    springboot项目以jar包运行的操作方法

    公司一个springboot项目本来是打war包的,突然要改为打jar包,不知所措了,纠结该如何操作呢,折腾半天终于搞定了,下面把解决方案分享给大家,对springboot打jar包方式感兴趣的朋友一起看看吧
    2021-06-06
  • springboot使用nacos的示例详解

    springboot使用nacos的示例详解

    这篇文章主要介绍了springboot使用nacos的示例代码,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-12-12
  • SpringBoot使用Prometheus实现监控

    SpringBoot使用Prometheus实现监控

    在当今的软件开发世界中,监控是至关重要的一部分,本文主要介绍了如何在Spring Boot应用程序中使用Prometheus进行监控,以帮助大家更好地理解和管理您的应用程序,有需要的可以参考下
    2023-10-10
  • spring-Kafka中的@KafkaListener深入源码解读

    spring-Kafka中的@KafkaListener深入源码解读

    本文主要通过深入了解源码,梳理从spring启动到真正监听kafka消息的这套流程,从spring启动开始处理@KafkaListener,本文结合实例流程图给大家讲解的非常详细,需要的朋友参考下
    2023-02-02
  • Java设置httponly cookie的实现示例

    Java设置httponly cookie的实现示例

    本文主要介绍了Java设置httponly cookie的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-08-08
  • Java int类型如何获取高低位

    Java int类型如何获取高低位

    这篇文章主要介绍了Java int类型如何获取高低位,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01
  • Spring Cloud Gateway 如何修改HTTP响应信息

    Spring Cloud Gateway 如何修改HTTP响应信息

    这篇文章主要介绍了Spring Cloud Gateway 修改HTTP响应信息的方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-07-07
  • Java异步编程Future应用方式

    Java异步编程Future应用方式

    Java中的Future接口用于构建复杂并行操作,它允许异步执行任务,并在需要时获取结果,通过Future接口,可以避免多线程编程中的一些常见问题,如线程执行顺序和结果获取的复杂性,然而,在使用Future时需要注意,并行执行可能会变为串行执行,特别是在使用get()方法时
    2025-02-02

最新评论