MybatisPlus重寫SQL分析方法

 
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.gaara.spiderman.utils.SpiderStringUitls;
import com.p6spy.engine.spy.appender.MessageFormattingStrategy;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/********************************
 *    Author Gaara              *
 *    Version 1.0               *
 *    @ClassName SqlLogger
 *    @Date 2022/4/20 4:30 下午
 *    @Description TODO         *
 ********************************/
public class SqlLogger implements MessageFormattingStrategy {
    public SqlLogger(){

    }
    @Override
    public String formatMessage(int connectionId, String now, long elapsed, String category, String sql, String url) {
        StringBuffer stringBuffer = new StringBuffer();
        List<String> list = SpiderStringUitls.getSplit(url,"'","'");
        AtomicInteger count = new AtomicInteger();
        list.forEach(e->{
            if (count.get() != 0){
                stringBuffer.append(",");
            }
            stringBuffer.append(e);
            count.getAndIncrement();
        });

        System.out.println("┏━━━━━ Debug [Gaara SqlPlus  connectionId="+connectionId+"] ━━━");
        System.out.println("┣ SQL:\t  "+ sql.replaceAll("[\\s]+", " ") );
        System.out.println("┣ 參數(shù):\t ["+stringBuffer.toString()+"]");
        System.out.println("┣ 耗時:\t "+elapsed+"ms");
        System.out.println("┣ 時間:\t "+now);
        System.out.println("┣ 結(jié)果:\t  ");
        System.out.println("┗━━━━━ Debug [Gaara SqlPlus :"+category+"] ━━━");
        return StringUtils.isNotBlank(sql) ? " Consume Time:" + elapsed + " ms "
                + now + "\n Execute SQL:" + sql.replaceAll("[\\s]+", " ") + "\n" : "";
    }


}

其中的SpiderStringUitls是我手寫的一個匹配字符串獲取特定參數(shù)的工具類,不影響
url是一個帶有sql語句及參數(shù)的字符串,主要就是截取其中的參數(shù)

效果圖
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容