企业网站开发效果,dede产品展示网站模板,二级建造师证报考条件,人才网站建设策划书文章目录 一、声明式事务1.1 什么是事务1.2 事务的应用场景1.3 事务的特性#xff08;ACID#xff09;1.4 未使用事务的代码示例1.5 配置 Spring 声明式事务学习总结 一、声明式事务 1.1 什么是事务 把一组业务当成一个业务来做#xff1b;要么都成功#xff0c;要么都失败… 文章目录 一、声明式事务1.1 什么是事务1.2 事务的应用场景1.3 事务的特性ACID1.4 未使用事务的代码示例1.5 配置 Spring 声明式事务学习总结 一、声明式事务 1.1 什么是事务 把一组业务当成一个业务来做要么都成功要么都失败 事务在项目开发中十分的重要涉及到数据的一致性问题不能马虎 1.2 事务的应用场景 在我们日常开发中具体的业务大多数都是操作数据库的增删改查其中包含了很多原子性的数据库操作。当同一个业务中出现多个原子性的数据库操作时为了数据的安全性避免数据不同步的状况发生就需要用到事务。 举个栗子银行转账 两个人来到银行进行转账的操作A 需要转给 B 1000块钱也就是 A 的钱减少一千B 的钱增加一千。但是如果在转账途中出现银行系统崩溃或者网络故障等状况导致 A 的钱确实是少了一千但是 B 的钱并没有增加这显然是不对的。这时候事务的功效就显示出来了两个操作要么就都成功要么直接事务回滚让操作都失败以此来保证数据的安全性和一致性。 1.3 事务的特性ACID 原子性Atomicity事务是最小的执行单位不允许分割。事务的原子性确保动作要么全部完成要么完全不起作用 一致性Consistency执行事务前后数据保持一致例如转账业务中无论事务是否成功转账者和收款人的总额应该是不变的 隔离性Isolation并发访问数据库时一个用户的事务不被其他事务所干扰各并发事务之间数据库是独立的 持久性Durability一个事务被提交之后。它对数据库中数据的改变是持久的即使数据库发生故障也不应该对其有任何影响。
只有保证了事务的持久性、原子性、隔离性之后一致性才能得到保障。也就是说 A、I、D 是手段C 是目的 1.4 未使用事务的代码示例 项目背景 Spring Mybatis 将第八章的项目原封不动的粘过来修改 mapper.xml 将删除 SQL 改成错误的写法其他内容不变 修改测试类新增一条数据在将这条数据删除 RunWith(SpringJUnit4ClassRunner.class)
ContextConfiguration(locations classpath:spring-config.xml)
public class Test_SpringMyBatis {Autowiredprivate UsersMapper userMapper;Testpublic void testFindUserList(){Users users new Users();users.setId(4);users.setName(姚青);users.setPwd(123456);int i userMapper.saveUser(users);int i3 userMapper.deleteUsers(users);}}执行结果可以看出执行到 delete 方法时候报错了但是第一条新增添加成功了这显然不是很合理 1.5 配置 Spring 声明式事务学习总结 上面的代码执行出来的结果明显是不符合真实情况的所有这里需要配置 Spring 的声明式事务 在 Spring 配置文件中新增配置 使用Spring管理事务需要导入头文件的约束 : tx xmlns:txhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd配置 Spring 声明式事务 !-- 配置 Spring 声明式事务 --bean idtransactionManager classorg.springframework.jdbc.datasource.DataSourceTransactionManagerconstructor-arg refdataSource//bean配置好声明式事务之后结合 Spring aop 实现事务的织入这里需要引入 tx 事务 !-- 结合 Spring aop 实现事务的织入 --
!-- 第一步配置事务通知 --
tx:advice idtxAdvice transaction-managertransactionManager!-- 给那些方法配置事务,可以是具体的方法名也可以是**代表所有方法 --!--配置事务的传播特性 new propagation--tx:attributestx:method nameadd propagationREQUIRED/tx:method namedelete propagationREQUIRED/tx:method nameupdate propagationREQUIRED/tx:method namequery read-onlytrue/tx:method name* propagationREQUIRED//tx:attributes
/tx:advice!-- 第二步配置事务切入 --
aop:config!-- 指定切入点为 mapper 包下所有接口中的所有方法 --aop:pointcut idtxPointCut expressionexecution(* com.sys.mapper.*.*(..))/aop:advisor advice-reftxAdvice pointcut-reftxPointCut/
/aop:configSpring 完整配置 ?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxmlns:txhttp://www.springframework.org/schema/txxmlns:aophttp://www.springframework.org/schema/aopxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aophttps://www.springframework.org/schema/aop/spring-aop.xsd!-- 加载数据库连接信息的属性文件 --context:property-placeholder locationclasspath:jdbc-config.properties/!-- 配置Druid数据源的Bean --bean iddataSource classcom.alibaba.druid.pool.DruidDataSourceproperty namedriverClassName value${jdbc.driver}/property nameurl value${jdbc.url}/property nameusername value${jdbc.username}/property namepassword value${jdbc.password}//bean!-- 配置SessionFactory的Bean --bean idsessionFactory classorg.mybatis.spring.SqlSessionFactoryBean!-- 注入数据源 --property namedataSource refdataSource/!-- 指定MyBatis配置文件的位置 --property nameconfigLocation valueclasspath:mybatis-config.xml/!-- 配置 xml 的映射 --property namemapperLocations valueclasspath:mapper/*.xml//bean!-- 配置mapper接口的扫描器将Mapper接口的实现类自动注入到IoC容器中实现类Bean的名称默认为接口类名的首字母小写 --bean classorg.mybatis.spring.mapper.MapperScannerConfigurer!-- basePackage属性指定自动扫描mapper接口所在的包 --property namebasePackage valuecom.sys.mapper//bean!-- 配置 Spring 声明式事务 --bean idtransactionManager classorg.springframework.jdbc.datasource.DataSourceTransactionManagerconstructor-arg refdataSource//bean!-- 结合 Spring aop 实现事务的织入 --!-- 第一步配置事务通知 --tx:advice idtxAdvice transaction-managertransactionManager!-- 给那些方法配置事务,可以是具体的方法名也可以是**代表所有方法 --!--配置事务的传播特性 new propagation--tx:attributestx:method nameadd propagationREQUIRED/tx:method namedelete propagationREQUIRED/tx:method nameupdate propagationREQUIRED/tx:method namequery read-onlytrue/tx:method name* propagationREQUIRED//tx:attributes/tx:advice!-- 第二步配置事务切入 --aop:config!-- 指定切入点为 mapper 包下所有接口中的所有方法 --aop:pointcut idtxPointCut expressionexecution(* com.sys.mapper.*.*(..))/aop:advisor advice-reftxAdvice pointcut-reftxPointCut//aop:config/beans其他代码不变 测试结果执行到 delete 方法时依然报错但是数据库没有新增数据事务回滚成功。