mysql动态添加mybatis的sql执行所需要的时间

该博客介绍了如何使用MyBatis的拦截器插件SqlStatementInterceptor来监控SQL执行的耗时。通过在Executor的update和query方法上添加拦截,记录并根据执行时间不同级别输出日志,帮助开发者定位慢查询问题。配置文件中启用了插件,实现了对SQL执行时间的精细化监控。
摘要由CSDN通过智能技术生成
SqlStatementInterceptor.java


import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Properties;

/**
 * @Author
 * @Date
 * @Version 版本号
 * @Description mybatis执行sql耗时
 */
@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, CacheKey.class,BoundSql.class})})
@SuppressWarnings({"unchecked", "rawtypes"})
public class SqlStatementInterceptor implements Interceptor {
    public static final Logger log = LoggerFactory.getLogger(SqlStatementInterceptor.class);

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        long startTime = System.currentTimeMillis();
        try {
            return invocation.proceed();
        } finally {
            long timeConsuming = System.currentTimeMillis()-startTime;
            log.debug("执行SQL:{}ms", timeConsuming);
            if(timeConsuming>999&&timeConsuming<5000){
                log.info("执行SQL大于1s:{}ms", timeConsuming);
            }else if(timeConsuming>=5000&&timeConsuming<10000){
                log.info("执行SQL大于5s:{}ms", timeConsuming);
            }else if(timeConsuming>=10000){
                log.info("执行SQL大于10s:{}ms", timeConsuming);
            }
        }
    }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {

    }
}

配置文件开启插件

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<settings>
		<setting name="cacheEnabled"             value="true"  />  <!-- 全局映射器启用缓存 -->
		<setting name="useGeneratedKeys"         value="false"  />  <!-- 允许 JDBC 支持自动生成主键 -->
		<setting name="defaultExecutorType"      value="REUSE" />  <!-- 配置默认的执行器 -->
		<setting name="logImpl"                  value="SLF4J" />  <!-- 指定 MyBatis 所用日志的具体实现 -->
		<setting name="mapUnderscoreToCamelCase" value="true"/>  <!--驼峰式命名 -->
	</settings>
	<plugins>
		<plugin interceptor="com.config.SqlStatementInterceptor"/>
	</plugins>
	
</configuration>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值