Log4j prints JdbcTemplate and parameters

JdbcTemplate is used in the project to encapsulate the DAO layer. Sometimes it is necessary to debug and view the SQL statements and parameters

. First , the SQL statement:

I checked the decompiled source code of JdbcTemplate and found that the log level of the print statement is debug.

Therefore, add the log4j parameter: 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 + "]");
    }


Followed by statement parameters:

also track the execution process of praparedStatement, add parameters: log4j.logger.org.springframework.jdbc.core.StatementCreatorUtils=debug


Final effect:

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

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327029017&siteId=291194637