Anda dapat mencetak pernyataan SQL berdasarkan waktu eksekusi.
Salinan kode adalah sebagai berikut:
paket mybatis;
impor java.text.dateFormat;
impor java.util.date;
impor java.util.list;
impor java.util.locale;
impor java.util.properties;
impor org.apache.iatis.executor.executor;
impor org.apache.ibatis.mapping.boundsql;
impor org.apache.ibatis.mapping.mappedStatement;
impor org.apache.ibatis.mapping.parametermapping;
impor org.apache.ibatis.plugin.interceptor;
impor org.apache.ibatis.plugin.intercepts;
impor org.apache.iatis.plugin.invocation;
impor org.apache.ibatis.plugin.plugin;
impor org.apache.iatis.plugin.signature;
impor org.apache.ibatis.reflection.metaObject;
impor org.apache.ibatis.Session.configuration;
Impor org.apache.Ibatis.Session.ResulThandler;
impor org.apache.ibatis.Session.rowbounds;
Impor org.apache.ibatis.type.typehandlerregistry;
@Intercepts ({
@Signature (type = executor.class, method = "update", args = {mappedstatement.class, objek.class}),
@Signature (type = executor.class, method = "query", args = {mappedstatement.class, objek.class,
Rowbounds.class, resulthandler.class})})
kelas publik mybatisInterceptor mengimplementasikan interceptor {
Properti Pribadi Properties;
Intercept Objek Publik (Doa Doa) Melemparkan Throwble {
MappedStatement MappedStatement = (MappedStatement) Invocation.getArgs () [0];
Parameter objek = null;
if (Invocation.getArgs (). Panjang> 1) {
parameter = Invocation.getArgs () [1];
}
String sqlid = mappedstatement.getId ();
BoundSQL BoundSQL = MappedStatement.getBoundSQL (parameter);
Konfigurasi konfigurasi = mappedstatement.getConfiguration ();
Objek returnValue = null;
Long Start = System.CurrentTimeMillis ();
returnValue = Invocation.proed ();
long end = system.currentTimemillis ();
lama = (end - start);
if (waktu> 1) {
String sql = getSql (konfigurasi, batas, sqlid, waktu);
System.err.println (SQL);
}
Return ReturnValue;
}
Public Static String GetSQL (Konfigurasi Konfigurasi, BoundSQL BoundSQL, String SQLID, Long Time) {
String sql = showql (konfigurasi, boundsql);
StringBuilder str = New StringBuilder (100);
Str.Append (SQLID);
str.Append (":");
Str.Append (SQL);
str.Append (":");
str.Append (waktu);
str.Append ("ms");
return str.tostring ();
}
string statis pribadi getParameterValue (objek obj) {
Nilai string = null;
if (instance Obj dari string) {
value = "'" + obj.tostring () + "'";
} lain jika (obj instance dari tanggal) {
DateFormatter formatter = dateFormat.getDateTimeInstance (dateFormat.default, dateFormat.default, locale.china);
value = "'" + formatter.format (new date ()) + "'";
} kalau tidak {
if (obj! = null) {
nilai = obj.tostring ();
} kalau tidak {
nilai = "";
}
}
nilai pengembalian;
}
Public Static String ShowQL (Konfigurasi Konfigurasi, BoundSQL BoundSQL) {
Objek parameterObject = boundsql.getParameterObject ();
Daftar <Parametermapping> parameterMappings = BoundSQL.GetParametermappings ();
String sql = boundsql.getsql (). Replaceall ("[// s]+", "");
if (parametermappings.size ()> 0 && parameterObject! = null) {
TypeHandlerRegistry typeHandlerRegistry = configuration.gettypehandlerregistry ();
if (typeHandlegregistry.hastypehandler (parameterobject.getClass ()) {
sql = sql.replacefirst ("//?", getParametervalue (parameterObject));
} kalau tidak {
MetaObject metaObject = configuration.newmetaObject (parameterObject);
untuk (parametermapping parameterMapping: parameterMappings) {
String propertyname = parametermapping.getProperty ();
if (metaObject.hasgetter (propertyname)) {
Objek obj = metaObject.getValue (propertiyname);
sql = sql.replacefirst ("//?", getParametervalue (obj));
} lain if (boundsql.hasadditionalparameter (propertieName)) {
Objek obj = boundsql.getAdditionalparameter (propertiyname);
sql = sql.replacefirst ("//?", getParametervalue (obj));
}
}
}
}
mengembalikan SQL;
}
plugin objek publik (target objek) {
return plugin.wrap (target, ini);
}
public void setProperties (properties properties0) {
this.properties = properties0;
}
}