项目配置是springboot + mybatis。
打印sql语句
application.properties配置文件中添加
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
打印sql执行时间
添加MapperAspect类
@Aspect
@Component
@Slf4j
public class MapperAspect {
@AfterReturning("execution(* com.myApp.bot.mapper.*Mapper.*(..))")
public void logServiceAccess(JoinPoint joinPoint){
// log.info("Completed: " + joinPoint);
}
@Pointcut("execution(* com.myApp.bot.mapper.*Mapper.*(..))")
private void pointCutMethod(){
}
@Around("pointCutMethod()")
public Object doAround(ProceedingJoinPoint pjp) throws Throwable{
long begin = System.nanoTime();
Object obj = pjp.proceed();
long end = System.nanoTime();
log.info("调用Mapper方法:{},参数:{},\n耗时:{}毫秒",
pjp.getSignature().toString(), Arrays.toString(pjp.getArgs()), (end - begin) / 1000000);
return obj;
}
}
代码中的com.myApp.bot.mapper.Mapper.(…))路径替换为你本地的Mapper文件路径。如下图: