最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

java利用mybatis攔截器統(tǒng)計sql執(zhí)行時間示例

 更新時間:2014年03月23日 09:30:24   作者:  
這篇文章主要介紹了java利用mybatis攔截器統(tǒng)計sql執(zhí)行時間示例,該攔截器攔截mybatis的query和update操作,能統(tǒng)計sql執(zhí)行時間

可以根據(jù)執(zhí)行時間打印sql語句,打印的sql語句是帶參數(shù)的,可以拷貝到查詢分析器什么的直接運行

復(fù)制代碼 代碼如下:

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;
 }
}

相關(guān)文章

  • SpringBoot工程下Lombok的應(yīng)用教程詳解

    SpringBoot工程下Lombok的應(yīng)用教程詳解

    這篇文章主要給大家介紹了關(guān)于SpringBoot工程下Lombok應(yīng)用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • SpringBoot定時任務(wù)詳解與案例代碼

    SpringBoot定時任務(wù)詳解與案例代碼

    SpringBoot是一個流行的Java開發(fā)框架,它提供了許多便捷的特性來簡化開發(fā)過程,其中之一就是定時任務(wù)的支持,讓開發(fā)人員可以輕松地在應(yīng)用程序中執(zhí)行定時任務(wù),本文將詳細(xì)介紹如何在Spring?Boot中使用定時任務(wù),并提供相關(guān)的代碼示例
    2023-06-06
  • springboot項目以jar包運行的操作方法

    springboot項目以jar包運行的操作方法

    公司一個springboot項目本來是打war包的,突然要改為打jar包,不知所措了,糾結(jié)該如何操作呢,折騰半天終于搞定了,下面把解決方案分享給大家,對springboot打jar包方式感興趣的朋友一起看看吧
    2021-06-06
  • springboot使用nacos的示例詳解

    springboot使用nacos的示例詳解

    這篇文章主要介紹了springboot使用nacos的示例代碼,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-12-12
  • SpringBoot使用Prometheus實現(xiàn)監(jiān)控

    SpringBoot使用Prometheus實現(xiàn)監(jiān)控

    在當(dāng)今的軟件開發(fā)世界中,監(jiān)控是至關(guān)重要的一部分,本文主要介紹了如何在Spring Boot應(yīng)用程序中使用Prometheus進行監(jiān)控,以幫助大家更好地理解和管理您的應(yīng)用程序,有需要的可以參考下
    2023-10-10
  • spring-Kafka中的@KafkaListener深入源碼解讀

    spring-Kafka中的@KafkaListener深入源碼解讀

    本文主要通過深入了解源碼,梳理從spring啟動到真正監(jiān)聽kafka消息的這套流程,從spring啟動開始處理@KafkaListener,本文結(jié)合實例流程圖給大家講解的非常詳細(xì),需要的朋友參考下
    2023-02-02
  • Java設(shè)置httponly?cookie的實現(xiàn)示例

    Java設(shè)置httponly?cookie的實現(xiàn)示例

    本文主要介紹了Java設(shè)置httponly?cookie的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • Java?int類型如何獲取高低位

    Java?int類型如何獲取高低位

    這篇文章主要介紹了Java?int類型如何獲取高低位,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • Spring Cloud Gateway 如何修改HTTP響應(yīng)信息

    Spring Cloud Gateway 如何修改HTTP響應(yīng)信息

    這篇文章主要介紹了Spring Cloud Gateway 修改HTTP響應(yīng)信息的方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Java異步編程Future應(yīng)用方式

    Java異步編程Future應(yīng)用方式

    Java中的Future接口用于構(gòu)建復(fù)雜并行操作,它允許異步執(zhí)行任務(wù),并在需要時獲取結(jié)果,通過Future接口,可以避免多線程編程中的一些常見問題,如線程執(zhí)行順序和結(jié)果獲取的復(fù)雜性,然而,在使用Future時需要注意,并行執(zhí)行可能會變?yōu)榇袌?zhí)行,特別是在使用get()方法時
    2025-02-02

最新評論

扶绥县| 乌拉特后旗| 玉龙| 彰化县| 同德县| 象州县| 汉中市| 徐闻县| 井研县| 灯塔市| 太湖县| 福清市| 宜昌市| 体育| 金门县| 封丘县| 天柱县| 洛川县| 张北县| 称多县| 大连市| 秀山| 牟定县| 广水市| 阿拉善右旗| 辽源市| 类乌齐县| 谢通门县| 巴东县| 旌德县| 香港| 刚察县| 星子县| 绥芬河市| 永靖县| 郧西县| 阿勒泰市| 无棣县| 木里| 建湖县| 札达县|