Hibernate打印SQL及附加参数(转载) (一)

2014-11-24 11:54:48 · 作者: · 浏览: 52

在Hibernate的配置文件hibernate.cfg.xml中有3个设置项跟显示SQL语句相关,他们的值都是boolean值:
(1)、show_sql:是否显示SQL语句
(2)、format_sql: 是否格式化输出字符串,增强SQL的可读性
(3)、use_sql_comments:是否显示注释,用于指示出是什么操作产生了这个SQL语句。

在默认情况下,Hibernate会把SQL语句打印在Console上,因此在开启了上面的设置之后,可以在控制台上看到如下结构的SQL语句:
Console代码:


[html] /* load collection cc.unmi.test.model.Post.securities */ select
02 securities0_.post_id as post1_7_1_,
03 security1_.shareclassid as sharecla1_16_0_,
04 security1_.company_id as company2_16_0_,
05 from
06 Post_Security_Relationship securities0_
07 inner join
08 unmi.securities security1_
09 on securities0_.shareclassid=security1_.shareclassid
10 where
11 securities0_.post_id=

01 /* load collection cc.unmi.test.model.Post.securities */ select
02 securities0_.post_id as post1_7_1_,
03 security1_.shareclassid as sharecla1_16_0_,
04 security1_.company_id as company2_16_0_,
05 from
06 Post_Security_Relationship securities0_
07 inner join
08 unmi.securities security1_
09 on securities0_.shareclassid=security1_.shareclassid
10 where
11 securities0_.post_id=

可以发现,在控制台上根本看不到,SQL语句对应的参数,一般情况下,Hibernate都会和Log4j配合使用,这样就可以更加灵活的控制 hibernate的日志文件输出。在hibernate中,默认的关于SQL语句对应参数的输出级别为TRACE,比默认的Log4j的日志等级 DEBUG还要低一等级,因此,为了显示参数,还需手动设置一下log4j的配置,把hibernate下的输出等级改为TRACE:
log4j.logger.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
log4j. loggerorg.hibernate.type.descriptor.sql.BasicExtractor=TRACE
这样修改之后,打印的SQL语句会变为如下形式:
Console代码:
[html] 01 20:13:40.710 [http-8080-1] DEBUG org.hibernate.SQL -
02 /* load collection cc.unmi.test.model.Post.categories */ select
03 categories0_.post_id as post1_7_1_,
04 elementite1_.id as id3_0_,
05 from
06 Post_Category_Relationship categories0_
07 inner join
08 unmi.element_item elementite1_
09 on categories0_.category_id=elementite1_.id
10 where
11 categories0_.post_id=
12 20:13:40.710 [http-8080-1] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [INTEGER] - 10
13 20:13:40.710 [http-8080-1] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - found [1002] as column [id3_0_]
14 20:13:40.710 [http-8080-1] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - found [10] as column [post1_7_1_]

01 20:13:40.710 [http-8080-1] DEBUG org.hibernate.SQL -
02 /* load collection cc.unmi.test.model.Post.categories */ select
03 categories0_.post_id as post1_7_1_,
04 elementite1_.id as id3_0_,
05 from
06 Post_Category_Relationship categories0_
07 inner join
08 unmi.element_item elementite1_
09 on categories0_.category_id=elementite1_.id
10 where
11 categories0_.post_id=
12 20:13:40.710 [http-8080-1] TRACE org.hibernate.type.descriptor.sql.BasicBinder - binding parameter [1] as [INTEGER] - 10
13 20:13:40.710 [http-8080-1] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - found [1002] as column [id3_0_]
14 20:13:40.710 [http-8080-1] TRACE org.hibernate.type.descriptor.sql.BasicExtractor - found [10] as column [post1_7_1_]


如果还想查看查询中命名参数的值,还需要在log4j的配置文件中加上如下的值:
log4j.logger.org.hibernate