Hibernate print SQL and additional parameters

Hibernate print SQL and additional parameters

In the Hibernate configuration file hibernate.cfg.xml, there are 3 settings related to the display of SQL statements, and their values ​​are all boolean values: 1. show_sql: whether to display the SQL statement 2. format_sql: whether to format the output string, enhanced The readability of SQL 3. use_sql_comments: Whether to display comments, used to indicate what operation generated the SQL statement.

If show_sql=true is set, only SQL statements will be printed by default without printing parameters:

If you need to print parameters, please add in the log4j configuration file: log4j.properties:

log4j.logger.org.hibernate.type.descriptor.sql.BasicBinder=TRACE

log4j.loggerorg.hibernate.type.descriptor.sql.BasicExtractor=TRACE

If you need to view the value of the named parameter in the query, continue to add:

 log4j.logger.org.hibernate.engine.QueryParameters=DEBUG

 log4j.logger.org.hibernate.engine.query.HQLQueryPlan=DEBUG

Next, provide all the configuration:

log4j.rootLogger=info, CA

# ConsoleAppender

log4j.appender.CA=org.apache.log4j.ConsoleAppender

log4j.appender.CA.layout=org.apache.log4j.PatternLayout

log4j.appender.CA.layout.ConversionPattern=%d{hh\:mm\:ss,SSS} [%t] %-5p %c %x – %m%n

#To display the parameters

log4j.logger.org.hibernate.type.descriptor.sql.BasicBinder=TRACE

log4j.loggerorg.hibernate.type.descriptor.sql.BasicExtractor=TRACE

#View the value of the named parameter in the query

 log4j.logger.org.hibernate.engine.QueryParameters=DEBUG

 log4j.logger.org.hibernate.engine.query.HQLQueryPlan=DEBUG

When using, just set show_sql in hibernate.cfg.xml to true and copy the red code to the configuration file.