在 Spring Boot 中整合 MySQL 并打印 SQL 日志,需通过配置数据源、JPA 及日志框架实现。以下是详细步骤及最佳实践:
一、添加依赖
在 pom.xml中引入 Spring Data JPA 和 MySQL 驱动依赖:
<dependencies>
<!-- Spring Data JPA -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- MySQL 驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
二、配置数据源与 JPA
在 application.properties或 application.yml中配置 MySQL 连接及日志参数:
# 数据源配置
spring.datasource.url=jdbc:mysql://localhost:3306/your_database?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.dat

最低0.47元/天 解锁文章
307

被折叠的 条评论
为什么被折叠?



