package com.cdos.vas.interceptor;import com.alibaba.druid.pool.DruidPooledPreparedStatement;import com.alibaba.druid.proxy.jdbc.PreparedStatementProxyImpl;import lombok.extern.slf4j.Slf4j;import org.apache.ibatis.executor.statement.StatementHandler;import org.apache.ibatis.plugin.Interceptor;import org.apache.ibatis.plugin.Intercepts;import org.apache.ibatis.plugin.Invocation;import org.apache.ibatis.plugin.Signature;import org.apache.ibatis.session.ResultHandler;import org.springframework.stereotype.Component;import java.sql.PreparedStatement;import java.sql.Statement;import java.util.Properties;@Slf4j@Component@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 ExplainInterceptor implements Interceptor { @Override public Object intercept(Invocation invocation) throws Throwable { Object returnValue; long start = System.currentTimeMillis(); returnValue = invocation.proceed(); long end = System.currentTimeMillis(); long time = end - start; if (time > 1000) { try { final Object[] args = invocation.getArgs(); //获取原始的ms DruidPooledPreparedStatement ms = (DruidPooledPreparedStatement) args[0]; PreparedStatement rawObject = ((PreparedStatementProxyImpl) ms.getStatement()).getRawObject(); log.info("zhp过滤慢sql" + rawObject.toString()); } catch (Exception e) { log.error("拦截sql处理出差" + e.getMessage()); e.printStackTrace(); } } return returnValue; } @Override public Object plugin(Object target) { return Interceptor.super.plugin(target); } @Override public void setProperties(Properties properties) { Interceptor.super.setProperties(properties); }}