第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

0216 aop和打印數(shù)據(jù)庫(kù)執(zhí)行日志

標(biāo)簽:
Java

需求

image.png

maven依赖

image.png

   <dependency>
            <groupId>p6spy</groupId>
            <artifactId>p6spy</artifactId>
            <version>3.8.7</version>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>28.2-jre</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

打印sql

配置要点:

  1. 驱动配置 application.properties

spring.datasource.driver-class-name=com.p6spy.engine.spy.P6SpyDriver
spring.datasource.url=jdbc:p6spy:mysql://localhost:3306/demo_datasource
  1. psy配置
# 单行日志
logMessageFormat=com.p6spy.engine.spy.appender.SingleLineFormat
# 使用Slf4J记录sql
appender=com.p6spy.engine.spy.appender.Slf4JLogger
# 是否开启慢SQL记录
outagedetection=true
# 慢SQL记录标准,单位秒
outagedetectioninterval=2

aop打印持久层执行时间

使用aop实现;

package com.springbootpractice.demo.p6spy.aop;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

/**
 * 说明:aop配置
 * @author carter
 * 创建时间: 2020年02月16日 8:49 下午
 **/
@Aspect
@Component
@Slf4j
public class PrintTimeCostAspectJConfig {

    @SneakyThrows
    @Around("myPointCut()")
    public Object around(ProceedingJoinPoint pj) {
        Object res = null;
        String methodName = pj.getSignature().toShortString();
        StopWatch stopWatch = new StopWatch(methodName);
        stopWatch.start();
        try {
            res = pj.proceed();
        } catch (Throwable ex) {
            throw ex;
        } finally {
            stopWatch.stop();
            log.warn("{}执行耗时{}毫秒", methodName, stopWatch.getTotalTimeMillis());
        }
        return res;
    }

    @Pointcut("execution(* com.springbootpractice.demo.p6spy.web..*(..))")
    public void myPointCut() {
    }

}

启用aop注解:

@EnableAspectJAutoProxy(proxyTargetClass = true)

小结

来个效果截图:
image.png

通过本片文章,你可以学会:

  1. 给代码添加aop切面,增加日志或者打印出方法执行总耗时;
  2. 给你的数据持久层打印出所有的sql语句,方便生产环境排查问题;

希望大家平安度过冠疫!每天持续精进!

image.png

本文由博客一文多发平台 OpenWrite 发布!

點(diǎn)擊查看更多內(nèi)容
1人點(diǎn)贊

若覺得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消