博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mybatis拦截器实现查看sql执行效率
阅读量:7039 次
发布时间:2019-06-28

本文共 1994 字,大约阅读时间需要 6 分钟。

import java.sql.Statement;import java.util.Properties;import org.apache.ibatis.executor.statement.StatementHandler;import org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl;import org.apache.ibatis.mapping.BoundSql;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.session.ResultHandler;/** * Sql执行时间记录拦截器 */@Intercepts({        @Signature(type = StatementHandler.class, method = "query", args = { Statement.class, ResultHandler.class }),        @Signature(type = StatementHandler.class, method = "update", args = { Statement.class }),        @Signature(type = StatementHandler.class, method = "batch", args = { Statement.class }) })public class SqlCostInterceptor implements Interceptor {    private static final JakartaCommonsLoggingImpl logger = new JakartaCommonsLoggingImpl("SqlCostInterceptor");    @Override    public Object intercept(Invocation invocation) throws Throwable {        long startTime = System.currentTimeMillis();        StatementHandler statementHandler = (StatementHandler) invocation.getTarget();        final BoundSql boundSql = statementHandler.getBoundSql();        try {            return invocation.proceed();        } finally {            final StringBuilder build = new StringBuilder(7);            build.append("<==  Preparing: ");            build.append("\n");            build.append(boundSql.getSql());            build.append("\n");            build.append("MYBATIS-SQL执行耗时[");            build.append((System.currentTimeMillis() - startTime));            build.append("ms]");            logger.debug(build.toString());        }    }    @Override    public Object plugin(Object target) {        return Plugin.wrap(target, this);    }    @Override    public void setProperties(Properties properties) {    }}

 

转载于:https://www.cnblogs.com/light-zhang/p/8398458.html

你可能感兴趣的文章
sql DATEPART() MONTH() convert() cast() dateadd() DATEDIFF() with(nolock)
查看>>
线程池ThreadPoolExecutor
查看>>
github中删除项目
查看>>
CentOS中/英文环境切换教程(CentOS6.8)
查看>>
Python的一个命名空间冲突,关于from-import机制
查看>>
jQuery动画详解
查看>>
3.移植驱动到3.4内核-移植DM9000C驱动
查看>>
Mysql 奇怪的连接错误
查看>>
给程序员简历的一些建议
查看>>
CSS3饼状loading效果
查看>>
docker日志
查看>>
Postman使用入门
查看>>
编程修养(一)
查看>>
Solidworks如何替换工程图参考零件
查看>>
2013年第四届蓝桥杯C/C++B组省赛题目解析
查看>>
重温.NET下Assembly的加载过程
查看>>
SpringBoot 之Spring Boot Starter依赖包及作用
查看>>
websocket 协议 使用
查看>>
微信小程序——自定义导航栏
查看>>
《JavaScript高级程序设计》笔记:DOM2和DOM3(十二)
查看>>