hibernate拦截器demo

拦截器类,拦截器可以拦截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>


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值