java利用mybatis * 统计sql执行时间示例

时间:2021-08-22 11:35:41 

可以根据执行时间打印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;
 }
}

标签:java,mybatis, ,
0
投稿

猜你喜欢

  • mybatis使用foreach查询不出结果也不报错的问题

    2023-11-24 22:36:17
  • Mybatis通过数据库表自动生成实体类和xml映射文件

    2022-01-11 07:05:46
  • Spring ApplicationListener监听器用法详解

    2022-08-21 00:00:08
  • mybatis foreach 循环 list(map)实例

    2023-11-23 23:39:05
  • 学习Java HashMap,看这篇就够了

    2023-11-11 11:18:56
  • java ThreadPoolExecutor线程池拒绝策略避坑

    2021-09-05 08:39:52
  • Java Controller实现参数验证与统一异常处理流程详细讲解

    2022-01-25 18:49:47
  • 详解Java变量与常量

    2023-11-09 21:00:14
  • 详解SpringCloud Zuul过滤器返回值拦截

    2023-02-05 07:59:37
  • IDEA解决springboot热部署失效问题(推荐)

    2023-08-12 10:40:49
  • 深入理解MyBatis中的一级缓存与二级缓存

    2022-05-25 09:41:34
  • java实现将结果集封装到List中的方法

    2021-10-27 22:29:45
  • Java基于享元模式实现五子棋游戏功能实例详解

    2023-07-23 16:20:18
  • Java进程cpu占用过高问题解决

    2021-08-09 00:16:59
  • Java语言读取配置文件config.properties的方法讲解

    2023-09-29 14:45:51
  • 详解Maven settings.xml配置(指定本地仓库、阿里云镜像设置)

    2022-04-09 23:45:14
  • 解析Java内存分配和回收策略以及MinorGC、MajorGC、FullGC

    2023-02-06 08:22:19
  • JAVA演示阿里云图像识别API,印刷文字识别-营业执照识别

    2022-04-21 15:05:22
  • C# 如何调用SAP RFC

    2023-07-02 18:11:42
  • SpringBoot使用Kaptcha实现验证码的生成与验证功能

    2022-11-30 15:57:44
  • asp之家 软件编程 m.aspxhome.com