How to Log SQL Statements in Spring Boot 3 Using JDBC, JPA, MyBatis & datasource-proxy
This article announces a Spring Boot 3 case collection with 100 permanent examples and then provides a comprehensive guide on recording SQL statements in Spring Boot applications via JDBC, JPA, MyBatis and the datasource‑proxy library, including configuration snippets, code samples and visual output illustrations.
Introduction
In Spring Boot application development, monitoring and recording executed SQL statements is essential for debugging, performance tuning, and ensuring the correctness of the data access layer. Whether you use plain JDBC, JPA, or MyBatis, each has its own way of logging SQL.
Practical Cases
1. JDBC SQL Logging
Code example:
<code>private final JdbcTemplate jdbcTemplate;
public JdbcService(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void query() {
String sql = "select id, name, age from user x where x.id = ?";
User user = this.jdbcTemplate.queryForObject(sql, new RowMapper<User>() {
public User mapRow(ResultSet rs, int rowNum) throws SQLException {
return new User(rs.getLong(1), rs.getString(2), rs.getString(3));
}
}, 8);
System.err.printf("user = %s\n", user);
}
</code>By default this does not output any SQL. Enable logging with:
<code>logging:
level:
'[org.springframework.jdbc.core.JdbcTemplate]': debug
</code>To also log bound parameters, add:
<code>logging:
level:
'[org.springframework.jdbc.core.StatementCreatorUtils]': trace
</code>2. JPA SQL Logging
Code example:
<code>private final UserRepository userRepository;
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
public User findById(Long id) {
return this.userRepository.findById(id).orElse(null);
}
</code>Common way to enable SQL output:
<code>spring:
jpa:
show-sql: true
properties:
hibernate:
format_sql: true
</code>Note: this prints SQL via System.out and does not show prepared‑statement parameters, which is not recommended.
Better approach: configure the logger:
<code>logging:
level:
'[org.hibernate.SQL]': debug
'[org.hibernate.orm.jdbc.bind]': trace
</code>3. MyBatis SQL Logging
Code example:
<code>@Select("select id, age, name, deleted from user where id = ${id}")
User queryUserById(@Param("id") Long id);
</code>Typical logger configuration:
<code>mybatis:
configuration:
map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
</code>This prints both SQL and parameter values via System.out , regardless of the log level.
To use SLF4J instead:
<code>mybatis:
configuration:
map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.slf4j.Slf4jImpl
</code>And add a logger rule for your mapper package:
<code>logging:
level:
'[com.pack.mapper]': DEBUG
</code>4. Universal SQL Logging with datasource‑proxy
Dependency:
<code><dependency>
<groupId>com.github.gavlyukovskiy</groupId>
<artifactId>datasource-proxy-spring-boot-starter</artifactId>
<version>1.10.0</version>
</dependency>
</code>Logging level:
<code>logging:
level:
'[net.ttddyy.dsproxy.listener]': debug
</code>With this starter, JDBC, JPA and MyBatis all share the same elegant SQL logging capability.
Promotional Notice
The "Spring Boot 3实战案例合集" now contains over 100 practical articles and will be continuously updated for free. Subscribers receive the complete MD documentation and source code. The collection aims to provide the latest techniques and real‑world examples for Spring Boot developers.
To get the collection, click the subscription link, send a private message, and the e‑book will be delivered promptly.
Conclusion
By configuring appropriate logging levels or using the datasource‑proxy starter, you can reliably capture SQL statements and their parameters across JDBC, JPA, and MyBatis in Spring Boot 3, facilitating debugging and performance analysis.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.