application.yml中配置打印jdbc的SQL语句
时间: 2023-08-05 13:09:48 浏览: 71
在Spring Boot应用中,可以通过在application.yml文件中配置,实现打印JDBC的SQL语句的功能。
具体的实现步骤如下:
1. 在application.yml文件中,配置以下信息:
```yaml
spring:
datasource:
url: jdbc:mysql://localhost:3306/test
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
# 打印SQL语句
# 可选的值:NONE、CONSOLE、LOG_FILE、LOG_FILE_AND_CONSOLE
# 默认值:NONE
# 可以使用logging.level.org.springframework.jdbc.core.JdbcTemplate=DEBUG代替
#logging:
# level:
# org:
# springframework:
# jdbc:
# core:
# JdbcTemplate: DEBUG
datasource:
type: com.alibaba.druid.pool.DruidDataSource
# 配置Druid数据源,可以省略
druid:
initial-size: 5
max-active: 20
min-idle: 5
max-wait: 60000
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
validation-query: SELECT 1
test-on-borrow: true
test-while-idle: true
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
filters: stat,wall,log4j
connection-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
```
其中,logging.level.org.springframework.jdbc.core.JdbcTemplate=DEBUG可以在application.yml中配置,也可以在log4j或logback等日志框架中配置。
2. 在代码中使用JdbcTemplate时,即可在日志中看到打印的SQL语句,例如:
```java
@Autowired
private JdbcTemplate jdbcTemplate;
public void query() {
List<Map<String, Object>> result = jdbcTemplate.queryForList("SELECT * FROM user");
//...
}
```
在日志中会打印出类似以下的SQL语句:
```java
DEBUG [main] o.s.j.c.JdbcTemplate - Executing SQL query [SELECT * FROM user]
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)