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