2015-04-07 17 views
1

我正在使用Hibernate的Spring MVC應用程序。以下是我的調度程序servlet代碼:如何在Spring MVC應用程序中的Hibernate中打印生成的SQL?

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xmlns:tx="http://www.springframework.org/schema/tx" 
xmlns:context="http://www.springframework.org/schema/context" 
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd  
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> 

    <mvc:annotation-driven/> 
    <mvc:resources mapping="/resources/**" location="/resources/" /> 
    <context:component-scan base-package="com.example.abc" /> 
    <tx:annotation-driven/> 
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> 
     <property name="prefix" value="/WEB-INF/jsp/"/> 
     <property name="suffix" value=".jsp"/> 
    </bean> 
    <bean id="jndiDataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> 
     <property name="jndiName" value="java:/MySqlDS" /> 
    </bean> 
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
     <property name="dataSource" ref="jndiDataSource" /> 
     <property name="packagesToScan" value="com.example.abc"></property> 
    </bean> 
    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
     <property name="sessionFactory" ref="sessionFactory"></property> 
    </bean> 
</beans> 

我沒有任何Hibernate配置文件,因爲Hibernate映射在模型類中完成。我需要打印從Hibernate語句生成的SQL。

回答

3

更新SessionFactory的如下:

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
    <property name="dataSource" ref="jndiDataSource" /> 
    <property name="packagesToScan" value="com.example.abc"></property> 
    <property name="hibernateProperties"> 
     <props> 
      <prop key="hibernate.show_sql">true</prop> 
     </props> 
    </property> 
</bean> 
+0

我在sessionFactory中添加了屬性。我現在怎樣才能打印sql? –

+0

不會出現在您的日誌中? –

+0

你可能需要檢查你的日誌級別以及它在這裏建議:http://stackoverflow.com/questions/7074481/hibernate-not-showing-sql-statement-even-with-sql-show-true/7077244 #7077244 –

0

通過使用org.hibernate.SQL的調試日誌級別,可以看到休眠發出的SQL。

更多信息

-1

你可以試試這個

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
     <property name="dataSource" ref="jndiDataSource" /> 
     <property name="packagesToScan" value="com.example.abc"></property> 
     <property name="hibernateProperties"> 
     <props>    
      <prop key="hibernate.show_sql">true</prop>    
     </props> 
    </property> 
    </bean> 

您可以提供其他的Hibernate配置屬性。此外,您可能希望爲這些配置使用屬性文件,並在xml配置中使用$ {}語法注入這些文件。例如,

<prop key="hibernate.show_sql">${hibernate.show_sql}</prop> 
+0

你檢查了已經給出的答案嗎? –

相關問題