您好,登錄后才能下訂單哦!
本篇內容介紹了“SpringBoot如何集成P6Spy實現SQL日志”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
P6Spy是一個可以用來在應用程序中攔截和修改數據操作語句的開源框架。
通過P6Spy可以對SQL語句進行攔截,相當于一個SQL語句的記錄器,這樣我們可以用它來作相關的分析,比如性能分析。
<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>
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
@Data @NoArgsConstructor @AllArgsConstructor @TableName("course_1") public class Course { @TableField("cid") private Long cId; private String cName; private Integer userId; private String cStatus; }
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開源產品。
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
# 使用日志系統記錄sql
appender=com.p6spy.engine.spy.appender.Slf4JLogger
## 配置記錄Log例外
excludecategories=info,debug,result,batc,resultset
# 設置使用p6spy driver來做代理
deregisterdrivers=true
# 日期格式
dateformat=yyyy-MM-dd HH:mm:ss
# 實際驅動
driverlist=com.mysql.cj.jdbc.Driver
# 是否開啟慢SQL記錄
outagedetection=true
# 慢SQL記錄標準 秒
outagedetectioninterval=2
P6Spy有內置的SQL輸出格式,如上配置文件。這里我們使用自定義SQL日志打印
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); } }
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
測試類不變
“SpringBoot如何集成P6Spy實現SQL日志”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。