如何设置hibernate打印sql语句
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何设置hibernate打印sql语句相关的知识,希望对你有一定的参考价值。
参考技术A 只要加上下面这个语句就可以了 Log4j.propreties代码 #输出sql语句到日志 log4j.logger.org.hibernate.SQL=debug #将对应的参数占位符?换成传入的参数 log4j.logger.org.hibernate.type=trace本回答被提问者采纳如何将Hibernate生成的SQL语句打印到日志文件
背景:项目开发需要。
1 操作
只需要在log4j.properties中加入以下两行代码即可
#输出sql语句到日志 log4j.logger.org.hibernate.SQL=debug #将对应的参数占位符?换成传入的参数 log4j.logger.org.hibernate.type=trace
实验效果:
2017-09-05 16:51:26 [DubboServerHandler-thread-1] DEBUG org.hibernate.SQL (104) - select cpsysparm0_.CODE as CODE65_0_, cpsysparm0_.NAME as NAME65_0_, cpsysparm0_.PARMVALUE as PARMVALUE65_0_, cpsysparm0_.PARMDESC as PARMDESC65_0_, cpsysparm0_.MODIFIABLE as MODIFIABLE65_0_, cpsysparm0_.VIEWABLE as VIEWABLE65_0_, cpsysparm0_.OPERUSER as OPERUSER65_0_, cpsysparm0_.OPERTIME as OPERTIME65_0_ from CP_SYSPARM cpsysparm0_ where cpsysparm0_.CODE=? 2017-09-05 16:51:26 [DubboServerHandler-thread-1] TRACE org.hibernate.type.descriptor.sql.BasicBinder (83) - binding parameter [1] as [VARCHAR] - 012 2017-09-05 16:51:26 [DubboServerHandler-thread-1] TRACE org.hibernate.type.descriptor.sql.BasicExtractor (72) - Found [当前场次 ] as column [NAME65_0_] 2017-09-05 16:51:26 [DubboServerHandler-thread-1] TRACE org.hibernate.type.descriptor.sql.BasicExtractor (72) - Found [1] as column [PARMVALUE65_0_] 2017-09-05 16:51:26 [DubboServerHandler-thread-1] TRACE org.hibernate.type.descriptor.sql.BasicExtractor (67) - Found [null] as column [PARMDESC65_0_] 2017-09-05 16:51:26 [DubboServerHandler-thread-1] TRACE org.hibernate.type.descriptor.sql.BasicExtractor (72) - Found [0] as column [MODIFIABLE65_0_] 2017-09-05 16:51:26 [DubboServerHandler-thread-1] TRACE org.hibernate.type.descriptor.sql.BasicExtractor (72) - Found [1] as column [VIEWABLE65_0_] 2017-09-05 16:51:26 [DubboServerHandler-thread-1] TRACE org.hibernate.type.descriptor.sql.BasicExtractor (72) - Found [init ] as column [OPERUSER65_0_] 2017-09-05 16:51:26 [DubboServerHandler-thread-1] TRACE org.hibernate.type.descriptor.sql.BasicExtractor (72) - Found [2011-11-23 10:06:47.0] as column [OPERTIME65_0_]
可以看到查询的过程和结果都进行了输出。
以上是关于如何设置hibernate打印sql语句的主要内容,如果未能解决你的问题,请参考以下文章
SpringBoot JPA打印JPA执行的SQL语句与参数Springboot JPA日志输出打印SQL语句和传入的参数 高阶篇