栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

10. Spring事务

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

10. Spring事务

文章目录

回顾事务为什么要有事务?————数据一致性

1. 事务由来项目结构2. 给UserMapper接口新增两个方法3. 编写UserMapper.xml新增的方法4. 编写实现类UserMapperImpl25. 进行测试 Spring中的事务管理

声明式事务管理

0. 项目结构:就是【上面的】事务由来的项目1. 使用Spring管理事务,需在beans.xml导入头文件aop,tx2. 事务管理器3. 配置事务的通知4. 配置AOP5. 进行测试

回顾事务

事务就是把一系列的动作当成一个独立的工作单元,这些动作要么全部完成,要么全部不起作用

事务四个属性ACID原则

原子性(atomicity)
事务是原子性操作,由一系列动作组成,事务的原子性确保动作要么全部完成,要么完全不起作用一致性(consistency)
一旦所有事务动作完成,事务就要被提交。数据和资源处于一种满足业务规则的一致性状态中
-隔离性(isolation)
可能多个事务会同时处理相同的数据,因此每个事务都应该与其他事务隔离开来,防止数据损坏持久性(durability)
事务一旦完成,无论系统发生什么错误,结果都不会受到影响。通常情况下,事务的结果被写到持久化存储器中



为什么要有事务?————数据一致性 1. 事务由来项目结构

2. 给UserMapper接口新增两个方法
//添加一个用户
int addUser(User user);

//根据id删除用户
int deleteUser(int id);
3. 编写UserMapper.xml新增的方法

我们故意把 delete 写错为deletes,测试!


insert into user (id,name,pwd) values (#{id},#{name},#{pwd})



deletes from user where id = #{id}

4. 编写实现类UserMapperImpl2
public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper {

    //增加一些操作
    public List selectUser() {
        User user = new User(444,"小明","123456");
        UserMapper mapper = getSqlSession().getMapper(UserMapper.class);

        mapper.addUser(user);

        mapper.deleteUser(4);

        return mapper.selectUser();
    }

    //新增
    public int addUser(User user) {
        UserMapper mapper = getSqlSession().getMapper(UserMapper.class);
        return mapper.addUser(user);
    }
    //删除
    public int deleteUser(int id) {
        UserMapper mapper = getSqlSession().getMapper(UserMapper.class);
        return mapper.deleteUser(id);
    }
}
5. 进行测试
@Test
public class MyTest {
    @Test
    public void test2(){
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        UserMapper mapper = (UserMapper) context.getBean("userMapper");
        List user = mapper.selectUser();
        System.out.println(user);
    }
}

报错:sql异常,delete写错了

结果 :插入成功!




Spring中的事务管理

为什么需要配置事务?

如果不配置,就需要我们手动提交控制事务;事务在项目开发过程非常重要,涉及到数据的一致性的问题,不容马虎!

Spring事务管理分为:

编程式事务管理:java代码

将事务管理代码嵌到业务方法中来控制事务的提交和回滚缺点:必须在每个事务操作业务逻辑中包含额外的事务管理代码

声明式事务管理:AOP

一般情况下比编程式事务好用。将事务管理代码从业务方法中分离出来,以声明的方式来实现事务管理。将事务管理作为横切关注点,通过aop方法模块化。Spring中通过Spring AOP框架支持声明式事务管理。



声明式事务管理 0. 项目结构:就是【上面的】事务由来的项目

1. 使用Spring管理事务,需在beans.xml导入头文件aop,tx
xmlns:aop="http://www.springframework.org/schema/aop"

http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
xmlns:tx="http://www.springframework.org/schema/tx"

http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
2. 事务管理器

无论使用Spring的哪种事务管理策略(编程式或者声明式)事务管理器都是必须的。

就是 Spring的核心事务管理抽象,管理封装了一组独立于技术的方法。

 

       

3. 配置事务的通知

add的意思是add开头的所有方法?还有所有添加的方法?

propagation="REQUIRED"
propagation是传播的意思
requierd:如果当前没有事务,就新建一个事务,如果已存在一个事务中,加入到这个事务中,这是最常见的选择,也是默认选择。


   
       
       
       
       
       
       
       
   

4. 配置AOP

记得导入aop的头文件!


   
   

5. 进行测试

删掉刚才插入的数据,再次测试!

@Test
public void test2(){
   ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
   UserMapper mapper = (UserMapper) context.getBean("userDao");
   List user = mapper.selectUser();
   System.out.println(user);
}

输出结果:


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/726683.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号