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

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

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

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

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

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 配置数据源 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3307/student" />
<property name="username" value="root" />
<property name="password" value="950906" />
</bean> <!-- 声明事物配置 开始 -->
<!-- 配置事物管理器 -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置事物的通知 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<!-- 配置哪些方法使用什么样的事物,配置事物的传播特性 -->
<!-- REQUIRED表示如果不存在事物则必须产生一个事物 -->
<tx:method name="*add*" propagation="REQUIRED" />
<tx:method name="*insert*" propagation="REQUIRED" />
<tx:method name="*update*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="*select*" read-only="true" />
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice> <aop:config>
<aop:pointcut id="pointcut"
expression="execution(* com.zhengbin.dao.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" />
</aop:config>
<!-- 声明事物配置 结束 --> <!-- 配置sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis.cfg.xml" />
</bean> <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
<bean id="studentDao" class="com.zhengbin.dao.StudentDaoImpl">
<property name="sqlSession" ref="sqlSessionTemplate"></property>
</bean>
</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下,声明与实现:

 package com.zhengbin.dao;

 import java.util.List;

 import org.mybatis.spring.support.SqlSessionDaoSupport;

 import com.zhengbin.entity.Student;

 public class StudentDaoImpl extends SqlSessionDaoSupport implements StudentDao{

     public List<Student> selectStudent() {
return getSqlSession().selectList("com.zhengbin.entity.studentMapper.getStudent");
}
public void delStudent(int id){
getSqlSession().delete("com.zhengbin.entity.studentMapper.delStudent",id);
}
public void addStudent(Student student) {
getSqlSession().insert("com.zhengbin.entity.studentMapper.addStudent", student);
}
public void updateStudent(Student student){
getSqlSession().insert("com.zhengbin.entity.studentMapper.updateStudent",student);
}
}

StudentDaoImpl.java

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 配置数据源 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3307/student" />
<property name="username" value="root" />
<property name="password" value="950906" />
</bean> <!-- 声明事物配置 开始 -->
<!-- 配置事物管理器 -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置事物的通知 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<!-- 配置哪些方法使用什么样的事物,配置事物的传播特性 -->
<!-- REQUIRED表示如果不存在事物则必须产生一个事物 -->
<tx:method name="*add*" propagation="REQUIRED" />
<tx:method name="*insert*" propagation="REQUIRED" />
<tx:method name="*update*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="*select*" read-only="true" />
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice> <aop:config>
<aop:pointcut id="pointcut"
expression="execution(* com.zhengbin.dao.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" />
</aop:config>
<!-- 声明事物配置 结束 --> <!-- 配置sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis.cfg.xml" />
</bean>
<!-- <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean> -->
<bean id="studentDao" class="com.zhengbin.dao.StudentDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
</beans>

beans.xml

  2.在mybatis-spring-1.2.1.jar下,声明与实现:

 package com.zhengbin.dao;

 import java.util.List;

 import org.mybatis.spring.SqlSessionTemplate;

 import com.zhengbin.entity.Student;

 public class StudentDaoImpl implements StudentDao{

     private SqlSessionTemplate sqlSession;

     public List<Student> selectStudent() {
return sqlSession.selectList("com.zhengbin.entity.studentMapper.getStudent");
}
public void delStudent(int id){
sqlSession.delete("com.zhengbin.entity.studentMapper.delStudent",id);
}
public void addStudent(Student student) {
sqlSession.insert("com.zhengbin.entity.studentMapper.addStudent", student);
}
public void updateStudent(Student student){
sqlSession.insert("com.zhengbin.entity.studentMapper.updateStudent",student);
} public SqlSessionTemplate getSqlSession() {
return sqlSession;
}
public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}
}

StudentDaoImpl.java

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 配置数据源 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3307/student" />
<property name="username" value="root" />
<property name="password" value="950906" />
</bean> <!-- 声明事物配置 开始 -->
<!-- 配置事物管理器 -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置事物的通知 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<!-- 配置哪些方法使用什么样的事物,配置事物的传播特性 -->
<!-- REQUIRED表示如果不存在事物则必须产生一个事物 -->
<tx:method name="*add*" propagation="REQUIRED" />
<tx:method name="*insert*" propagation="REQUIRED" />
<tx:method name="*update*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="*select*" read-only="true" />
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice> <aop:config>
<aop:pointcut id="pointcut"
expression="execution(* com.zhengbin.dao.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut" />
</aop:config>
<!-- 声明事物配置 结束 --> <!-- 配置sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis.cfg.xml" />
</bean> <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
<bean id="studentDao" class="com.zhengbin.dao.StudentDaoImpl">
<property name="sqlSession" ref="sqlSessionTemplate"></property>
</bean>
</beans>

beans.xml

Spring4.1.6+mybatis3.3.0整合所需jar:

 aopalliance.jar
asm-4.2.jar
aspectjrt.jar
aspectjweaver.jar
cglib-3.1.jar
commons-logging-1.2.jar
log4j-1.2.17.jar
log4j-api-2.2.jar
log4j-core-2.2.jar
mybatis-3.3.0.jar
mybatis-spring-1.2.1.jar
mysql-connector-java-5.0.6-bin.jar
slf4j-api-1.7.12.jar
slf4j-log4j12-1.7.12.jar
spring-aop-4.1.6.RELEASE.jar
spring-aspects-4.1.6.RELEASE.jar
spring-beans-4.1.6.RELEASE.jar
spring-context-4.1.6.RELEASE.jar
spring-context-support-4.1.6.RELEASE.jar
spring-core-4.1.6.RELEASE.jar
spring-expression-4.1.6.RELEASE.jar
spring-instrument-4.1.6.RELEASE.jar
spring-instrument-tomcat-4.1.6.RELEASE.jar
spring-jdbc-4.1.6.RELEASE.jar
spring-jms-4.1.6.RELEASE.jar
spring-messaging-4.1.6.RELEASE.jar
spring-orm-4.1.6.RELEASE.jar
spring-oxm-4.1.6.RELEASE.jar
spring-test-4.1.6.RELEASE.jar
spring-tx-4.1.6.RELEASE.jar
spring-web-4.1.6.RELEASE.jar
spring-webmvc-4.1.6.RELEASE.jar
spring-webmvc-portlet-4.1.6.RELEASE.jar
spring-websocket-4.1.6.RELEASE.jar

jarlist

Spring学习之声明式事物管理的更多相关文章

  1. SSH学习——声明式事物管理(Spring)

    1.什么是事物? 事务是一组操作的执行单元,相对于数据库操作来讲,事务管理的是一组SQL指令,比如增加,修改,删除等,事务的一致性,要求,这个事务内的操作必须全部执行成功,如果在此过程种出现了差错,比 ...

  2. Spring—SSJ集成&声明式事务管理

    1.   课程介绍 1.  SSJ集成;(掌握) 2.  声明式事务管理;(掌握) 什么是三大框架 2.1.  ssh Struts/Struts2 Spring Hibernate 2.2.  ss ...

  3. spring+mybatis之声明式事务管理初识(小实例)

    前几篇的文章都只是初步学习spring和mybatis框架,所写的实例也都非常简单,所进行的数据访问控制也都很简单,没有加入事务管理.这篇文章将初步接触事务管理. 1.事务管理 理解事务管理之前,先通 ...

  4. 全面分析 Spring 的编程式事务管理及声明式事务管理

    开始之前 关于本教程 本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本 ...

  5. 全面分析 Spring 的编程式事务管理及声明式事务管理--转

    开始之前 关于本教程 本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本 ...

  6. Spring编程式事务管理及声明式事务管理

    本文将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. Spring 事务属性分析 事务管理 ...

  7. spring事物配置,声明式事务管理和基于@Transactional注解的使用

    http://blog.csdn.net/bao19901210/article/details/41724355 http://www.cnblogs.com/leiOOlei/p/3725911. ...

  8. Spring中的事物管理,用 @Transactional 注解声明式地管理事务

    事物: 事务管理是企业级应用程序开发中必不可少的技术,  用来确保数据的 完整性和 一致性. 事务就是一系列的动作, 它们被当做一个单独的工作单元. 这些动作要么全部完成, 要么全部不起作用 事务的四 ...

  9. spring 事物(三)—— 声明式事务管理详解

    spring的事务处理分为两种: 1.编程式事务:在程序中控制事务开始,执行和提交:详情请点此跳转: 2.声明式事务:在Spring配置文件中对事务进行配置,无须在程序中写代码:(建议使用) 我对&q ...

随机推荐

  1. 帝国cms如何调用栏目别名作为分类标题?[!--classname--]标签不能用

    用帝国cms建站安全性和生成速度会比dedecms好些,但ecms有个比较不方便的地方就是后台默认模板栏目那边没有一个seo标题设置的输入框,列表模板用的是[!--pagetitle--]标签,那么分 ...

  2. HTTP/2 对 Web 性能的影响(上)

    一.前言 HTTP/2 于 2015 年 5 月正式推出.自诞生以来,它就一直在影响着网络性能最佳实践.在本篇文章中,我们将讨论 HTTP/2 的二进制帧.延迟削减.潜在利弊以及相应的应对措施. 超文 ...

  3. 一个Java程序员应该掌握的10项技能

    1.语法:必须比较熟悉,在写代码的时候IDE的编辑器对某一行报错应该能够根据报错信息知道是什么样的语法错误并且知道任何修正. 2.命令:必须熟悉JDK带的一些常用命令及其常用选项,命令至少需要熟悉:a ...

  4. POJ 3045

    Cow Acrobats Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2302   Accepted: 912 Descr ...

  5. hdu 4686 Arc of Dream

    思路:构造矩阵 a[i]*b[i]=ax*bx*a[i-1]*b[i-1]+ax*by*a[i-1]+ay*bx*b[i-1]+ay*by 代码如下: #include<iostream> ...

  6. hdu1151 Air Raid

    http://acm.hdu.edu.cn/showproblem.php?pid=1151 增广路的变种2:DAG图的最小路径覆盖=定点数-最大匹配数 #include<iostream> ...

  7. hdu 3544 Alice's Game

    #include<stdio.h> int main() { int t,n; __int64 sum1,sum2; int i,j,a,b; scanf("%d",& ...

  8. Sina App Engine(SAE)教程(11)- Yaf使用

    Yaf参考资料 Yaf(Yet Another Framework)用户手册 想在SAE使用Yaf? 无需申请,sae环境已经全面支持. Yaf 实战 下面是一个运行在SAE的Yaf的hello wo ...

  9. React测试Mixin

    1.test.jsx var randomNumberMixin = require("./randomNumberMixin.jsx"); describe("test ...

  10. mysql: java.sql.SQLException: Incorrect string value: '\xF0\x9F\x92\x90</...'

    插入数据出现问题,因为包含了特殊字符. 现象: 插入的数据中如果含有某些特殊字符,会导致插入数据失败,例如字符串”测试**插入数据...“,在console中insert是正常的,但是使用java代码 ...