项目中使用JdbcTemplate进行DAO层封装,有时需要调试查看SQL语句及参数
首先是SQL语句:
查看了JdbcTemplate的反编译源码,发现打印语句的地方,其日志级别为debug,
因此,添加log4j参数:log4j.logger.org.springframework.jdbc.core.JdbcTemplate=debug
public int update(String sql) throws DataAccessException {
Assert.notNull(sql, “SQL must not be null”);
if (this.logger.isDebugEnabled()) {
this.logger.debug(“Executing SQL update [” + sql + “]”);
}
其后是语句参数:
同样跟踪执行praparedStatement执行过程,添加参数:log4j.logger.org.springframework.jdbc.core.StatementCreatorUtils=debug
最终效果:
17:05:26,034 DEBUG JdbcTemplate:574 – Executing prepared SQL statement [select * from A WHERE ID=?]
17:05:26,459 DEBUG StatementCreatorUtils:207 – Setting SQL statement parameter value: column index 1, parameter value [1111], value class [java.lang.String], SQL type 12
测试3.0后需要把
改为
我测试的是4.0.2,也是在源码中看到它用的是trace