Вы можете распечатать операторы SQL на основе времени выполнения.
Кода -копия выглядит следующим образом:
Пакет Mybatis;
импортировать java.text.dateformat;
импортировать java.util.date;
импортировать java.util.list;
импортировать java.util.locale;
импортировать java.util.properties;
импорт org.apache.ibatis.executor.executor;
Импорт org.apache.ibatis.mapping.boundsql;
Импорт org.apache.ibatis.mapping.mappedStatement;
Импорт org.apache.ibatis.mapping.parametermapping;
Импорт org.apache.ibatis.plugin.interceptor;
Импорт org.apache.ibatis.plugin.intercepts;
Импорт org.apache.ibatis.plugin.invocation;
Импорт org.apache.ibatis.plugin.plugin;
Импорт org.apache.ibatis.plugin.signature;
Импорт org.apache.ibatis.reflection.metaobject;
Импорт org.apache.ibatis.session.configuration;
Импорт org.apache.ibatis.session.resulthandler;
Импорт org.apache.ibatis.session.rowbounds;
Импорт org.apache.ibatis.type.typehandlerregistry;
@Intercepts ({
@Signature (type = executor.class, method = "update", args = {mapedstatement.class, object.class}),
@Signature (type = executor.class, method = "Query", args = {mapedstatement.class, object.class,
Rowbounds.class, resulthandler.class})})
открытый класс mybatisinterceptor реализует Interceptor {
частные объекты недвижимости;
Общедоступный объект перехват (вызов) бросает бросок {
MapedStatement MapedStatement = (MapedStatement) vocation.getArgs () [0];
Объект параметр = null;
if (upocation.getargs (). Length> 1) {
параметр = vocation.getargs () [1];
}
String sqlid = mapedStatement.getId ();
BoundsQl BoundsQl = mapedStatement.getBoundSql (параметр);
Конфигурация конфигурации = mapedStatement.getConfiguration ();
Объект returnValue = null;
long start = System.currentTimeMillis ();
returnValue = vocation.proceed ();
Long End = System.CurrentTimeMillis ();
давно = (End - Start);
if (время> 1) {
String sql = getsql (конфигурация, boundsql, sqlid, time);
System.err.println (SQL);
}
returnValue;
}
Общественная статическая строка getsql (конфигурация конфигурации, Boundsql Boundsql, String sqlid, долгое время) {
String sql = showql (configuration, boundsql);
StringBuilder Str = new StringBuilder (100);
str.append (sqlid);
str.append (":");
str.append (SQL);
str.append (":");
str.append (время);
str.append ("ms");
вернуть str.toString ();
}
частная статическая строка getParametervalue (Object obj) {
String value = null;
if (obj encessionof String) {
value = "'" + obj.tostring () + "'";
} else if (obj ancessionof date) {
Dateformat formatter = dateformat.getDateTimeInstance (dateformat.default, dateformat.default, locale.china);
value = "'' + formatter.format (new Date ()) +" '";
} еще {
if (obj! = null) {
value = obj.toString ();
} еще {
value = "";
}
}
возвращаемое значение;
}
Public Static String Showql (конфигурация конфигурации, BoundSQL Boundsql) {
Объект parameterObject = boundsql.getParameterObject ();
Список <parametermapping> parametermAppings = boundsql.getParameterMappings ();
String sql = boundsql.getsql (). Ryplaceall ("[// s]+", "");
if (parameterMappings.size ()> 0 && parameterObject! = null) {
TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry ();
if (typehandlerregistry.hastypehandler (parameterobject.getClass ())) {
sql = sql.replacefirst ("//?", getParametervalue (parameterObject));
} еще {
MetaObject metaObject = configuration.newMetaObject (parameterObject);
для (параметрирование параметрирования: параметры) {
String propertyname = parametermapping.getProperty ();
if (metaobject.hasgetter (PropertyName)) {
Объект obj = metaobject.getValue (PropertyName);
SQL = SQL.ReplaceFirst ("//?", GetParametervalue (OBJ));
} else if (boundsql.hasadditionalParameter (PropertyName)) {
Object obj = boundsql.getAdditionalParameter (PropertyName);
SQL = SQL.ReplaceFirst ("//?", GetParametervalue (OBJ));
}
}
}
}
вернуть SQL;
}
плагин публичного объекта (объект цели) {
return plugin.wrap (target, это);
}
public void setProperties (Properties Properties0) {
this.properties = Properties0;
}
}