Spring声明式事务管理示例——MyBatis学习笔记之十六

Spring

首先写一个DAO类,代码如下:

package com.abc.dao;import com.abc.mapper.StudentMapper;import com.abc.domain.Student;public class StudentDao {private StudentMapper studentMapper;//studentMapper的setter和getter方法public void setStudentMapper(StudentMapper studentMapper){this.studentMapper = studentMapper;}public StudentMapper getStudentMapper(){return this.studentMapper;}public void save(Student student){this.studentMapper.add(student);int a = 1 / 0;}}

在此类中,有一个save方法用于保存学生,当然也可以添加其他方法。

接下来,我们要在Spring中应用AOP此类的方法提供事务管理,其配置如下(beans.xml):

<?xml version=”1.0″ encoding=”utf8″?><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”xmlns:context=”http://www.springframework.org/schema/context”xsi:schemaLocation=” http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/jee/spring-jee-3.0.xsd http://www.springframework.org/schema/tx/spring-tx-3.0.xsd”default-autowire=”byName” default-lazy-init=”false”><!–本示例采用DBCP连接池,应预先把DBCP的jar包复制到工程的lib目录下。连接池配置如下–><bean><property value=”com.mysql.jdbc.Driver”/><propertyvalue=”jdbc:mysql://localhost/courseman”/><property value=”courseman”/><property value=”abc123″/></bean><bean><!–dataSource属性指定要用到的连接池–><property ref=”dataSource”/><!–configLocation属性指定mybatis的核心配置文件–><property value=”resources/configuration.xml”/></bean><bean><property ref=”dataSource” /></bean><beanabstract=”true”><!–sqlSessionFactory属性指定要用到的SqlSessionFactory实例–><property ref=”sqlSessionFactory” /></bean><bean parent=”parentMapper”><!–mapperInterface属性指定映射器接口,服务器空间,用于实现此接口并生成映射器对象–><property value=”com.abc.mapper.StudentMapper” /></bean><!–为studentDao注入studentMapper–><bean><property ref=”studentMapper” /></bean><!–配置事务处理策略,香港服务器,transaction-manager属性指定事务管理器。若事务管理器bean的id即为transactionManager,则transaction-manager的属性可以不指定–><tx:advice transaction-manager=”transactionManager”><tx:attributes><!–所有以find开头的方法都是只读的–><tx:method read-only=”true” /><tx:method /><!–其他方法使用默认事务策略–><tx:method /></tx:attributes></tx:advice><!– AOP配置–><aop:config><!–pointcut元素定义一个切入点,execution中的第一个星号用以匹配方法的返回类型,这里星号表明匹配所有返回类型。com.abc.dao.*.*(..)表明匹配com.abc.dao包下的所有类的所有方法–><aop:pointcutexpression=”execution(* com.abc.dao.*.*(..))” /><!–将定义好的事务处理策略应用到上述的切入点–><aop:advisor advice-ref=”txAdvice” pointcut-ref=”myPointcut” /></aop:config></beans>

在StudentDao的save方法中,故意添加了一行代码inta=1/0。这行代码会抛出ArithmeticException(这是一个RuntimeException),按照以上的配置,Spring会回滚事务。执行结果如下图所示:

如图中所示,事务已回滚,到数据库查询,学生记录也没有被插入。

本示例源码下载:。

【MyBatis学习笔记】系列之预备篇一:ant的下载与安装

【MyBatis学习笔记】系列之预备篇二:ant入门示例

【MyBatis学习笔记】系列之一:MyBatis入门示例

【MyBatis学习笔记】系列之二:MyBatis增删改示例

【MyBatis学习笔记】系列之三:MyBatis的association示例

【MyBatis学习笔记】系列之四:MyBatisassociation的两种形式

【MyBatis学习笔记】系列之五:MyBatis与Spring集成示例

【MyBatis学习笔记】系列之六:MyBatis与Spring集成示例续

【MyBatis学习笔记】系列之七:MyBatis一对多双向关联

【MyBatis学习笔记】系列之八:MyBatisMapperScannerConfigurer配置

【MyBatis学习笔记】系列之九:MyBatiscollection的两种形式

【MyBatis学习笔记】系列之十:MyBatis日志之Log4j示例

【MyBatis学习笔记】系列之十一:MyBatis多参数传递之注解方式示例

【MyBatis学习笔记】系列之十二:MyBatis多参数传递之默认命名方式示例

【MyBatis学习笔记】系列之十三:MyBatis多参数传递之Map方式示例

【MyBatis学习笔记】系列之十四:MyBatis中的N+1问题

【MyBatis学习笔记】系列之十五:MyBatis多参数传递之混合方式

【MyBatis学习笔记】系列之十六:Spring声明式事务管理示例

【MyBatis学习笔记】系列之十七:MyBatis多对多保存示例

本文出自 “肖凡的专栏” 博客,香港服务器,请务必保留此出处

做事的能力往往只能给你一种机会,

Spring声明式事务管理示例——MyBatis学习笔记之十六

相关文章:

你感兴趣的文章:

标签云: