実行時間に基づいてSQLステートメントを印刷でき、クエリアナライザーにコピーして直接実行できます。
コードコピーは次のとおりです。
MyBatisをパッケージします。
java.text.dateformatをインポートします。
Import Java.util.date;
java.util.listをインポートします。
java.util.localeをインポートします。
java.util.propertiesをインポートします。
org.apache.ibatis.executor.executorをインポートします。
Import org.apache.ibatis.mapping.boundsql;
Import org.apache.ibatis.mapping.mappedStatement;
org.apache.ibatis.mapping.parametermappingをインポートします。
org.apache.ibatis.plugin.interceptorをインポートします。
org.apache.ibatis.plugin.interceptsをインポートします。
Import org.apache.ibatis.plugin.invocation;
org.apache.ibatis.plugin.pluginをインポートします。
org.apache.ibatis.plugin.signatureをインポートします。
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)vocation.getArgs()[0];
オブジェクトパラメーター= null;
if(invocation.getargs()。length> 1){
parameter = invocation.getargs()[1];
}
文字列sqlid = mappedstatement.getId();
BoundsQl BoundSql = MappedStatement.GetBoundSQL(パラメーター);
configuration configuration = mappedStatement.getConfiguration();
オブジェクトreturnValue = null;
long start = system.currenttimemillis();
returnValue =招待状.proceed();
long end = system.currenttimemillis();
long time =(end -start);
if(time> 1){
string sql = getsql(configuration、boundsql、sqlid、time);
System.err.println(SQL);
}
ReturnValue;
}
public static string getsql(構成構成、boundsql bundsql、string sqlid、long time){
string sql = showsql(configuration、boundsql);
stringbuilder str = new StringBuilder(100);
str.append(sqlid);
str.append( ":");
str.append(sql);
str.append( ":");
str.append(time);
str.append( "ms");
return str.toString();
}
private static string getParametervalue(オブジェクト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){
value = obj.toString();
} それ以外 {
value = "";
}
}
返品値。
}
public static string showsql(configuration configuration、boundsql bundsql){
object parameterObject = bundsql.getParameterObject();
リスト<パラメーターマッピング> parametermappings = bundsql.getParametermappings();
string sql = boundsql.getsql()。factereall( "[// 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(parametermapping parametermapping:parametermappings){
string propertyname = parametermapping.getProperty();
if(metaobject.hasgetter(propertyname)){
オブジェクトobj = metaobject.getValue(propertyName);
sql = sql.replacefirst( "//?"、getParametervalue(obj));
} else if(boundsql.hasadditionalparameter(propertyname)){
オブジェクトobj = boundsql.getadditionalparameter(propertyname);
sql = sql.replacefirst( "//?"、getParametervalue(obj));
}
}
}
}
SQLを返します。
}
パブリックオブジェクトプラグイン(オブジェクトターゲット){
plugin.wrap(ターゲット、this)を返します。
}
public void setProperties(Properties Properties0){
this.properties = properties0;
}
}