1、hibernate.properties
2、applicationContext.xml
1
hibernate.dialect=org.hibernate.dialect.MySQLDialect
2
hibernate.driverClassName=com.mysql.jdbc.Driver
3
hibernate.url=jdbc:mysql://127.0.0.1:3306/test
4
hibernate.username=root
5
hibernate.password=sa
6
hibernate.showSQL=true
7
hibernate.maxActive=50
8
hibernate.maxIdle=30
9
hibernate.maxWait=1000

2

3

4

5

6

7

8

9

2、applicationContext.xml
1
<?xml version="1.0" encoding="UTF-8"?>
2
<beans
3
xmlns="http://www.springframework.org/schema/beans"
4
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
6
7
<!-- 讀入屬性文件 -->
8
<bean id="propertyConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
9
<property name="locations">
10
<list>
11
<value>classpath:hibernate.properties</value>
12
</list>
13
</property>
14
</bean>
15
16
<!-- 配置數(shù)據(jù)源,可以其他方式 -->
17
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
18
<property name="driverClassName" value="${hibernate.driverClassName}" />
19
<property name="url" value="${hibernate.url}" />
20
<property name="username" value="${hibernate.username}" />
21
<property name="password" value="${hibernate.password}" />
22
<property name="maxActive" value="${hibernate.maxActive}" />
23
<property name="maxIdle" value="${hibernate.maxIdle}" />
24
<property name="maxWait" value="${hibernate.maxWait}" />
25
</bean>
26
27
<!-- 配置Hibernate的Session工廠,注入數(shù)據(jù)源、映射文件 -->
28
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
29
<property name="dataSource">
30
<ref local="dataSource"/>
31
</property>
32
<property name="mappingResources">
33
<list>
34
<value>com/sailor/test/dao/Employee.hbm.xml</value>
35
</list>
36
</property>
37
<property name="hibernateProperties">
38
<props>
39
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
40
<prop key="hibernate.show_sql">${hibernate.showSQL}</prop>
41
</props>
42
</property>
43
</bean>
44
45
46
<!-- 定義事務(wù)管理器,使用適用于Hibernte的事務(wù)管理器-->
47
<bean id="transactionManager"
48
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
49
<!-- HibernateTransactionManager bean需要依賴注入一個(gè)SessionFactory bean的引用-->
50
<property name="sessionFactory">
51
<ref local="sessionFactory" />
52
</property>
53
</bean>
54
55
<!-- 配置事務(wù)攔截器-->
56
<bean id="transactionInterceptor"
57
class="org.springframework.transaction.interceptor.TransactionInterceptor">
58
<!-- 事務(wù)攔截器bean需要依賴注入一個(gè)事務(wù)管理器 -->
59
<property name="transactionManager" ref="transactionManager" />
60
<property name="transactionAttributes">
61
<!-- 下面定義事務(wù)傳播屬性-->
62
<props>
63
<!-- 所有以save開頭的方法,采用required的事務(wù)策略-->
64
<prop key="save*">PROPAGATION_REQUIRED</prop>
65
<!-- 所有以mod開頭的方法,采用required的事務(wù)策略-->
66
<prop key="mod*">PROPAGATION_REQUIRED</prop>
67
<!-- 所有以del開頭的方法,采用required的事務(wù)策略-->
68
<prop key="del*">PROPAGATION_REQUIRED</prop>
69
<!-- 其他方法,readOnly -->
70
<prop key="*">readOnly</prop>
71
</props>
72
</property>
73
</bean>
74
75
<!-- 定義BeanNameAutoProxyCreator,該bean是個(gè)bean后處理器,無需被引用,因此沒有id屬性
76
這個(gè)bean后處理器,根據(jù)事務(wù)攔截器為目標(biāo)bean自動(dòng)創(chuàng)建事務(wù)代理 -->
77
<bean
78
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
79
<!--指定對(duì)滿足哪些bean name的bean自動(dòng)生成業(yè)務(wù)代理 -->
80
<property name="beanNames">
81
<!-- 下面是所有需要自動(dòng)創(chuàng)建事務(wù)代理的bean-->
82
<list>
83
<value>employeeService</value>
84
</list>
85
<!-- 此處可增加其他需要自動(dòng)創(chuàng)建事務(wù)代理的bean-->
86
</property>
87
<!-- 下面定義BeanNameAutoProxyCreator所需的事務(wù)攔截器-->
88
<property name="interceptorNames">
89
<list>
90
<value>transactionInterceptor</value>
91
<!-- 此處可增加其他新的Interceptor -->
92
</list>
93
</property>
94
</bean>
95
96
<!-- dao層 -->
97
<bean id="employeeDAO" class="com.sailor.test.dao.EmployeeDAO">
98
<property name="sessionFactory" ref="sessionFactory"></property>
99
</bean>
100
101
<!-- service層 -->
102
<bean id="employeeService" class="com.sailor.test.service.impl.EmployeeServiceImpl">
103
<property name="employeeDAO" ref="employeeDAO" />
104
</bean>
105
106
</beans>
源代碼:/Files/sailor/spring_hibernate_transaction1.rar

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106
