1。web.xml 加载你需要的配置文件,eg:data.xml
2.data.xml
在spring的上下文定义
a.定义 dataSource
b.定义sessionFactory 并refer to the pre defined datasource
c.定义hibernatetemplate ref to the predefind sessionFactory
d. 创建GenericDaoHibernate
public class GenericDaoHibernate<T, PK extends Serializable> extends HibernateDaoSupport implements IGenericDao<T, PK>
e: 创建子的hibernatedao 继承 GenericDaoHibernate 并在spring的上下文理声明,在子的hibernateDao 中用getHibernateTemplate().*
f: 定义transactionmanager ref to the pre defined sessionFactory, 并enable声明式的事务 <tx:annotation-driven transaction-manager="txManager" order="3"/>
对于声明式的事务管理可以用AOP方式实现。
<tx:advice id="mcaServiceAdvice" transaction-manager="mcaTxManager">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED" rollback-for="Throwable"/>
<tx:method name="del*" propagation="REQUIRED" rollback-for="Throwable"/>
<tx:method name="update*" propagation="REQUIRED" rollback-for="Throwable"/>
<tx:method name="save*" propagation="REQUIRED" rollback-for="Throwable"/>
<tx:method name="*" read-only="false"/>
</tx:attributes>
</tx:advice>
<aop:config proxy-target-class="true">
<aop:pointcut id="mcaServicePointcut" expression="execution(* com.citi.oprisk.mca.service.*.*(..))"/>
<aop:advisor pointcut-ref="mcaServicePointcut" advice-ref="mcaServiceAdvice"/>
</aop:config>
也可以在具体的需要事务的地方用annotaiton 声明
@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
default-lazy-init="true">
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"
lazy-init="true">
<property name="jndiName">
<value>DataSource</value>
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingLocations">
<list>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop>
<prop key="hibernate.generate_statistics">false</prop>
<prop key="hibernate.query.substitutions">true 1, false 0, yes 'Y', no 'N'</prop>
<!-- prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</prop>
<prop key="hibernate.cache.use_query_cache">false</prop-->
</props>
</property>
</bean>
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory"><ref bean="sessionFactory"/></property>
<property name="cacheQueries" value="true"></property>
</bean>
<!-- Hibernate DAO Def -->
<bean id="genericDaoHibernate" class="com.hibernate.GenericDaoHibernate">
<property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>
<bean id="spaQafRelatedValueDao" class="com.dao.hibernate.SPAScenQafRelatedValueDaoHibernate" parent="genericDaoHibernate"/>
<!-- Transaction Manager Def -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
<property name="nestedTransactionAllowed" value="true"/>
</bean>
<!-- Enable @Transactional support -->
<tx:annotation-driven transaction-manager="txManager" order="3"/>