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

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 {

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;
}
你可能想看:
MyBatis查Java优学网类型处理器:轻松优化数据转换,提升开发效率与查询性能
Java优学网MyBatis分页入门解析:轻松掌握高效数据分页技巧,告别复杂SQL编写烦恼
Java优学网MyBatis类型处理器入门解析:轻松掌握Java与数据库类型转换,告别数据转换烦恼
Java优学网SpringBoot多环境配置讲解:轻松实现开发、测试、生产环境一键切换,告别配置错误烦恼
MyBatis查Java优学网动态SQL:告别重复代码,实现灵活数据库查询
Java优学网MyBatis删除短文:轻松掌握高效删除操作,告别数据冗余烦恼