Sie können SQL -Anweisungen basierend auf Ausführungszeit drucken.
Die Codekopie lautet wie folgt:
Paket mybatis;
Java.Text.DateFormat importieren;
import Java.util.date;
importieren 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.mappingStatement;
import org.apache.ibatis.mapping.parametermaping;
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.resaulthandler;
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})})
öffentliche Klasse MyBatiSTerceptor implementiert Interceptor {
private Eigenschaften Eigenschaften;
öffentliches Objekt Intercept (Invocation Invocation) wirft Throwable {
MADCPDSTATEMENT MADCKDSTATEMENT = (MADCKADDSTATEMENT) -Revocation.getArgs () [0];
Objektparameter = null;
if (invocation.getargs (). Länge> 1) {
Parameter = Invocation.getargs () [1];
}
String SQLID = MADPDStatement.getId ();
BoundSQL boundSQL = MADPDStatement.getBoundSQL (Parameter);
Configuration configuration = mapPedStatement.getConfiguration ();
Objekt returnValue = null;
Long start = system.currentTimemillis ();
returnValue = invocation.Procece ();
Long End = System.currentTimemillis ();
lange Zeit = (Ende - Start);
if (Zeit> 1) {
String sql = getQl (Konfiguration, boundSQL, SQLID, Zeit);
System.err.println (SQL);
}
return returnValue;
}
public static String getQl (Konfigurationskonfiguration, boundSQL boundSQL, String SQLID, Langzeit) {
String SQL = shosql (Konfiguration, boundSQL);
StringBuilder Str = New StringBuilder (100);
Str.Append (SQLID);
Str.Append (":");
Str.Append (SQL);
Str.Append (":");
Str.Append (Zeit);
Str.Append ("MS");
return Str.ToString ();
}
private statische Zeichenfolge GetParameterValue (Objekt obj) {
String value = null;
if (obj instanceof string) {
value = "'" + obj.toString () + "'";
} else if (obj instanceof Datum) {
DateFormat Formatter = DateFormat.getDatetimeInstance (DateFormat.Default, DateFormat.Default, Locale.china);
value = "'" + formatter.format (neues Datum ()) + "'";
} anders {
if (obj! = null) {
value = obj.toString ();
} anders {
value = "";
}
}
Rückgabewert;
}
public static String zeigt QL (Konfigurationskonfiguration, boundSQL boundSQL) {
Object ParameterObject = bodernsql.getParameterObject ();
Liste <PARAMETERMAPPING> ParameterMappings = bodernSQL.getParametermappings ();
String sql = bodensql.getSql (). Ersatz ("[// s]+", "");
if (parameterMappings.size ()> 0 && parameterObject! = null) {
TypeHandlerregistry typeHandlerregistry = configuration.gettypeHandlerregistry ();
if (typeHandlerregistry.hastypeHandler (parameterObject.getClass ())) {
SQL = SQL.ReplaceFirst ("//?", getParametervalue (parameterObject));
} anders {
MetaObject metaObject = configuration.newmetaObject (parameterObject);
für (Parametermapping Parametermapping: Parametermappings) {{
String PropertyName = parametermaping.getProperty ();
if (metaObject.hasgertter (PropertyName)) {
Objekt obj = metaObject.getValue (Eigenschaftsname);
SQL = SQL.ReplaceFirst ("//?", GetParametervalue (obj));
} else if (boundsql.hasadditionalParameter (PropertyName)) {
Object OBJ = boundsql.getAdDitionalParameter (PropertyName);
SQL = SQL.ReplaceFirst ("//?", GetParametervalue (obj));
}
}
}
}
Return SQL;
}
öffentliches Objekt -Plugin (Objektziel) {
return plugin.wrap (target, this);
}
public void setProperties (Eigenschaften Eigenschaften0) {{
this.Properties = Properties0;
}
}