java利用mybatis拦截器统计sql执行时间示例
可以根据执行时间打印sql语句,打印的sql语句是带参数的,可以拷贝到查询分析器什么的直接运行
package mybatis;
import java.text.dateformat;
import java.util.date;
import java.util.list;
import java.util.locale;
import java.util.properties;
import org.apache.ibatis.executor.executor;
import org.apache.ibatis.mapping.boundsql;
import org.apache.ibatis.mapping.mappedstatement;
import org.apache.ibatis.mapping.parametermapping;
import org.apache.ibatis.plugin.interceptor;
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 }) })
public class mybatisinterceptor implements interceptor {
private properties properties;
public object intercept(invocation invocation) throws throwable {
mappedstatement mappedstatement = (mappedstatement) invocation.getargs()[0];
object parameter = null;
if (invocation.getargs().length > 1) {
parameter = invocation.getargs()[1];
}
string sqlid = mappedstatement.getid();
boundsql boundsql = mappedstatement.getboundsql(parameter);
configuration configuration = mappedstatement.getconfiguration();
object returnvalue = null;
long start = system.currenttimemillis();
returnvalue = invocation.proceed();
long end = system.currenttimemillis();
long time = (end - start);
if (time > 1) {
string sql = getsql(configuration, boundsql, sqlid, time);
system.err.println(sql);
}
return returnvalue;
}
public static string getsql(configuration configuration, boundsql boundsql, 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(object 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()) + "'";
} else {
if (obj != null) {
value = obj.tostring();
} else {
value = "";
}
}
return value;
}
public static string showsql(configuration configuration, boundsql boundsql) {
object parameterobject = boundsql.getparameterobject();
list<parametermapping> 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));
} else {
metaobject metaobject = configuration.newmetaobject(parameterobject);
for (parametermapping parametermapping : parametermappings) {
string propertyname = parametermapping.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));
}
}
}
}
return sql;
}
public object plugin(object target) {
return plugin.wrap(target, this);
}
public void setproperties(properties properties0) {
this.properties = properties0;
}
}