Spring整合Hibernate声明事务管理

DAO

View Code
1 package org.xiong.spring.dao;
2 
3 public interface DAO<T>
4 {
5     public void add(T u);
6 }

UserDAOImpl.java

View Code
 1 package org.xiong.spring.dao.impl;
 2 
 3 import javax.annotation.Resource;
 4 
 5 import org.hibernate.Session;
 6 import org.hibernate.SessionFactory;
 7 import org.hibernate.Transaction;
 8 import org.springframework.stereotype.Component;
 9 import org.xiong.spring.dao.DAO;
10 import org.xiong.spring.model.User;
11 
12 @Component("userDao")
13 public class UserDAOImpl implements DAO<User>
14 {
15     private SessionFactory sessionFactory;
16 
17     public SessionFactory getSessionFactory()
18     {
19         return sessionFactory;
20     }
21 
22     @Resource(name = "mySessionFactory")
23     public void setSessionFactory(SessionFactory sessionFactory)
24     {
25         this.sessionFactory = sessionFactory;
26     }
27 
28     @Override
29     public void add(User u)
30     {
31         Session session = sessionFactory.getCurrentSession();
32         
33         session.save(u);
34         System.out.println(u.getName() + "被存储至数据库!");
35             }
36 
37 }

UserLogDAOImpl.java

View Code
 1 package org.xiong.spring.dao.impl;
 2 
 3 import java.sql.SQLException;
 4 import java.text.SimpleDateFormat;
 5 
 6 import javax.annotation.Resource;
 7 import javax.sql.DataSource;
 8 
 9 import org.hibernate.Session;
10 import org.hibernate.SessionFactory;
11 import org.hibernate.Transaction;
12 import org.springframework.stereotype.Component;
13 import org.xiong.spring.dao.DAO;
14 import org.xiong.spring.model.User;
15 import org.xiong.spring.model.UserLog;
16 
17 @Component("userLogDao")
18 public class UserLogDAOImpl implements DAO<UserLog>
19 {
20     private SessionFactory sessionFactory;
21 
22     public SessionFactory getSessionFactory()
23     {
24         return sessionFactory;
25     }
26 
27     @Resource(name = "mySessionFactory")
28     public void setSessionFactory(SessionFactory sessionFactory)
29     {
30         this.sessionFactory = sessionFactory;
31     }
32 
33     @Override
34     public void add(UserLog u)
35     {
36         Session session=sessionFactory.getCurrentSession();
37         session.save(u);
38         System.out.println("==="+new SimpleDateFormat("yyyy:MM:dd HH:mm:ss").format(u.getLoggingDate())+"===");
39         
40     }
41 
42 }


User.java

View Code
 1 package org.xiong.spring.model;
 2 
 3 import javax.persistence.Entity;
 4 import javax.persistence.GeneratedValue;
 5 import javax.persistence.Id;
 6 import javax.persistence.Table;
 7 
 8 @Entity
 9 @Table(name="SHUser")
10 public class User
11 {
12     private int id;
13     private String name;
14 
15     @Id
16     @GeneratedValue
17     public int getId()
18     {
19         return id;
20     }
21 
22     public void setId(int id)
23     {
24         this.id = id;
25     }
26 
27     public String getName()
28     {
29         return name;
30     }
31 
32     public void setName(String name)
33     {
34         this.name = name;
35     }
36 
37 }

UserLog.java

View Code
 1 package org.xiong.spring.model;
 2 
 3 import java.util.Date;
 4 
 5 import javax.persistence.Entity;
 6 import javax.persistence.GeneratedValue;
 7 import javax.persistence.Id;
 8 import javax.persistence.Table;
 9 
10 @Entity
11 public class UserLog
12 {
13     private int id;
14     private Date loggingDate;
15 
16     @Id
17     @GeneratedValue
18     public int getId()
19     {
20         return id;
21     }
22 
23     public void setId(int id)
24     {
25         this.id = id;
26     }
27 
28     public Date getLoggingDate()
29     {
30         return loggingDate;
31     }
32 
33     public void setLoggingDate(Date loggingDate)
34     {
35         this.loggingDate = loggingDate;
36     }
37 
38 }

方式一:Annotation方式配置@Transactional

UserManager.java

View Code
 1 package org.xiong.spring.service;
 2 
 3 import java.util.Date;
 4 
 5 import javax.annotation.Resource;
 6 
 7 import org.springframework.stereotype.Component;
 8 import org.springframework.transaction.annotation.Isolation;
 9 import org.springframework.transaction.annotation.Propagation;
10 import org.springframework.transaction.annotation.Transactional;
11 import org.xiong.spring.dao.DAO;
12 import org.xiong.spring.model.User;
13 import org.xiong.spring.model.UserLog;
14 
15 @Component("userMagager")
16 public class UserManager
17 {
18     private DAO<User> userDao;
19     private DAO<UserLog> userLogDao;
20 
21     public DAO<UserLog> getUserLogDao()
22     {
23         return userLogDao;
24     }
25 
26     @Resource(name = "userLogDao")
27     public void setUserLogDao(DAO<UserLog> userLogDao)
28     {
29         this.userLogDao = userLogDao;
30     }
31 
32     public DAO<User> getUserDao()
33     {
34         return userDao;
35     }
36 
37     @Resource(name = "userDao")
38     public void setUserDao(DAO<User> userDao)
39     {
40         this.userDao = userDao;
41     }
42 
43     @Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.READ_COMMITTED)
44     public void save(User u)
45     {
46         UserLog uLog = new UserLog();
47         this.userDao.add(u);
48         uLog.setLoggingDate(new Date());
49         this.userLogDao.add(uLog);
50     }
51 } 

applicationContext.xml

View Code
 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     xmlns:context="http://www.springframework.org/schema/context"
 6     xmlns:tx="http://www.springframework.org/schema/tx"
 7     xmlns:aop="http://www.springframework.org/schema/aop"
 8     xmlns:p="http://www.springframework.org/schema/p"
 9     xsi:schemaLocation="http://www.springframework.org/schema/beans 
10                         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
11                         http://www.springframework.org/schema/context 
12                         http://www.springframework.org/schema/context/spring-context-3.0.xsd
13                         http://www.springframework.org/schema/tx 
14                          http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
15                         http://www.springframework.org/schema/aop 
16                         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
17         
18     <context:annotation-config/>
19     <context:component-scan base-package="org.xiong.spring"></context:component-scan>
20     
21     <!-- DataSource -->    
22     <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"    destroy-method="close">
23         <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
24         <property name="url" value="jdbc:oracle:thin:@localhost:1521:study" />
25         <property name="username" value="scott" />
26         <property name="password" value="tiger" />
27     </bean>
28     
29     <!-- SessionFactory -->    
30     <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
31         <property name="dataSource" ref="myDataSource"/>
32         <property name="annotatedClasses">
33           <list>
34             <value>org.xiong.spring.model.User</value>
35             <value>org.xiong.spring.model.UserLog</value>
36           </list>
37         </property>
38         <property name="hibernateProperties">
39             <props>
40               <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
41               <!-- <prop key="hibernate.current_session_context_class">thread</prop> -->
42               <prop key="hibernate.show_sql">true</prop>
43               <prop key="hibernate.format_sql">true</prop>
44               <prop key="hibernate.hbm2ddl.auto">update</prop>
45              </props>
46         </property>
47       </bean>
48   
49   <!-- Annotation Transaction -->
50     <tx:annotation-driven transaction-manager="transactionManager"/>
51     
52     <bean id="transactionManager"    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
53         <property name="sessionFactory" ref="mySessionFactory" />
54     </bean>     
55 </beans>

方式二:XML配置advisor

View Code
 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     xmlns:context="http://www.springframework.org/schema/context"
 6     xmlns:tx="http://www.springframework.org/schema/tx"
 7     xmlns:aop="http://www.springframework.org/schema/aop"
 8     xmlns:p="http://www.springframework.org/schema/p"
 9     xsi:schemaLocation="http://www.springframework.org/schema/beans 
10                         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
11                         http://www.springframework.org/schema/context 
12                         http://www.springframework.org/schema/context/spring-context-3.0.xsd
13                         http://www.springframework.org/schema/tx 
14                      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
15                         http://www.springframework.org/schema/aop 
16                         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
17         
18     <context:annotation-config/>
19     <context:component-scan base-package="org.xiong.spring"></context:component-scan>
20     
21     <!-- DataSource -->    
22     <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"    destroy-method="close">
23         <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
24         <property name="url" value="jdbc:oracle:thin:@localhost:1521:study" />
25         <property name="username" value="scott" />
26         <property name="password" value="tiger" />
27     </bean>
28     
29     <!-- SessionFactory-->
30     <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
31       <property name="dataSource" ref="myDataSource"/>
32         <property name="annotatedClasses">
33           <list>
34             <value>org.xiong.spring.model.User</value>
35             <value>org.xiong.spring.model.UserLog</value>
36           </list>
37         </property>
38         <property name="hibernateProperties">
39                 <props>
40                   <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
41                   <!-- <prop key="hibernate.current_session_context_class">thread</prop> -->
42                   <prop key="hibernate.show_sql">true</prop>
43                   <prop key="hibernate.format_sql">true</prop>
44                   <prop key="hibernate.hbm2ddl.auto">update</prop>
45                  </props>
46       </property>
47   </bean>
48   
49 
50     <!-- XML Configuration TransactionManagent-->
51     <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
52         <property name="sessionFactory" ref="mySessionFactory" />
53     </bean>
54     
55     <aop:config>
56         <aop:pointcut expression="execution(public * org.xiong.spring..*.save(..))" id="servicePoinitcut"/>
57         <aop:advisor advice-ref="txAdvice" pointcut-ref="servicePoinitcut"/>
58     </aop:config>
59     
60      <tx:advice id="txAdvice" transaction-manager="transactionManager">
61         <tx:attributes>
62           <tx:method name="get*" read-only="true"/>
63           <tx:method name="save*" propagation="REQUIRED"/>
64         </tx:attributes>
65   </tx:advice>
66 </beans>
原文地址:https://www.cnblogs.com/xiongyu/p/2494310.html