springboot-mybatis 打印sql执行时间,以及sql语句

#打印执行的sql语句
logging.level.com.lsj.xcjfs.dao=debug

打印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)