最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot集成P6Spy實現(xiàn)SQL日志的記錄詳解

 更新時間:2022年11月28日 16:43:51   作者:喜羊羊sk  
P6Spy是一個框架,它可以無縫地攔截和記錄數(shù)據(jù)庫活動,而無需更改現(xiàn)有應(yīng)用程序的代碼。一般我們使用的比較多的是使用p6spy打印我們最后執(zhí)行的sql語句

P6Spy簡介

P6Spy是一個可以用來在應(yīng)用程序中攔截和修改數(shù)據(jù)操作語句的開源框架。

通過P6Spy可以對SQL語句進行攔截,相當(dāng)于一個SQL語句的記錄器,這樣我們可以用它來作相關(guān)的分析,比如性能分析。

應(yīng)用場景

pom

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.20</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>p6spy</groupId>
            <artifactId>p6spy</artifactId>
            <version>3.8.2</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.83</version>
        </dependency>
    </dependencies>

application.yml

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    #driver-class-name: com.p6spy.engine.spy.P6SpyDriver
    type: com.alibaba.druid.pool.DruidDataSource
    url: jdbc:mysql://localhost:3306/course_db?serverTimezone=GMT%2B8
    # url: jdbc:p6spy:mysql://localhost:3306/course_db?serverTimezone=GMT%2B8
    username: root
    password: root

# 打開mybatis-plus的sql日志輸出
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

entity

@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("course_1")
public class Course {
    @TableField("cid")
    private Long cId;
    private String cName;
    private Integer userId;
    private String cStatus;
}

Mapper

public interface CourseMapper extends BaseMapper<Course> {
}

啟動類

@SpringBootApplication
@MapperScan(basePackages = "cn.zysheep.mapper")
public class ShardingjdbcdemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(ShardingjdbcdemoApplication.class, args);
    }
}

測試類

@SpringBootTest
@Slf4j
class ShardingjdbcdemoApplicationTests {
    @Autowired
    private CourseMapper courseMapper;
    @SneakyThrows
    @Test
    void findCourse() {
        courseMapper.selectList(null).forEach(System.out::println);
    }
}

mybatis-plus也可以打印輸出的sql日志,但是不是我們想要的效果,如何來控制想要的sql日志輸出,可以使用P6Spy開源產(chǎn)品。

P6Spy入門使用

spy.properties

resources目錄添加配置文件,類似log4j.xml,記錄配置信息

module.log=com.p6spy.engine.logging.P6LogFactory,com.p6spy.engine.outage.P6OutageFactory
# sql日志打印輸出
# 1、logMessageFormat=com.p6spy.engine.spy.appender.SingleLineFormat
# 2、logMessageFormat=com.p6spy.engine.spy.appender.CustomLineFormat
# customLogMessageFormat=%(currentTime) | SQL use time: %(executionTime) ms | connect info: %(category)-%(connectionId) | execute sql: %(sql)
# 3、自定義日志打印(全限定類名)
logMessageFormat=cn.zysheep.config.P6SPYConfig
# 使用日志系統(tǒng)記錄sql
appender=com.p6spy.engine.spy.appender.Slf4JLogger
## 配置記錄Log例外
excludecategories=info,debug,result,batc,resultset
# 設(shè)置使用p6spy driver來做代理
deregisterdrivers=true
# 日期格式
dateformat=yyyy-MM-dd HH:mm:ss
# 實際驅(qū)動
driverlist=com.mysql.cj.jdbc.Driver
# 是否開啟慢SQL記錄
outagedetection=true
# 慢SQL記錄標準 秒
outagedetectioninterval=2

P6Spy有內(nèi)置的SQL輸出格式,如上配置文件。這里我們使用自定義SQL日志打印

P6SPYConfig

public class P6SPYConfig  implements MessageFormattingStrategy {
    @Override
    public String formatMessage(int connectionId, String now, long elapsed, String category, String prepared, String sql, String url) {
        Map<String, Object> message = new LinkedHashMap<>(8);
        String newPrepared = prepared.replace("   ", "").replace("\n", " ");
        message.put("prepared", newPrepared);
        String newSql = sql.replace("   ", "").replace("\n", " ");
        message.put("sql", newSql);
        return JSONObject.toJSONString(message, true);
    }
}

application.yml

spring:
  datasource:
    # driver-class-name: com.mysql.cj.jdbc.Driver
    driver-class-name: com.p6spy.engine.spy.P6SpyDriver
    type: com.alibaba.druid.pool.DruidDataSource
    #  url: jdbc:mysql://localhost:3306/course_db?serverTimezone=GMT%2B8
    url: jdbc:p6spy:mysql://localhost:3306/course_db?serverTimezone=GMT%2B8
    username: root
    password: root

# 打開mybatis-plus的sql日志輸出
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

測試類不變

到此這篇關(guān)于SpringBoot集成P6Spy實現(xiàn)SQL日志的記錄詳解的文章就介紹到這了,更多相關(guān)SpringBoot集成P6Spy內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 淺談SpringBoot內(nèi)嵌Tomcat的實現(xiàn)原理解析

    淺談SpringBoot內(nèi)嵌Tomcat的實現(xiàn)原理解析

    這篇文章主要介紹了淺談SpringBoot內(nèi)嵌Tomcat的實現(xiàn)原理解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • SpringBoot+WebSocket+Netty實現(xiàn)消息推送的示例代碼

    SpringBoot+WebSocket+Netty實現(xiàn)消息推送的示例代碼

    這篇文章主要介紹了SpringBoot+WebSocket+Netty實現(xiàn)消息推送的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • IDEA解決@Slf4j中l(wèi)og報紅問題

    IDEA解決@Slf4j中l(wèi)og報紅問題

    在IntelliJ IDEA中使用log.info()時,如果出現(xiàn)錯誤,通常是因為缺少Lombok插件,以下是解決方法:打開IntelliJ IDEA,進入設(shè)置(File > Settings 或者 Ctrl+Alt+S),在Plugins部分點擊Browse repositories,搜索Lombok并安裝,安裝完成后,問題通??梢越鉀Q
    2024-12-12
  • 集群環(huán)境中使用ehcache_動力節(jié)點Java學(xué)院整理

    集群環(huán)境中使用ehcache_動力節(jié)點Java學(xué)院整理

    這篇文章主要為大家詳細介紹了集群環(huán)境中使用ehcache的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Java+Selenium實現(xiàn)文件上傳下載功能詳解

    Java+Selenium實現(xiàn)文件上傳下載功能詳解

    這篇文章主要介紹了java代碼如何利用selenium操作瀏覽器上傳和下載文件功能,文中的示例代碼講解詳細,具有一定的借鑒價值,需要的可以參考一下
    2023-01-01
  • springboot基于keytool實現(xiàn)https的雙向認證示例教程

    springboot基于keytool實現(xiàn)https的雙向認證示例教程

    這篇文章主要介紹了springboot基于keytool實現(xiàn)https的雙向認證,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-06-06
  • Java實現(xiàn)支付寶之第三方支付寶即時到賬支付功能

    Java實現(xiàn)支付寶之第三方支付寶即時到賬支付功能

    這篇文章主要介紹了Java實現(xiàn)支付寶之第三方支付寶即時到賬支付功能的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-07-07
  • Springboot 2.x中server.servlet.context-path的運用詳解

    Springboot 2.x中server.servlet.context-path的運用詳解

    這篇文章主要介紹了Springboot 2.x中server.servlet.context-path的運用詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • Java線性結(jié)構(gòu)中的雙向鏈表實現(xiàn)原理

    Java線性結(jié)構(gòu)中的雙向鏈表實現(xiàn)原理

    這篇文章將給大家詳細講解雙向鏈表的內(nèi)容,尤其是會通過代碼來進行鏈表的操作,文中的代碼示例介紹的非常詳細,具有一定的參考價值,需要的朋友可以參考下
    2023-07-07
  • mybatis if test 不為空字符串且不為null的問題

    mybatis if test 不為空字符串且不為null的問題

    這篇文章主要介紹了mybatis if test 不為空字符串且不為null的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03

最新評論

鹤壁市| 中卫市| 平凉市| 祁阳县| 达孜县| 浠水县| 安福县| 延安市| 塔河县| 南康市| 惠水县| 万盛区| 沂南县| 大港区| 本溪| 遂川县| 开化县| 北碚区| 保康县| 临夏县| 灵璧县| 盐池县| 凤翔县| 个旧市| 永城市| 花莲市| 乌兰察布市| 荥阳市| 敖汉旗| 资溪县| 闵行区| 杨浦区| 嘉荫县| 朝阳市| 新兴县| 宜兰县| 和平县| 蓬溪县| 衡南县| 贺州市| 天柱县|