Hibenrate控制SQL的输出

邢红瑞 发表于 2005-5-24 10:26:59

hibernate可以打印出sql语句,一般是配置文件加
<property name="show_sql">true</property>
或者直接
SessionFactory sf = new Configuration()
.setProperty("hibernate.show_sql", "true")
// ...
.buildSessionFactory();
其实这是不好的做法,有些服务器使用log4控制日志的例如JBOSS必须修改文件才允许访问System.out,还有就是所有的打印输出混淆在一起。
其实可以使用log4j的配置,
log4j.logger.org.hibernate.SQL=DEBUG, SQL_APPENDER
log4j.additivity.org.hibernate.SQL=false
og4j.properties 只需要放到你的 classpath 中就可以了, 对于 war, 你可以放到 WEB-INF/classes/
log4j.additivity.org.hibernate.SQL=false说明
这个 Logger 的 appender 不继承它的父Logger 是org.hibernate.SQL和 ROOT Logger,
目的是防止jboss那个xml中定义的ROOT Logger和我们这个log4j.properties中定义的Logger互相干扰.
 Beware that appenders are not singletons, they are additive! A category inherits all the appenders from its ancestors also (by default).
If you add an appender to a category and it writes to the same underlying stream (console, same file etc.) as some other appender, the same log message will appear twice (or more) in the log. In addition, if two categories in a hierarchy are configured to use the same appender name, Log4j will write twice to that appender.
Use cat.setAdditivity(false) on a category to disable inheriting of appenders. Then, log messages will only be sent to the appenders specifically configured for that category.
良好格式hibernate sql输出
 <property name="show_sql">true</property>
 <property name="format_sql">true</property>
log4j文件
log4j.logger.org.hibernate.SQL=DEBUG, SQL_APPENDER
#只显示启动信息
log4j.logger.org.hibernate=INFO
# 记录JDBC 绑定参数
log4j.logger.org.hibernate.type=INFO

你可能感兴趣的:(sql,Hibernate,log4j,jboss,jdbc)