java利用mybatis * 统计sql执行时间示例
时间:2021-08-22 11:35:41
可以根据执行时间打印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;
}
}
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
mybatis使用foreach查询不出结果也不报错的问题
![](https://img.aspxhome.com/file/2023/6/60116_0s.png)
Mybatis通过数据库表自动生成实体类和xml映射文件
Spring ApplicationListener监听器用法详解
mybatis foreach 循环 list(map)实例
![](https://img.aspxhome.com/file/2023/6/58876_0s.jpg)
学习Java HashMap,看这篇就够了
![](https://img.aspxhome.com/file/2023/2/59422_0s.png)
java ThreadPoolExecutor线程池拒绝策略避坑
Java Controller实现参数验证与统一异常处理流程详细讲解
详解Java变量与常量
![](https://img.aspxhome.com/file/2023/5/59005_0s.png)
详解SpringCloud Zuul过滤器返回值拦截
IDEA解决springboot热部署失效问题(推荐)
![](https://img.aspxhome.com/file/2023/8/58148_0s.png)
深入理解MyBatis中的一级缓存与二级缓存
![](https://img.aspxhome.com/file/2023/7/66057_0s.png)
java实现将结果集封装到List中的方法
Java基于享元模式实现五子棋游戏功能实例详解
![](https://img.aspxhome.com/file/2023/4/57714_0s.png)
Java进程cpu占用过高问题解决
![](https://img.aspxhome.com/file/2023/7/70337_0s.jpg)
Java语言读取配置文件config.properties的方法讲解
![](https://img.aspxhome.com/file/2023/9/86759_0s.png)
详解Maven settings.xml配置(指定本地仓库、阿里云镜像设置)
![](https://img.aspxhome.com/file/2023/9/65799_0s.png)
解析Java内存分配和回收策略以及MinorGC、MajorGC、FullGC
![](https://img.aspxhome.com/file/2023/3/62783_0s.png)
JAVA演示阿里云图像识别API,印刷文字识别-营业执照识别
C# 如何调用SAP RFC
![](https://img.aspxhome.com/file/2023/7/70437_0s.png)
SpringBoot使用Kaptcha实现验证码的生成与验证功能
![](https://img.aspxhome.com/file/2023/7/66977_0s.png)