拦截器类,拦截器可以拦截sql语句,有必要时,可以对sql进行修改。
package com.csair.interceptor;
import org.hibernate.EmptyInterceptor;
@SuppressWarnings("serial")
public class HibermateInterceptor extends EmptyInterceptor {
public String onPrepareStatement(String sql) {
System.out.println("onPrepareStatement");
return sql;
}
}
spring配置文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.csair.dao"/>
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource_jdbc" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</bean>
<bean id="hibermateInterceptor" class="com.csair.interceptor.HibermateInterceptor"/>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="dataSource_jdbc"/>
</property>
<property name="entityInterceptor" ref="hibermateInterceptor"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">false</prop>
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
<prop key="hibernate.cache.provider_configuration_file_resource_path">classpath:cache/ehcache.xml</prop><!-- hibernate have a default xml -->
</props>
</property>
<!-- <property name="mappingLocations">
<list>
<value>hibernate_mapping/*</value>
</list>
</property> -->
<property name="annotatedClasses">
<list>
<value>com.csair.dao.UserAnotation</value>
<value>com.csair.excel.SmSubBiz</value>
</list>
</property>
</bean>
<bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>
<!-- <tx:annotation-driven transaction-manager="hibernateTransactionManager"/> -->
<tx:advice id="advice" transaction-manager="hibernateTransactionManager">
<tx:attributes>
<tx:method name="query*" propagation="REQUIRED"/>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut expression="execution(* com.csair.dao.**.*(..))" id="pointcut"/>
<aop:advisor advice-ref="advice" pointcut-ref="pointcut"/>
</aop:config>
<bean id="hibernateDemo" class="com.csair.dao.HibernateDemo">
<!-- <property name="sessionFactory">
<ref local="sessionFactory"/>
</property> -->
</bean>
<!-- <bean id="parseExcel" class="com.csair.excel.ParseExcel">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean> -->
</beans>