Hibernate configuration print sql statement and parameters

In Hibernate's configuration file hibernate.cfg.xml, there are 3 setting items related to displaying SQL statements, and their values ​​are all boolean values: 
1. show_sql: whether to display SQL statements 
2. format_sql: whether to format output strings, enhance SQL readability 
3. use_sql_comments: Whether to display comments to indicate what operation generated this SQL statement.

If show_sql=true is set, only the SQL statement will be printed without printing parameters by default: 
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 see the value of the named parameter in the query, go ahead and add: 
log4j.logger.org.hibernate.engine.QueryParameters=DEBUG 
log4j.logger.org.hibernate.engine.query.HQLQueryPlan=DEBUG

Next, give all the configuration: 
log4j.rootLogger=info, CA

Console Appender

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 parameters

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

View the value of a named parameter in a query

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

When using it, you only need to set show_sql in hibernate.cfg.xml to true and then copy the red code to the configuration file.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325984287&siteId=291194637