第四种:使用tx标签配置的拦截器:
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4     xmlns:context="http://www.springframework.org/schema/context"
5     xmlns:aop="http://www.springframework.org/schema/aop"
6     xmlns:tx="http://www.springframework.org/schema/tx"
7     xsi:schemaLocation="http://www.springframework.org/schema/beans
8            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
9            http://www.springframework.org/schema/context
10            http://www.springframework.org/schema/context/spring-context-2.5.xsd
11            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
12            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
13     <!-- 数据源 -->
14     <bean id="dataSource"
15         class="org.apache.commons.dbcp.BasicDataSource"
16         destroy-method="close">
17         <property name="driverClassName" value="com.mysql.jdbc.Driver" />
18         <property name="url"
19             value="jdbc:mysql://192.168.0.244:3306/test?useUnicode=true&amp;characterEncoding=UTF-8" />
20         <property name="username" value="root" />
21         <property name="password" value="root" />
22         <!-- 连接池启动时的初始值 -->
23         <property name="initialSize" value="10" />
24         <!-- 连接池的大值 -->
25         <property name="maxActive" value="10" />
26         <!-- 大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
27         <property name="maxIdle" value="20" />
28         <!--  小空闲值.当空闲的连接数少于阀值时,连接池会预申请去一些连接,以免洪峰来时来不及申请 -->
29         <property name="minIdle" value="10" />
30         <property name="defaultAutoCommit" value="true" />
31     </bean>
32     <!-- 会话工厂 -->
33     <bean id="sessionFactory"
34         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
35         <property name="dataSource" ref="dataSource" />
36         <property name="mappingLocations">
37             <list>
38                 <value>classpath:/com/nms/entity/**/*.hbm.xml</value>
39             </list>
40         </property>
41         <property name="hibernateProperties">
42             <props>
43                 <prop key="hibernate.dialect">
44                     org.hibernate.dialect.MySQL5Dialect
45                 </prop>
46                 <prop key="hibernate.show_sql">true</prop>
47                 <prop key="hibernate.format_sql">true</prop>
48             </props>
49         </property>
50     </bean>
51     <context:annotation-config />
52     <context:component-scan base-package="com.dao" />
53     <!-- 定义事务管理器 -->
54     <bean id="transactionManager"
55         class="org.springframework.orm.hibernate3.HibernateTransactionManager">
56         <property name="sessionFactory" ref="sessionFactory" />
57     </bean>
58     <!-- 定义事务 -->
59     <tx:advice id="txAdvice" transaction-manager="transactionManager">
60         <tx:attributes>
61             <tx:method name="*" propagation="REQUIRED" />
62         </tx:attributes>
63     </tx:advice>
64     <!-- 定义切面 -->
65     <aop:config>
66         <aop:pointcut id="interceptorPointCuts" expression="execution(* com.dao.*.*(..))" />
67         <aop:advisor advice-ref="txAdvice" pointcut-ref="interceptorPointCuts" />
68     </aop:config>
69 </beans>
  第五种:注解:
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4     xmlns:context="http://www.springframework.org/schema/context"
5     xmlns:aop="http://www.springframework.org/schema/aop"
6     xmlns:tx="http://www.springframework.org/schema/tx"
7     xsi:schemaLocation="http://www.springframework.org/schema/beans
8            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
9            http://www.springframework.org/schema/context
10            http://www.springframework.org/schema/context/spring-context-2.5.xsd
11            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
12            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
13     <!-- 数据源 -->
14     <bean id="dataSource"
15         class="org.apache.commons.dbcp.BasicDataSource"
16         destroy-method="close">
17         <property name="driverClassName" value="com.mysql.jdbc.Driver" />
18         <property name="url"
19             value="jdbc:mysql://192.168.0.244:3306/test?useUnicode=true&amp;characterEncoding=UTF-8" />
20         <property name="username" value="root" />
21         <property name="password" value="root" />
22         <!-- 连接池启动时的初始值 -->
23         <property name="initialSize" value="10" />
24         <!-- 连接池的大值 -->
25         <property name="maxActive" value="10" />
26         <!-- 大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
27         <property name="maxIdle" value="20" />
28         <!--  小空闲值.当空闲的连接数少于阀值时,连接池会预申请去一些连接,以免洪峰来时来不及申请 -->
29         <property name="minIdle" value="10" />
30         <property name="defaultAutoCommit" value="true" />
31     </bean>
32     <!-- 会话工厂 -->
33     <bean id="sessionFactory"
34         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
35         <property name="dataSource" ref="dataSource" />
36         <property name="mappingLocations">
37             <list>
38                 <value>classpath:/com/nms/entity/**/*.hbm.xml</value>
39             </list>
40         </property>
41         <property name="hibernateProperties">
42             <props>
43                 <prop key="hibernate.dialect">
44                     org.hibernate.dialect.MySQL5Dialect
45                 </prop>
46                 <prop key="hibernate.show_sql">true</prop>
47                 <prop key="hibernate.format_sql">true</prop>
48             </props>
49         </property>
50     </bean>
51     <context:annotation-config />
52     <!-- 使用注解的包路径 -->
53     <context:component-scan base-package="com.dao" />
54     <!-- 支持  @Transactional 标记 -->
55     <tx:annotation-driven transaction-manager="transactionManager"/>
56     <!-- 定义事务管理器 -->
57     <bean id="transactionManager"
58         class="org.springframework.orm.hibernate3.HibernateTransactionManager">
59         <property name="sessionFactory" ref="sessionFactory" />
60     </bean>
61 </beans>
  如果使用了注解,那么实现类应该这样写:
1 package com.dao.impl;
2 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
3 import org.springframework.stereotype.Component;
4 import org.springframework.transaction.annotation.Transactional;
5 import com.dao.UserDao;
6 @Transactional
7 @Component("userDaoAgency")
8 public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
9     /**
10      * 为方法增加事务处理特性
11      */
12     @Transactional(readOnly=true)
13     public void getUser(){
14     }
15 }
  这样每个方法都能自己定义自己的事务处理!