Java uses MyBatis interceptor to count SQL execution time samples _java

Source: Internet
Author: User

You can print SQL statements based on the execution time, and the printed SQL statements are parameterized and can be copied to the Query Analyzer what's directly running

Copy Code code as follows:

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.