微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

自定义 mybatis-plus 日志


import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.Properties;

@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})
})
@Slf4j
@Component("mybatisLogInterceptor")
public class MybatisLogInterceptor implements Interceptor {

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        long start = System.currentTimeMillis();
        Object result = invocation.proceed();
        long end = System.currentTimeMillis();
        PreparedStatement statement = (PreparedStatement) invocation.getArgs()[0];
        if (Proxy.isProxyClass(statement.getClass())) {
            InvocationHandler preparedStatementLogger = Proxy.getInvocationHandler(statement);
            if (preparedStatementLogger.getClass().getName().endsWith(".PreparedStatementLogger")) {
                Field field = preparedStatementLogger.getClass().getDeclaredField("statement");
                field.setAccessible(true);
                PreparedStatement clientPreparedStatement = null;
                PreparedStatement tempPreparedStatement = (PreparedStatement) field.get(preparedStatementLogger);

                if (tempPreparedStatement.getClass().getName().endsWith(".DruidPooledPreparedStatement")) {
                    field = tempPreparedStatement.getClass().getDeclaredField("stmt");
                    field.setAccessible(true);
                    tempPreparedStatement = (PreparedStatement) field.get(tempPreparedStatement);

                    if (tempPreparedStatement.getClass().getName().endsWith(".ClientPreparedStatement")) {
                        clientPreparedStatement = tempPreparedStatement;
                    } else if (tempPreparedStatement.getClass().getName().endsWith(".PreparedStatementProxyImpl")) {
                        field = tempPreparedStatement.getClass().getDeclaredField("statement");
                        field.setAccessible(true);
                        tempPreparedStatement = (PreparedStatement) field.get(tempPreparedStatement);
                        if (tempPreparedStatement.getClass().getName().endsWith(".ClientPreparedStatement")) {
                            clientPreparedStatement = tempPreparedStatement;
                        }
                    }
                } else {
                    clientPreparedStatement = tempPreparedStatement;
                }
                Long cost = end - start;
                if (clientPreparedStatement != null) {
                    log.info("cost:{} ms, executesql:{}, costFirst-{}, costLevel-{} ", cost,
                             clientPreparedStatement.toString().replaceAll("[\\s\n ]+", " ")
                                                    .replaceFirst("com.MysqL.cj.jdbc.ClientPreparedStatement:", " "),
                             String.valueOf(cost).charat(0), String.valueOf(cost).length());
                }
            }
        }
        return result;
    }

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

    @Override
    public void setProperties(Properties properties) {

    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐