Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into BBS_User (user_username, user_password, user_authority, user_id) values (?, ?, ?, ?)
于是乎,上网猛搜,力求在console里打印出完整的SQL语句。
1.记得要加入 log4j 的jar包。
2.下面是web的配置
WEB-INF下的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- utf8 start-->
<filter> <filter-name>SetCharacterEncoding</filter-name>
<filter-class>util.SetCharacterEncodingFilter</filter-class>
<init-param> <param-name>encoding</param-name>
<param-value>UTF-8</param-value> </init-param> </filter>
<filter-mapping> <filter-name>SetCharacterEncoding</filter-name>
<servlet-name>/*</servlet-name> </filter-mapping>
<!-- utf8 end-->
<!—index start-->
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<!--index end-->
<!-- log4j的 start-->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<!-- log4j end -->
<!-- spring start-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext_hibernate.xml,
classpath:applicationContext_user.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- spring end-->
<!-- struts start-->
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- struts end-->
</web-app>
3.接下来配置 log4j 的配置文件:log4j.properties,放在Myeclipse的资源目录下。
这是在hibernate给的例子中找出来的,关键在于show sql下的三句:
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### show SQL ###
log4j.logger.org.hibernate.type=TRACE
log4j.logger.org.hibernate.sql=TRACE
### 下面的是错误的!!
###log4j.appender.stdout.Threshold=trace
###log4j.category.org.hibernate.SQL=trace
###log4j.category.org.hibernate.type=trace
### direct messages to file hibernate.log ###
#log4j.appender.file=org.apache.log4j.FileAppender
#log4j.appender.file.File=hibernate.log
#log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j.rootLogger=warn, stdout
#log4j.logger.org.hibernate=info
#log4j.logger.org.hibernate=debug
### log HQL query parser activity
#log4j.logger.org.hibernate.hql.ast.AST=debug
### log just the SQL
#log4j.logger.org.hibernate.SQL=debug
### log JDBC bind parameters ###
log4j.logger.org.hibernate.type=info
#log4j.logger.org.hibernate.type=debug
### log schema export/update ###
log4j.logger.org.hibernate.tool.hbm2ddl=debug
### log HQL parse trees
#log4j.logger.org.hibernate.hql=debug
### log cache activity ###
#log4j.logger.org.hibernate.cache=debug
### log transaction activity
#log4j.logger.org.hibernate.transaction=debug
### log JDBC resource acquisition
#log4j.logger.org.hibernate.jdbc=debug
### enable the following line if you want to track down connection ###
### leakages when using DriverManagerConnectionProvider ###
#log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider=trace
4.加入相应的jar包就可以自动让 Hibernate 的 slf4j 转换为 log4j 输出了:
注意:例如我用的是Hibernate3.6.0,在其目录下 lib/required 中有 slf4j-api-1.6.1.jar 和 slf4j-nop-1.6.1.jar,那就需要下载slf4j-1.6.1(http://www.slf4j.org/download.html),使用其中给出的 slf4j-log4j12-1.6.1.jar替换掉 slf4j-nop-1.6.1.jar,就大功告成了~。
在发行包hibernate-distribution\project\etc下有log4j.properties,将其复制到工程目录下,一般在src(应该紧挨着hibernate.cfg.xml)。"Hibernate 3 is using Simple Logging Facade for Java (SLF4J) (per the docs),if you are relying on Log4j 1.2 you willalso need theslf4j-log4j12-1.5.10.jar if you are wanting tofully configure Hibernate logging with a log4j configuration file. "(Configuring Hibernate logging using Log4j XML config file?)因为Hibernate 3用得是slf4j,因此要想使用log4j配置文件来全面配置Hibernate日志,还需要添加slf4j-log4j12-XX.jar。注意,如果添加了此jar包,需要移除slf4j-nop-XX.jar,否则控制台会有“Multiple bindings were found on the class path ”错误。
5.console下面显示sql的参数类型、参数值以及sql的执行时间!