Vous pouvez imprimer des instructions SQL en fonction du temps d'exécution.
La copie de code est la suivante:
package mybatis;
import java.text.dateformat;
Importer java.util.date;
Importer 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.maptstatement;
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.sse.Configuration;
Import org.apache.ibatis.Session.Resulthandler;
Import org.apache.ibatis.Session.RowBounds;
import org.apache.ibatis.type.TypeHandlerRegistry;
@Intercepts ({
@Signature (type = exécutor.class, méthode = "update", args = {mappedstatement.class, object.class}),
@Signature (type = exécutor.class, méthode = "query", args = {mappedstatement.class, object.class,
Rowbounds.class, resulthandler.class})})
classe publique MyBatisInterceptor implémente l'interceptor {
Propriétés privées Propriétés;
Interception d'objet public (invocation invocation) lance le jetable {
MaptedStatement mapsstatement = (mappedstatement) invocation.getArgs () [0];
Paramètre d'objet = null;
if (invocation.getArgs (). Longueur> 1) {
paramètre = invocation.getArgs () [1];
}
String sqlid = mappedStatement.getId ();
BOUNDSQL BOURNSQL = MAPPEDSATAMENT.GETBOUNDSQL (Paramètre);
Configuration Configuration = mappedStatement.getConfiguration ();
Objet returnValue = null;
Long start = System.CurrentTimemillis ();
returnValue = invocation.proceed ();
Long End = System.Currenttimemillis ();
long temps = (fin - démarrage);
if (time> 1) {
String sql = getSQL (configuration, boundsql, sqlid, time);
System.err.println (SQL);
}
retour returnValue;
}
Public Static String GetSQL (Configuration Configuration, BoundsQL BOURNSQL, 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 (temps);
str.append ("ms");
return str.toString ();
}
chaîne statique privée getParameTervalue (objet 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 ()) + "'";
} autre {
if (obj! = null) {
valeur = obj.toString ();
} autre {
value = "";
}
}
valeur de retour;
}
Public Static String showsql (configuration de configuration, BoundsQL BoundsQL) {
Object ParameterObject = boundsql.getParameterObject ();
List <paramètres de paramètres> Paramètres de paramètres = boundsql.getParameTermAppings ();
String sql = boundsql.getsql (). RempaceALL ("[// s] +", "");
if (ParameTermAppings.Size ()> 0 && ParameterObject! = null) {
TypeHandlerRegistry TypeHandlerRegistry = configuration.getTypeHandlerRegistry ();
if (typeHandlerRegistry.hastypeHandler (ParameterObject.getClass ())) {
sql = sql.replaceFirst ("//?", getParameTervalue (ParameterObject));
} autre {
MetaObject metaObject = configuration.newMetaObject (ParameterObject);
pour (paramètre paramétriques paramétriques: paramètres) {
String PropertyName = ParameTermapping.getProperty ();
if (metaObject.hasgeter (propriétéName)) {
Objet obj = metaObject.getValue (PropertyName);
sql = sql.replaceFirst ("//?", getParameTervalue (obj));
} else if (boundsql.hasadditionalParameter (PropertyName)) {
Objet obj = boundsql.getAdditionalParameter (PropertyName);
sql = sql.replaceFirst ("//?", getParameTervalue (obj));
}
}
}
}
return SQL;
}
Plugin d'objet public (cible de l'objet) {
return Plugin.Wrap (Target, This);
}
public void setProperties (Properties Properties0) {
this.properties = properties0;
}
}