SpringMVC事务控制(xml文件配置和注解配置) 您所在的位置:网站首页 spring开启事务的方式 SpringMVC事务控制(xml文件配置和注解配置)

SpringMVC事务控制(xml文件配置和注解配置)

2024-07-04 21:08| 来源: 网络整理| 查看: 265

事务的定义

事务应该具有4个属性:原子性、一致性、隔离性、持久性。这四个属性通常称为ACID特性。 原子性(atomicity)。一个事务是一个不可分割的工作单位,事务中包括的操作要么都做,要么都不做。 一致性(consistency)。事务必须是使数据库从一个一致性状态变到另一个一致性状态。一致性与原子性是密切相关的。 隔离性(isolation)。一个事务的执行不能被其他事务干扰。即一个事务内部的操作及使用的数据对并发的其他事务是隔离的,并发执行的各个事务之间不能互相干扰。 持久性(durability)。持久性也称永久性(permanence),指一个事务一旦提交,它对数据库中数据的改变就应该是永久性的。接下来的其他操作或故障不应该对其有任何影响。

编程式事务控制三大对象

1.PlatformTransactionManager 2.TransactionDefinition 3.TransactionStatus

PlatformTransactionManager对象

PlatformTransactionManager 接口是 spring 的事务管理器,它里面提供了我们常用的操作事务的方法 在这里插入图片描述

TransactionDefinition

在这里插入图片描述 在这里插入图片描述

在这里插入图片描述

TransactionStatus

在这里插入图片描述

SpringMVC要做的事情

在SpringMVC中我们主要要做的是当事务出现问题的时候把数据给回滚 比如A向B转钱 这要有两个动作:1.A 减钱 2.B加钱 如果1 和2 中间出现问题就把数据给回滚

转钱例子的环境搭建(用配置文件的方式)

在搭建环境的时候一共需要三个包 Controller Service 和Dao Dao是最底层的 他相当于是只提供最底层的零件 Service是用Dao层提供的零件来组装成工具来让Service层来使用 Controller是用Service提供的工具来完成一些功能 在Dao层和Service层 我们要先写接口 来确定我们要完成什么功能 然后再写这些接口的实现类 impl

这下面的实现类中都提供了set方法是为了让spring容器通过xml配置文件来注入

dao层接口

public interface AccountDao { public void out(String outMan,double money); public void in(String inMan,double money); }

dao层接口的实现

public class AccountDaoimpl implements AccountDao { private JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } @Override public void out(String outMan, double money) { jdbcTemplate.update("update account set money=money-? where name=?",money,outMan); } @Override public void in(String inMan, double money) { jdbcTemplate.update("update account set money=money+? where name=?",money,inMan); } }

service层接口

public interface AccountService { public void transfer(String outMan,String inMan,double money); }

service层接口的实现

public class AccountServiceImpl implements AccountService { private AccountDao accountDao; public void setAccountDao(AccountDaoimpl accountDao) { this.accountDao = accountDao; } @Override public void transfer(String outMan, String inMan, double money) { accountDao.out(outMan,money); // int a=1/0; accountDao.in(inMan,money); } }

实现功能的controller

public class AccountController { public static void main(String[] args) { ApplicationContext app= new ClassPathXmlApplicationContext("applicationContext.xml"); AccountService bean = app.getBean(AccountService.class); bean.transfer("tom","lucy",500); } }

配置文件applicationContext.xml

关于配置文件的详解

这里来着重说一下说一下配置文件中关于事务控制的三部分内容

平台事务管理器

平台事务管理器提供了常用的操作事务的方法

选择用那个管理器👆 讲驱动配置导入👆

上面的datasource如下

通知 (事务)的增强

name:切点方法名称 isolation:事务的隔离级别 propogation:事务的传播行为 timeout:超时时间 read-only:是否只读

配置事务的aop织入 新加的功能(事务功能) 👆 👆原来的转账功能

织入指的是把原来的功能和新加的功能结合到一起的过程 execution(* com.itheima.service.impl. * . * (.*.)) com前面实际上要写 public void 这种东西 但是 *的意思是全部都可以 这个里面是指定对应的类 其中 的意思是当前的所有 两个点..指的是当前包的下面的所有

用注解的方式配置声明式事务控制 编写 AccoutDao @Repository("accountDao") 👆是这个在spring容器中的名字 public class AccountDaoImpl implements AccountDao { @Autowired 👆是从spring容器中根据一样的类来配置 private JdbcTemplate jdbcTemplate; public void out(String outMan, double money) { jdbcTemplate.update("update account set money=money-? where name=?",money,outMan); } public void in(String inMan, double money) { jdbcTemplate.update("update account set money=money+? where name=?",money,inMan); } } 编写 AccoutService @Service("accountService") 👆这个在spring容器中的名字 @Transactional 👆 这是标记当前这个是事务 public class AccountServiceImpl implements AccountService { @Autowired 👆是从spring容器中找到一样的类来注入到👇中 private AccountDao accountDao; @Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED) 👆配置通知的属性 public void transfer(String outMan, String inMan, double money) { accountDao.out(outMan,money); int i = 1/0; accountDao.in(inMan,money); } } 编写controller public static void main(String[] args) { ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext-anno.xml"); AccountService accountService = app.getBean(AccountService.class); accountService.transfer("tom","lucy",500); } 编写 applicationContext.xml 配置文件

主要的是下面这些

全部的是



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有