Você pode imprimir instruções SQL com base no tempo de execução.
A cópia do código é a seguinte:
pacote mybatis;
importar java.text.dateFormat;
importar java.util.date;
importar java.util.list;
importar java.util.locale;
importar 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.intercept;
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 = executor.class, method = "update", args = {mapedstatement.class, object.class}),
@Signature (type = executor.class, métod = "query", args = {mapedstatement.class, object.class,
Rowbounds.class, resultHandler.class})})
classe pública mybatisintercept implementa interceptador {
Propriedades de propriedades privadas;
Public Object Intercept (Invocation Invocation) lança jogável {
MAPPEDSTATEMENT MAPPEDSTATEMENT = (MAPPEDSTATEMENT) Invocation.getargs () [0];
Parâmetro do objeto = null;
if (Invocation.GeTargs (). Comprimento> 1) {
parâmetro = invocação.getargs () [1];
}
String sqlid = mapedstatement.getId ();
Boundsql Boundsql = MappEdStatement.GetBoundSql (parâmetro);
Configuração da configuração = mapedstatement.getConfiguration ();
Objeto retornValue = null;
long start = system.currenttimemillis ();
returnValue = Invocation.proeced ();
Long end = System.currenttimemillis ();
longo tempo = (final - start);
if (tempo> 1) {
String sql = getsql (configuração, boundsql, sqlid, tempo);
System.err.println (SQL);
}
return retornValue;
}
public static string getsql (configuração de configuração, boundsql boundsql, string sqlid, longo tempo) {
String sql = showql (configuração, boundsql);
StringBuilder str = new StringBuilder (100);
str.Append (sqlid);
str.Append (":");
str.Append (SQL);
str.Append (":");
str.Append (tempo);
str.Append ("MS");
return str.ToString ();
}
String estática privada getParameterValue (objeto 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 ()) + "'";
} outro {
if (obj! = null) {
valor = obj.toString ();
} outro {
value = "";
}
}
valor de retorno;
}
public Static String Showsql (configuração de configuração, Boundsql Boundsql) {
Objeto parameterObject = boundSql.getParameToBject ();
Lista <MeameterMapping> 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));
} outro {
MetaObject metaObject = configuration.newMetaObject (parameterObject);
para (parametermapping parametermapping: parametermappings) {
String PropertyName = ParameTerMapping.getProperty ();
if (metaobject.hasgetter (PropertyName)) {
Objeto obj = metaObject.getValue (PropertyName);
sql = sql.replacefirst ("//?", getParameterValue (obj));
} else if (boundsql.hasadditionalparameter (PropertyName)) {
Objeto obj = boundsql.getAdditionCarameter (PropertyName);
sql = sql.replacefirst ("//?", getParameterValue (obj));
}
}
}
}
retornar SQL;
}
Plugin de objeto público (destino do objeto) {
Return Plugin.wrap (Target, este);
}
public void setProperties (Properties Properties0) {
this.properties = Properties0;
}
}