인쇄 된 SQL 문은 매개 변수이며 직접 실행할 수 있습니다.
코드 사본은 다음과 같습니다.
패키지 mybatis;
import java.text.dateformat;
import java.util.date;
Java.util.list 가져 오기;
java.util.locale import;
java.util.properties import;
import org.apache.ibatis.executor.executor;
import org.apache.ibatis.mapping.boundsql;
import org.apache.ibatis.mapping.mappedstatement;
import org.apache.ibatis.mapping.parametermpapping;
org.apache.ibatis.plugin.interceptor import;
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.session.configuration;
import org.apache.ibatis.session.resulthandler;
import org.apache.ibatis.session.rowbounds;
import org.apache.ibatis.type.typehandlerregistry;
@intercepts ({
@Signature (type = executor.class, method = "update", args = {mappedstatement.class, object.class}),
@signature (type = executor.class, method = "query", args = {mappedstatement.class, object.class,
rowbounds.class, resulthandler.class})}))
공개 클래스 MyBatisinterceptor는 인터셉터를 구현합니다.
개인 속성 속성;
공개 물체 인터셉트 (호출) 던지기 가능 {
MappedStatement MAPPEDSTATEMENT = (MAPPEDSTATEMENT) invocation.getArgs () [0];
객체 매개 변수 = null;
if (invocation.getArgs (). 길이> 1) {
매개 변수 = invocation.getargs () [1];
}
문자열 sqlid = mappedStatement.getId ();
BONDSQL BONDSQL = MAPPEDSTATEMENT.GETBOUNDSQL (매개 변수);
구성 구성 = MappedStatement.getConfiguration ();
객체 returnValue = null;
Long Start = System.CurrentTimeMillis ();
returnValue = invocation.proceed ();
Long End = System.CurrentTimeMillis ();
긴 시간 = (종료 - 시작);
if (time> 1) {
문자열 sql = getSql (configuration, boundsql, sqlid, time);
System.err.println (SQL);
}
return returnValue;
}
public static string getSql (구성 구성, boundsql boundsql, String sqlid, 오랜 시간) {
문자열 sql = showql (configuration, boundsql);
StringBuilder str = new StringBuilder (100);
str.append (sqlid);
str.append ( ":");
str.append (SQL);
str.append ( ":");
str.append (시간);
str.append ( "MS");
return str.toString ();
}
개인 정적 문자열 getParameterValue (Object obj) {
문자열 값 = 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 ()) + "'";
} 또 다른 {
if (obj! = null) {
값 = obj.tostring ();
} 또 다른 {
value = "";
}
}
반환 값;
}
public static string showql (구성 구성, boundsql boundsql) {
Object ParameterObject = boundsql.getParameterObject ();
List <ParametErmpaping> ParametErmappings = boundsql.getParameterMappings ();
문자열 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));
} 또 다른 {
metaobject metaobject = configuration.newmetaobject (parameterObject);
for (parametermpapping parametermpaping : parameterMappings) {
문자열 propertyname = parameterMpapp.GetProperty ();
if (metaobject.hasgetter (propertyname)) {
Object 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, this);
}
public void setProperties (Properties Properties0) {
this.properties = properties0;
}
}