如何将Hibernate生成的SQL语句打印到日志文件
发布网友
发布时间:2022-10-30 07:40
我来回答
共1个回答
热心网友
时间:2023-10-10 23:00
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_]
可以看到查询的过程和结果都进行了输出。