1     public List<Student> selectStudent() {
2         Student s = new Student();
3         s.setName("zhengbin");
4         s.setScore(109);
5         sqlSession.insert("com.zhengbin.entity.studentMapper.addStudent",s);
6         sqlSession.delete("com.zhengbin.entity.studentMapper.delStudent",6);
7         return sqlSession.selectList("com.zhengbin.entity.studentMapper.getStudent");
8     }

  代码运行的前提是数据库不存在id为6的数据

  1.如果不加入事物管理,则运行的结果是,第五行成功执行,向数据库插入了新数据

  2.如果加入事物管理,则运行的结果是,虽然第五行可以执行成功,但第六行不能成功执行,则第五行的操作会回滚,不会将记录写入数据库

 

声明式事物管理在Spring配置文件beans.xml中的配置:

 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" xmlns:aop="http://www.springframework.org/schema/aop"
 4     xmlns:tx="http://www.springframework.org/schema/tx"
 5     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 6         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 
 7         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
 8 
 9     <!-- 配置数据源 -->
10     <bean id="dataSource"
11         class="org.springframework.jdbc.datasource.DriverManagerDataSource">
12         <property name="driverClassName" value="com.mysql.jdbc.Driver" />
13         <property name="url" value="jdbc:mysql://localhost:3307/student" />
14         <property name="username" value="root" />
15         <property name="password" value="950906" />
16     </bean>
17 
18     <!-- 声明事物配置 开始 -->
19     <!-- 配置事物管理器 -->
20     <bean id="txManager"
21         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
22         <property name="dataSource" ref="dataSource" />
23     </bean>
24     <!-- 配置事物的通知 -->
25     <tx:advice id="txAdvice" transaction-manager="txManager">
26         <tx:attributes>
27             <!-- 配置哪些方法使用什么样的事物,配置事物的传播特性 -->
28             <!-- REQUIRED表示如果不存在事物则必须产生一个事物 -->
29             <tx:method name="*add*" propagation="REQUIRED" />
30             <tx:method name="*insert*" propagation="REQUIRED" />
31             <tx:method name="*update*" propagation="REQUIRED" />
32             <tx:method name="del*" propagation="REQUIRED" />
33             <tx:method name="*select*" read-only="true" />
34             <tx:method name="*" propagation="REQUIRED" />
35         </tx:attributes>
36     </tx:advice>
37 
38     <aop:config>
39         <aop:pointcut id="pointcut"
40             expression="execution(* com.zhengbin.dao.*.*(..))" />
41         <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" />
42     </aop:config>
43     <!-- 声明事物配置 结束 -->
44     
45     
46     <!-- 配置sqlSessionFactory -->
47     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
48         <property name="dataSource" ref="dataSource" />
49         <property name="configLocation" value="classpath:mybatis.cfg.xml" />
50     </bean>
51 
52     <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
53         <constructor-arg index="0" ref="sqlSessionFactory" />
54     </bean>
55     <bean id="studentDao" class="com.zhengbin.dao.StudentDaoImpl">
56         <property name="sqlSession" ref="sqlSessionTemplate"></property>
57     </bean>
58 </beans>

 事务的几种传播特性
1. PROPAGATION_REQUIRED: 如果存在一个事务,则支持当前事务。如果没有事务则开启
2. PROPAGATION_SUPPORTS: 如果存在一个事务,支持当前事务。如果没有事务,则非事务的执行
3. PROPAGATION_MANDATORY: 如果已经存在一个事务,支持当前事务。如果没有一个活动的事务,则抛出异常。
4. PROPAGATION_REQUIRES_NEW: 总是开启一个新的事务。如果一个事务已经存在,则将这个存在的事务挂起。
5. PROPAGATION_NOT_SUPPORTED: 总是非事务地执行,并挂起任何存在的事务。
6. PROPAGATION_NEVER: 总是非事务地执行,如果存在一个活动事务,则抛出异常
7. PROPAGATION_NESTED:如果一个活动的事务存在,则运行在一个嵌套的事务中. 如果没有活动事务, 

 

补充:

  1.在mybatis-spring-1.2.3.jar下,声明与实现:

 1 package com.zhengbin.dao;
 2 
 3 import java.util.List;
 4 
 5 import org.mybatis.spring.support.SqlSessionDaoSupport;
 6 
 7 import com.zhengbin.entity.Student;
 8 
 9 public class StudentDaoImpl extends SqlSessionDaoSupport implements StudentDao{
10     
11     public List<Student> selectStudent() {
12         return getSqlSession().selectList("com.zhengbin.entity.studentMapper.getStudent");
13     }
14     public void delStudent(int id){
15         getSqlSession().delete("com.zhengbin.entity.studentMapper.delStudent",id);
16     }
17     public void addStudent(Student student) {
18         getSqlSession().insert("com.zhengbin.entity.studentMapper.addStudent", student);
19     }
20     public void updateStudent(Student student){
21         getSqlSession().insert("com.zhengbin.entity.studentMapper.updateStudent",student);
22     }
23 }
StudentDaoImpl.java

相关文章: