springboot 打印sql执行信息日志 (sql语句,执行时间)

原文链接: https://blog.csdn.net/ysk_xh_521/article/details/81624534

最近接口ab压测,发现写接口比较慢,所以需要在日志中打印一些sql的执行时间,排查问题。


@Aspect
@Component
@Log4j2
public class MapperAspect {

    @AfterReturning("execution(* com.lsj.xcjfs.dao.*Mapper.*(..))")
    public void logServiceAccess(JoinPoint joinPoint) {
        log.info("Completed: " + joinPoint);
    }


    /**
     * 监控com.lsj.xcjfs.dao..*Mapper包及其子包的所有public方法
     */
    @Pointcut("execution(* com.lsj.xcjfs.dao.*Mapper.*(..))")
    private void pointCutMethod() {
    }

    /**
     * 声明环绕通知
     *
     * @param pjp
     * @return
     * @throws Throwable
     */
    @Around("pointCutMethod()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        long begin = System.nanoTime();
        Object obj = pjp.proceed();
        long end = System.nanoTime();

        log.info("调用Mapper方法:{},参数:{},执行耗时:{}纳秒,耗时:{}毫秒",
                pjp.getSignature().toString(), Arrays.toString(pjp.getArgs()),
                (end - begin), (end - begin) / 1000000);
        return obj;
    }
}

 

你可能感兴趣的:(springboot,打印sql执行时间,mysql,数据库,项目)