Hibernate displays the SQL statement parameters in the console ~~

Source: Internet
Author: User

When using SSH2 for course design, Hibernate is used for database addition. Because no parameters are visible, there are a lot of SQL statements printed by hibernate ?, I mistakenly thought it was a problem of data transmission.

Hibernate: Select hibernate_sequence.nextval from dual
Hibernate: insert into bbs_user (user_username, user_password, user_authority, user_id) values (?, ?, ?, ?)

As a result, you can search online and try to print the complete SQL statement on the console.

This should start from the beginning, because I found the log4j configuration method on the Internet, so I will not innovate (a little bit despise myself ...) Configure log4j first:

Add the jar package of log4j.

Web. xml under the WEB-INF:

<?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>
 

Next, configure the log4j configuration file: log4j. properties, which is placed in the Resource Directory of myeclipse.

This is found in the example given by hibernate. The key lies in the three sentences in 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.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
 

Add the corresponding jar package to automatically convert hibernate slf4j to log4j output:

For example, I used hibernate3.6.0, in its directory lib/required has a slf4j-api-1.6.1.jar and slf4j-nop-1.6.1.jar, then you need to download the slf4j-1.6.1 (http://www.slf4j.org/download.html), use the given slf4j-log4j12-1.6.1.jar to replace the slf4j-nop-1.6.1.jar, that's all ~.

Figure:

References:

Http://hi.baidu.com/yych13/blog/item/ef4d06d4bdb4470ea18bb793.html

Http://unmi.cc/hibernate-log-sql-log4j-slf4j

Http://houwen.iteye.com/blog/446721

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.