修改applicationContext.xml文件,
修改sessionFactory的配置信息。
在hibernateProperties中添加三个属性。
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.use_sql_comments">true</prop>
完整代码如下:
<!--注入SessionFactory实例配置信息-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.use_sql_comments">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/po/Student.hbm.xml</value>
</list>
</property>
</bean>
如果只用了Hibernate框架,则在Hibernate的配置文件中添加这三个属性。具体代码略有不同,但属性名和属性值一样。
执行效果如下图:红框内的是HQL语句,红框后面select开始的是对应的SQL语句。