Seeing the underlying SQL in the Spring JdbcTemplate?

后端 未结 7 989
深忆病人
深忆病人 2020-11-30 02:24

I am learning about the wonders of JdbcTemplate and NamedParameterJdbcTemplate. I like what I see, but is there any easy way to see the underlying SQL that it ends up execut

相关标签:
7条回答
  • 2020-11-30 02:37

    I'm not 100% sure what you're getting at since usually you will pass in your SQL queries (parameterized or not) to the JdbcTemplate, in which case you would just log those. If you have PreparedStatements and you don't know which one is being executed, the toString method should work fine. But while we're on the subject, there's a nice Jdbc logger package here which will let you automatically log your queries as well as see the bound parameters each time. Very useful. The output looks something like this:

    executing PreparedStatement: 'insert into ECAL_USER_APPT
    (appt_id, user_id, accepted, scheduler, id) values (?, ?, ?, ?, null)'
         with bind parameters: {1=25, 2=49, 3=1, 4=1} 
    
    0 讨论(0)
  • 2020-11-30 02:38

    I use this line for Spring Boot applications:

    logging.level.org.springframework.jdbc.core = TRACE
    

    This approach pretty universal and I usually use it for any other classes inside my application.

    0 讨论(0)
  • 2020-11-30 02:39

    The Spring documentation says they're logged at DEBUG level:

    All SQL issued by this class is logged at the DEBUG level under the category corresponding to the fully qualified class name of the template instance (typically JdbcTemplate, but it may be different if you are using a custom subclass of the JdbcTemplate class).

    In XML terms, you need to configure the logger something like:

    <category name="org.springframework.jdbc.core.JdbcTemplate">
        <priority value="debug" />
    </category>
    

    This subject was however discussed here a month ago and it seems not as easy to get to work as in Hibernate and/or it didn't return the expected information: Spring JDBC is not logging SQL with log4j This topic under each suggests to use P6Spy which can also be integrated in Spring according this article.

    0 讨论(0)
  • 2020-11-30 02:40

    Try adding in log4j.xml

    <!--  enable query logging -->
    <category name="org.springframework.jdbc.core.JdbcTemplate">
        <priority value="DEBUG" />
    </category>
    
    <!-- enable query logging for SQL statement parameter value -->
    <category name="org.springframework.jdbc.core.StatementCreatorUtils">
        <priority value="TRACE" />
    </category>
    

    your logs looks like:

    DEBUG JdbcTemplate:682 - Executing prepared SQL query
    DEBUG JdbcTemplate:616 - Executing prepared SQL statement [your sql query]
    TRACE StatementCreatorUtils:228 - Setting SQL statement parameter value: column index 1, parameter value [param], value class [java.lang.String], SQL type unknown
    
    0 讨论(0)
  • 2020-11-30 02:47

    Parameter values seem to be printed on TRACE level. This worked for me:

    log4j.logger.org.springframework.jdbc.core.JdbcTem plate=DEBUG, file
    log4j.logger.org.springframework.jdbc.core.StatementCreatorUtils=TRACE, file
    

    Console output:

    02:40:56,519 TRACE http-bio-8080-exec-13 core.StatementCreatorUtils:206 - Setting SQL statement parameter value: column index 1, parameter value [Tue May 31 14:00:00 CEST 2005], value class [java.util.Date], SQL type unknown
    02:40:56,528 TRACE http-bio-8080-exec-13 core.StatementCreatorUtils:206 - Setting SQL statement parameter value: column index 2, parameter value [61], value class [java.lang.Integer], SQL type unknown
    02:40:56,528 TRACE http-bio-8080-exec-13 core.StatementCreatorUtils:206 - Setting SQL statement parameter value: column index 3, parameter value [80], value class [java.lang.Integer], SQL type unknown
    
    0 讨论(0)
  • 2020-11-30 02:50

    This worked for me with log4j2 and xml parameters:

    <?xml version="1.0" encoding="UTF-8"?>
    <Configuration status="debug">
        <Properties>
            <Property name="log-path">/some_path/logs/</Property>
            <Property name="app-id">my_app</Property>
        </Properties>
    
        <Appenders>
            <RollingFile name="file-log" fileName="${log-path}/${app-id}.log"
                filePattern="${log-path}/${app-id}-%d{yyyy-MM-dd}.log">
                <PatternLayout>
                    <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
                    </pattern>
                </PatternLayout>
                <Policies>
                    <TimeBasedTriggeringPolicy interval="1"
                        modulate="true" />
                </Policies>
            </RollingFile>
    
            <Console name="console" target="SYSTEM_OUT">
                <PatternLayout
                    pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" />
            </Console>
        </Appenders>
        <Loggers>
    
            <Logger name="org.springframework.jdbc.core" level="trace" additivity="false">
                <appender-ref ref="file-log" />
                <appender-ref ref="console" />
            </Logger>
    
            <Root level="info" additivity="false">
                <appender-ref ref="file-log" />
                <appender-ref ref="console" />
            </Root>
        </Loggers>
    
    </Configuration>
    

    Result console and file log was:

    JdbcTemplate - Executing prepared SQL query
    JdbcTemplate - Executing prepared SQL statement [select a, b from c where id = ? ]
    StatementCreatorUtils - Setting SQL statement parameter value: column index 1, parameter value [my_id], value class [java.lang.String], SQL type unknown
    

    Just copy/past

    HTH

    0 讨论(0)
提交回复
热议问题