Puede imprimir declaraciones SQL basadas en el tiempo de ejecución.
La copia del código es la siguiente:
paquete mybatis;
import java.text.dateformat;
import java.util.date;
import java.util.list;
import java.util.locale;
import java.util.properties;
importar org.apache.ibatis.executor.executor;
importar org.apache.ibatis.mapping.boundsql;
importar org.apache.ibatis.mapping.MappedStatement;
importar org.apache.ibatis.mapping.parametermapping;
importar org.apache.ibatis.plugin.interceptor;
importar org.apache.ibatis.plugin.Intercepts;
importar org.apache.ibatis.plugin.invocation;
importar org.apache.ibatis.plugin.plugin;
importar org.apache.ibatis.plugin.signature;
importar org.apache.ibatis.REFLECTION.MetaObject;
importar org.apache.ibatis.session.configuration;
importar org.apache.ibatis.session.resulthandler;
importar org.apache.ibatis.session.rowbounds;
importar org.apache.ibatis.Type.TypeHandlerRegistry;
@Intercepts ({
@Signature (type = ejecutor.class, método = "update", args = {mappedStatement.class, object.class}),
@Signature (type = ejecutor.class, método = "Query", args = {mappedStatement.class, object.class,
RowBounds.class, Resulthandler.class})})
clase pública mybatisinterceptor implementa interceptor {
Propiedades de propiedades privadas;
Interception Public Object (Invocation Invocation) lanza lanzamiento {
MappedStatement MappedStatement = (MappedStatement) Invocation.getArgs () [0];
Parámetro de objeto = nulo;
if (invocation.getArgs (). Longitud> 1) {
parámetro = invocación.getArgs () [1];
}
Cadena sqlid = mappedStatement.getId ();
BoundSQL BoundSQL = MappedStatement.getBoundSQL (parámetro);
Configuración configuración = mappedStatement.getConfiguration ();
Objeto returnValue = null;
Long Start = System.CurrentTimemillis ();
returnValue = invocation.proced ();
Long End = System.CurrentTimemillis ();
mucho tiempo = (final - inicio);
if (tiempo> 1) {
Cadena sql = getsql (configuración, boundsql, sqlid, tiempo);
System.err.println (SQL);
}
return returnValue;
}
Cadena publicitaria pública getsql (configuración de configuración, boundsql boundsql, string sqlid, mucho tiempo) {
Cadena sql = showsql (configuración, boundsql);
StringBuilder str = new StringBuilder (100);
str.append (sqlid);
str.append (":");
str.append (SQL);
str.append (":");
str.append (tiempo);
str.append ("ms");
return str.ToString ();
}
cadena estática privada getParametervalue (object obj) {
Valor de cadena = nulo;
if (obj instanciaf string) {
valor = "'" + obj.ToString () + "'";
} else if (obj instanciaf fecha) {
DateFormat formatter = dateFormat.getDateTimeInstance (dateFormat.default, dateFormat.default, locale.china);
valor = "'" + formatter.format (new Date ()) + "'";
} demás {
if (obj! = null) {
valor = obj.ToString ();
} demás {
valor = "";
}
}
valor de retorno;
}
Cadena estática pública Showsql (Configuración de configuración, BoundSQL BoundSQL) {
Objeto parametREAnt = boundSql.getParametRoCject ();
Lista <amametermapping> parametermappings = boundsql.getParametermappings ();
Cadena sql = boundsql.getSql (). ReplaceAll ("[// s]+", "");
if (parametermappings.size ()> 0 && parameteroBject! = null) {
TypeHandlerRegistry typeHandlerRegistry = Configuration.gettypeHandlerRegistry ();
if (typeHandlerRegistry.hastypeHandler (parametREAnt.getClass ())) {
sql = sql.replaceFirst ("//?", getParametervalue (parametREAnt));
} demás {
MetaObject metaObject = Configuration.newMetaObject (parametREAnt);
para (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 (proppertyname)) {
Object obj = boundsql.getAdditionAlParameter (PropertyName);
sql = sql.replaceFirst ("//?", getParametervalue (obj));
}
}
}
}
return sql;
}
Public Object Plugin (Object Target) {
return plugin.wrap (objetivo, este);
}
public void setProperties (Properties Properties0) {
this.properties = Properties0;
}
}