1. package com.cdos.vas.interceptor;
    2. import com.alibaba.druid.pool.DruidPooledPreparedStatement;
    3. import com.alibaba.druid.proxy.jdbc.PreparedStatementProxyImpl;
    4. import lombok.extern.slf4j.Slf4j;
    5. import org.apache.ibatis.executor.statement.StatementHandler;
    6. import org.apache.ibatis.plugin.Interceptor;
    7. import org.apache.ibatis.plugin.Intercepts;
    8. import org.apache.ibatis.plugin.Invocation;
    9. import org.apache.ibatis.plugin.Signature;
    10. import org.apache.ibatis.session.ResultHandler;
    11. import org.springframework.stereotype.Component;
    12. import java.sql.PreparedStatement;
    13. import java.sql.Statement;
    14. import java.util.Properties;
    15. @Slf4j
    16. @Component
    17. @Intercepts({@Signature(type = StatementHandler.class, method = "query", args = {Statement.class, ResultHandler.class}),
    18. @Signature(type = StatementHandler.class, method = "update", args = {Statement.class}),
    19. @Signature(type = StatementHandler.class, method = "batch", args = {Statement.class})})
    20. public class ExplainInterceptor implements Interceptor {
    21. @Override
    22. public Object intercept(Invocation invocation) throws Throwable {
    23. Object returnValue;
    24. long start = System.currentTimeMillis();
    25. returnValue = invocation.proceed();
    26. long end = System.currentTimeMillis();
    27. long time = end - start;
    28. if (time > 1000) {
    29. try {
    30. final Object[] args = invocation.getArgs();
    31. //获取原始的ms
    32. DruidPooledPreparedStatement ms = (DruidPooledPreparedStatement) args[0];
    33. PreparedStatement rawObject = ((PreparedStatementProxyImpl) ms.getStatement()).getRawObject();
    34. log.info("zhp过滤慢sql" + rawObject.toString());
    35. } catch (Exception e) {
    36. log.error("拦截sql处理出差" + e.getMessage());
    37. e.printStackTrace();
    38. }
    39. }
    40. return returnValue;
    41. }
    42. @Override
    43. public Object plugin(Object target) {
    44. return Interceptor.super.plugin(target);
    45. }
    46. @Override
    47. public void setProperties(Properties properties) {
    48. Interceptor.super.setProperties(properties);
    49. }
    50. }