欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

P6Spy簡介

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

通過P6Spy可以對(duì)SQL語句進(jìn)行攔截,相當(dāng)于一個(gè)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> {
}

啟動(dòng)類

@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
# 實(shí)際驅(qū)動(dòng)
driverlist=com.mysql.cj.jdbc.Driver
# 是否開啟慢SQL記錄
outagedetection=true
# 慢SQL記錄標(biāo)準(zhǔn) 秒
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實(shí)現(xiàn)SQL日志的記錄詳解的文章就介紹到這了,更多相關(guān)SpringBoot集成P6Spy內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot配置主從數(shù)據(jù)庫實(shí)現(xiàn)讀寫分離

    SpringBoot配置主從數(shù)據(jù)庫實(shí)現(xiàn)讀寫分離

    現(xiàn)在的 Web 應(yīng)用大都是讀多寫少,本文主要介紹了SpringBoot配置主從數(shù)據(jù)庫實(shí)現(xiàn)讀寫分離,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-11-11
  • Java實(shí)現(xiàn)圖片拼接

    Java實(shí)現(xiàn)圖片拼接

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)圖片拼接的相關(guān)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • Java編程rabbitMQ實(shí)現(xiàn)消息的收發(fā)

    Java編程rabbitMQ實(shí)現(xiàn)消息的收發(fā)

    RabbitMQ是一個(gè)在AMQP基礎(chǔ)上完成的,可復(fù)用的企業(yè)消息系統(tǒng),本文通過實(shí)例來給大家分享通過操作rabbitMQ實(shí)現(xiàn)消息的收發(fā),感興趣的朋友可以參考下。
    2017-09-09
  • 徹底解決Spring mvc中時(shí)間的轉(zhuǎn)換和序列化等問題

    徹底解決Spring mvc中時(shí)間的轉(zhuǎn)換和序列化等問題

    這篇文章主要介紹了徹底解決Spring mvc中時(shí)間的轉(zhuǎn)換和序列化等問題,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • Java拆裝箱深度剖析

    Java拆裝箱深度剖析

    這篇文章主要為大家深度剖析了Java拆箱裝箱的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • 解析Java異常的棧軌跡及其相關(guān)方法

    解析Java異常的棧軌跡及其相關(guān)方法

    這篇文章主要介紹了解析Java異常的棧軌跡及其相關(guān)方法,包括棧軌跡的打印和fillInStackTrace方法等,需要的朋友可以參考下
    2015-11-11
  • 詳解Struts2中json 相互引用死循環(huán)解決辦法

    詳解Struts2中json 相互引用死循環(huán)解決辦法

    本篇文章主要介紹詳解Struts2中json 相互引用死循環(huán)解決辦法,具有一定的參考價(jià)值,有興趣的可以了解一下。
    2017-01-01
  • SpringBoot之@Value獲取application.properties配置無效的解決

    SpringBoot之@Value獲取application.properties配置無效的解決

    這篇文章主要介紹了SpringBoot之@Value獲取application.properties配置無效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • java實(shí)現(xiàn)單鏈表、雙向鏈表

    java實(shí)現(xiàn)單鏈表、雙向鏈表

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)單鏈表、雙向鏈表的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-03-03
  • Springboot的yml配置文件用法

    Springboot的yml配置文件用法

    這篇文章主要介紹了Springboot的yml配置文件用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03

最新評(píng)論