Spring学习之声明式事物管理
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学习之声明式事物管理的更多相关文章
- SSH学习——声明式事物管理(Spring)
1.什么是事物? 事务是一组操作的执行单元,相对于数据库操作来讲,事务管理的是一组SQL指令,比如增加,修改,删除等,事务的一致性,要求,这个事务内的操作必须全部执行成功,如果在此过程种出现了差错,比 ...
- Spring—SSJ集成&声明式事务管理
1. 课程介绍 1. SSJ集成;(掌握) 2. 声明式事务管理;(掌握) 什么是三大框架 2.1. ssh Struts/Struts2 Spring Hibernate 2.2. ss ...
- spring+mybatis之声明式事务管理初识(小实例)
前几篇的文章都只是初步学习spring和mybatis框架,所写的实例也都非常简单,所进行的数据访问控制也都很简单,没有加入事务管理.这篇文章将初步接触事务管理. 1.事务管理 理解事务管理之前,先通 ...
- 全面分析 Spring 的编程式事务管理及声明式事务管理
开始之前 关于本教程 本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本 ...
- 全面分析 Spring 的编程式事务管理及声明式事务管理--转
开始之前 关于本教程 本教程将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. 先决条件 本 ...
- Spring编程式事务管理及声明式事务管理
本文将深入讲解 Spring 简单而强大的事务管理功能,包括编程式事务和声明式事务.通过对本教程的学习,您将能够理解 Spring 事务管理的本质,并灵活运用之. Spring 事务属性分析 事务管理 ...
- spring事物配置,声明式事务管理和基于@Transactional注解的使用
http://blog.csdn.net/bao19901210/article/details/41724355 http://www.cnblogs.com/leiOOlei/p/3725911. ...
- Spring中的事物管理,用 @Transactional 注解声明式地管理事务
事物: 事务管理是企业级应用程序开发中必不可少的技术, 用来确保数据的 完整性和 一致性. 事务就是一系列的动作, 它们被当做一个单独的工作单元. 这些动作要么全部完成, 要么全部不起作用 事务的四 ...
- spring 事物(三)—— 声明式事务管理详解
spring的事务处理分为两种: 1.编程式事务:在程序中控制事务开始,执行和提交:详情请点此跳转: 2.声明式事务:在Spring配置文件中对事务进行配置,无须在程序中写代码:(建议使用) 我对&q ...
随机推荐
- MySql修改默认端口号,修改my.ini的端口号
MySql默认端口号为3306,如果安装多个或者冲突需要修改端口号,修改my.ini的端口号就可以了,文件一般情况下在安装目录下.下面是具体说明: 方法/步骤 先在服务里停止mysql的服务器,再找到 ...
- spoj 1108
要求输出一个牌的顺序 使每隔1.2.......n翻牌后出现1 2 3 4 5 6 7 8 9 .... n 将牌想象成n个空格 正向推 空n个位置放n 循环 需优化 #include <io ...
- Java学习第二篇:类,对象,成员属性,成员方法,构造方法,类变量,类方法
一.类的定义 一个全面的类定义是比较复杂的, 定义如下:
- 转贴: A Simple c# Wrapper for ffMpeg
原帖地址:http://jasonjano.wordpress.com/2010/02/09/a-simple-c-wrapper-for-ffmpeg/ A Simple c# Wrapper fo ...
- darwin转发时,摄像机在3G和4G模式下的参数设置
darwin转发时,摄像机在3G和4G模式下的参数设置 我们转发的是摄像机的子码流,因为在不同的网络环境下,为了达到当前网络环境下最清晰,最流畅的目标,在转发前要根据使用的是3G还是4G及信号强度来自 ...
- swift:自动引用计数ARC
Swift自动引用计数:ARC 原文链接:https://numbbbbb.gitbooks.io/-the-swift-programming-language-/content/chapte ...
- ubuntu 映射网络驱动器到本地
公司办公都是在ubuntu服务器上,所以每每拷贝修改文件都要ftp之类的,实在不方便. 索性将服务器挂载到自己本地目录下. 服务器端参考其他samba安装和配置.这里只是说下本地自动挂载方法. 一.首 ...
- [Quick-x]cocos2dx下的彩色文本显示--RichLabel
部分关键代码与思路参考 http://www.cocoachina.com/bbs/read.php?tid=218977&page=1 感谢原作者 i7909 代码下载地址:https:// ...
- 协同滤波 Collaborative filtering 《推荐系统实践》 第二章
利用用户行为数据 简介: 用户在网站上最简单存在形式就是日志. 原始日志(raw log)------>会话日志(session log)-->展示日志或点击日志 用户行一般分为两种: 1 ...
- SQL性能优化之一
1: 如果确定要用到删除全表 delete from tablename commit 这种方式是不可取的,因这样会产生大量的redo-log,一般建议用truncate或是临时表(这样可以告诉ora ...