声明式事务-整合Spring、Hibernate

编程式事务:通过编码的方式,让事务处理的代码侵入到核心的业务代码中。

声明式事务:完成了事务处理的代码和业务核心代码的解耦合。提供事务处理代码的复用性和降低维护成本。

声明式事务:aop最典型的应用。

使用动态代理实现事务的管理:

Jdk: 实现动态是通过实现某个特定的接口(代理类和目标类必须是相同的接口),产生一个虚拟的class文件(代理类的)。必须有接口的实现才能使用jdk完成动态代理

Cglib:动态代理,如果没有接口的实现使用cglib完成动态代理,使用了一个asm框架,完成某个类(目标类)的子类(虚拟的class文件)

Hibernate,spring 使用jdk的动态代理(默认情况),也可以使用cglig

1 新建java项目

进行spring+hibernate的集成:

         使用IOC进行对象管理和注入

         使用AOP进行事务的管理

2加入jar包

         Spring的jar包

         Hibernate的jar包 

         Dbcp的数据库连接池包(c3p0)

3 建立配置文件

         在src目录下:

         Hibernate.cfg.xml

         applicationContext.xml 

hibernate配置文件的内容:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE hibernate-configuration PUBLIC
 3     "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 4     "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
 5 <hibernate-configuration>
 6     <session-factory>
 7         <!-- 数据库的运行环境 -->
 8         <property name="connection.driver_class">
 9             com.mysql.jdbc.Driver
10         </property>
11         <property name="connection.url">
12             jdbc:mysql://localhost:3306/ssh
13         </property>
14         <property name="connection.username">root</property>
15         <property name="connection.password">root</property>
16         <!-- 数据库方言 -->
17         <property name="dialect">
18             org.hibernate.dialect.MySQLDialect
19         </property>
20         <!-- hiberante 其他的属性 -->
21         <property name="show_sql">true</property>
22         <property name="format_sql">true</property>
23         <property name="hbm2ddl.auto">update</property>
24         <!-- 加载映射文件 -->
25         <mapping resource="org/guangsoft/pojo/Student.hbm.xml" />
26     </session-factory>
27 </hibernate-configuration>

4建立Dao接口

1 package org.guangsoft.dao;
2 import org.guangsoft.pojo.Student;
3 /***
4  * StudentDao接口
5  * **/
6 public interface StudentDao
7 {
8     public void addStudent(Student stu);
9 }

5建立Dao接口的实现类

 1 package org.guangsoft.dao.impl;
 2 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
 3 import org.guangsoft.dao.StudentDao;
 4 import org.guangsoft.pojo.Student;
 5 /***
 6  * StudentDao接口的实现类,完成数据库的访问
 7  * HibernateDaoSupport:是spring完成对hibernate的支持
 8  * private HibernateTemplate hibernateTemplate;用来完成数据的crud操作
 9  * 给HibernateDaoSupport注入sessoinFactory
10  * super.getSession();获得hibernate原始的session对象
11  * ***/
12 public class StudentDaoImpl extends HibernateDaoSupport
13 implements StudentDao
14 {
15     /***
16      * 添加学生信息
17      * ***/
18     @Override
19     public void addStudent(Student stu)
20     {
21         super.getHibernateTemplate().save(stu);
22     }
23 }

6 建立service接口

1 package org.guangsoft.service;
2 import org.guangsoft.pojo.Student;
3 /** 建立用户service接口 ***/
4 public interface StudentService
5 {
6     public void saveUser(Student stu);
7 }

7 建立service接口的实现类

 1 package org.guangsoft.service.impl;
 2 import org.guangsoft.dao.StudentDao;
 3 import org.guangsoft.pojo.Student;
 4 import org.guangsoft.service.StudentService;
 5 public class StudentServiceImpl implements StudentService
 6 {
 7     // 声明dao对象
 8     private StudentDao studentDao;
 9     public void setStudentDao(StudentDao studentDao)
10     {
11         this.studentDao = studentDao;
12     }
13     @Override
14     public void saveUser(Student stu)
15     {
16         studentDao.addStudent(stu);
17     }
18 }

8进行spring的配置

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!-- 到入xml文件的约束 -->
 3 <beans xmlns="http://www.springframework.org/schema/beans"
 4     xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
 5     xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
 6     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 7     xsi:schemaLocation="http://www.springframework.org/schema/beans
 8      http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
 9      http://www.springframework.org/schema/aop
10      http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
11      http://www.springframework.org/schema/context
12      http://www.springframework.org/schema/context/spring-context-4.1.xsd
13       http://www.springframework.org/schema/tx
14      http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
15      ">
16     <!-- 实例化一个SessionFactory -->
17     <bean id="sessionFactory"
18         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
19         <!-- 加载hibernate的配置文件 -->
20         <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
21     </bean>
22     <!-- 实例Dao对象 -->
23     <bean id="studentDao" class="org.guangsoft.dao.impl.StudentDaoImpl">
24         <!-- 注入sessionFactory -->
25         <property name="sessionFactory" ref="sessionFactory"></property>
26     </bean>
27     <!-- 实例化service对象 -->
28     <bean id="studentService" class="org.guangsoft.service.impl.StudentServiceImpl">
29         <!-- 注入dao对象 -->
30         <property name="studentDao" ref="studentDao"></property>
31     </bean>
32     <!-- 声明式事务:进行事务的配置 -->
33     <!-- 1 实例化事务管理器对象 -->
34     <bean id="transactionManager"
35         class="org.springframework.orm.hibernate3.HibernateTransactionManager">
36         <!-- 注入sessionFactory -->
37         <property name="sessionFactory" ref="sessionFactory"></property>
38     </bean>
39     <!-- 2 进行事务特征的声明: 切面 id:用来完成切面在其他地方引用 transaction-manager:引用事务管理器对象 -->
40     <tx:advice id="txAdvice" transaction-manager="transactionManager">
41         <tx:attributes>
42             <!-- name:声明的是将哪些命名规范的方法,纳入事务管理 propagation:事务传播机制。 PROPAGATION_REQUIRED: 
43                 如果存在一个事务,则支持当前事务。如果没有事务则开启 isolation:事务的隔离级别,是数据库平台默认的隔离级别 read-only:被刺事务操作的数据是只读的 
44                 rollback-for=:异常的完全限定名,如果发生这样的异常进行回滚 -->
45             <tx:method name="add*" propagation="REQUIRED" isolation="DEFAULT" />
46             <tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT" />
47             <tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT" />
48             <tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT" />
49             <tx:method name="modf*" propagation="REQUIRED" isolation="DEFAULT" />
50             <tx:method name="get*" propagation="REQUIRED" isolation="DEFAULT"
51                 read-only="true" />
52             <tx:method name="select*" propagation="REQUIRED" isolation="DEFAULT"
53                 read-only="true" />
54         </tx:attributes>
55     </tx:advice>
56     <!-- 3进行AOP的配置 -->
57     <aop:config>
58         <!-- 声明切入点 -->
59         <aop:pointcut expression="execution(* org.guangsoft.service.impl.*.*(..))"
60             id="pc" />
61         <!-- 进行织入 -->
62         <aop:advisor advice-ref="txAdvice" pointcut-ref="pc" />
63     </aop:config>
64 </beans>
65  

9添加事务的测试

 1 package org.guangsoft.test;
 2 import org.junit.Test;
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 import org.guangsoft.pojo.Student;
 6 import org.guangsoft.service.StudentService;
 7 public class TransactionTest
 8 {
 9     @Test
10     public void testTransaction()
11     {
12         // 加载spring的配置文件,获得bean容器
13         ApplicationContext ac = new
14         ClassPathXmlApplicationContext("applicationContext.xml");
15         // 获得bean对象
16         StudentService studentService = (StudentService) ac
17                 .getBean("studentService");
18         Student stu = new Student();
19         stu.setSname("小强");
20         studentService.saveUser(stu);
21     }
22 }

1 实例化事务管理器对象

2 声明事务切面(事务的特征)

3 进行aop的配置

原文地址:https://www.cnblogs.com/guanghe/p/6131886.html