Spring学习之声明式事物管理
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 }
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 <!-- <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> 52 <constructor-arg index="0" ref="sqlSessionFactory" /> 53 </bean> --> 54 <bean id="studentDao" class="com.zhengbin.dao.StudentDaoImpl"> 55 <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> 56 </bean> 57 </beans>
2.在mybatis-spring-1.2.1.jar下,声明与实现:
1 package com.zhengbin.dao; 2 3 import java.util.List; 4 5 import org.mybatis.spring.SqlSessionTemplate; 6 7 import com.zhengbin.entity.Student; 8 9 public class StudentDaoImpl implements StudentDao{ 10 11 private SqlSessionTemplate sqlSession; 12 13 public List<Student> selectStudent() { 14 return sqlSession.selectList("com.zhengbin.entity.studentMapper.getStudent"); 15 } 16 public void delStudent(int id){ 17 sqlSession.delete("com.zhengbin.entity.studentMapper.delStudent",id); 18 } 19 public void addStudent(Student student) { 20 sqlSession.insert("com.zhengbin.entity.studentMapper.addStudent", student); 21 } 22 public void updateStudent(Student student){ 23 sqlSession.insert("com.zhengbin.entity.studentMapper.updateStudent",student); 24 } 25 26 public SqlSessionTemplate getSqlSession() { 27 return sqlSession; 28 } 29 public void setSqlSession(SqlSessionTemplate sqlSession) { 30 this.sqlSession = sqlSession; 31 } 32 }
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>
Spring4.1.6+mybatis3.3.0整合所需jar:
1 aopalliance.jar 2 asm-4.2.jar 3 aspectjrt.jar 4 aspectjweaver.jar 5 cglib-3.1.jar 6 commons-logging-1.2.jar 7 log4j-1.2.17.jar 8 log4j-api-2.2.jar 9 log4j-core-2.2.jar 10 mybatis-3.3.0.jar 11 mybatis-spring-1.2.1.jar 12 mysql-connector-java-5.0.6-bin.jar 13 slf4j-api-1.7.12.jar 14 slf4j-log4j12-1.7.12.jar 15 spring-aop-4.1.6.RELEASE.jar 16 spring-aspects-4.1.6.RELEASE.jar 17 spring-beans-4.1.6.RELEASE.jar 18 spring-context-4.1.6.RELEASE.jar 19 spring-context-support-4.1.6.RELEASE.jar 20 spring-core-4.1.6.RELEASE.jar 21 spring-expression-4.1.6.RELEASE.jar 22 spring-instrument-4.1.6.RELEASE.jar 23 spring-instrument-tomcat-4.1.6.RELEASE.jar 24 spring-jdbc-4.1.6.RELEASE.jar 25 spring-jms-4.1.6.RELEASE.jar 26 spring-messaging-4.1.6.RELEASE.jar 27 spring-orm-4.1.6.RELEASE.jar 28 spring-oxm-4.1.6.RELEASE.jar 29 spring-test-4.1.6.RELEASE.jar 30 spring-tx-4.1.6.RELEASE.jar 31 spring-web-4.1.6.RELEASE.jar 32 spring-webmvc-4.1.6.RELEASE.jar 33 spring-webmvc-portlet-4.1.6.RELEASE.jar 34 spring-websocket-4.1.6.RELEASE.jar
梦想要一步步来!