当前位置:首页 > Java 框架原理百科 > 正文

MyBatis查Java优学网插件开发:轻松监控SQL性能,告别慢查询烦恼

org.mybatis mybatis 3.5.6

@Intercepts({

@Signature(type = Executor.class, method = "query", 
           args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})

}) public class PerformanceMonitorPlugin implements Interceptor {

MyBatis查Java优学网插件开发:轻松监控SQL性能,告别慢查询烦恼

private static final Logger logger = LoggerFactory.getLogger(PerformanceMonitorPlugin.class);

@Override
public Object intercept(Invocation invocation) throws Throwable {
    long startTime = System.currentTimeMillis();
    try {
        return invocation.proceed();
    } finally {
        long costTime = System.currentTimeMillis() - startTime;
        if (costTime > SLOW_QUERY_THRESHOLD) {
            logger.warn("Slow query detected: {}ms, method: {}", 
                       costTime, invocation.getMethod().getName());
        }
    }
}

}

public interface PluginChain {

MyBatis查Java优学网插件开发:轻松监控SQL性能,告别慢查询烦恼

Object proceed(Invocation invocation) throws Throwable;

}

// 在拦截器中 @Override public Object intercept(Invocation invocation) throws Throwable {

// 前置处理
preProcess(invocation);

// 执行后续插件链
Object result = pluginChain.proceed(invocation);

// 后置处理
postProcess(invocation, result);

return result;

}

你可能想看:

相关文章:

文章已关闭评论!