zoukankan      html  css  js  c++  java
  • mybatis拦截器实现查看sql执行效率

    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("
    ");
                build.append(boundSql.getSql());
                build.append("
    ");
                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) {
        }
    
    }
  • 相关阅读:
    常用软件自动化测试工具汇总
    推荐一款国产优秀的基于 AI 的 Web 自动化测试工具——kylinTOP 测试与监控平台
    常用性能测试工具
    一款类似loadRunner的优秀国产压力测试工具——kylinTOP测试与监控平台
    性能测试工具基本工作原理及仿真能力比较
    10大主流性能测试工具推荐
    AVL平衡二叉查找树
    图的深度优先和广度优先遍历
    向图中增加结点
    图数据类型的定义
  • 原文地址:https://www.cnblogs.com/light-zhang/p/8398458.html
Copyright © 2011-2022 走看看