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

Spring声明式事务

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

Spring声明式事务

  • 事务ACID原则
    • 原子性
    • 一致性
    • 隔离性
      • 多个业务可能操作同一个资源,防止数据损坏
    • 持久性
      • 事务一旦提交,无论系统发生什么问题,结果都不会在被影响,被持久化的写到存储器中。

 使用声明式事务需要用到Mybatis—Spring中的DataSourceTransactionManager对象

 配置声明式事务 使用Mybatis-Spring创建一个DataSourceTransactionManager对象-->
    

        
    
使用AOP完成事务的织入,增删改使用事务,查询只用于只读状态



    

        
        
            
            
            

            
            
        
    

配置事务的切入点,切入在Mapper文件下所有类和方法


    

        

        
    

实现类,继承SqlSessionDaoSupport 创建SqlSession

public class StudentMapperImpl2 extends SqlSessionDaoSupport implements StudentMapper{
    public List selectStudent() {
        //获取SqlSession,不在用SqlSessionTemplate来创建SQLSession了
        //创建一个student用户
        StudentMapper studentMapper2 = getSqlSession().getMapper(StudentMapper.class);
        student student = new student(1007, "图图", 1, "幼儿园班");
        int i = studentMapper2.InsertStudent(student);
        System.out.println("插入结果为:"+i);
        int r = studentMapper2.DelStudent(1007);  //此时在这会报错,而且切入点实在mapper路径下,所以要写在查询方法中
        System.out.println("删除结果为:"+r);
        return getSqlSession().getMapper(StudentMapper.class).selectStudent();
    }

    public int InsertStudent(student student) {
        return getSqlSession().getMapper(StudentMapper.class).InsertStudent(student);
    }

    public int DelStudent(int id) {
        return getSqlSession().getMapper(StudentMapper.class).DelStudent(id);
    }


}

 测试类

 //测试一个事务
    @Test
    public void Test(){

        //读取Spring配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        StudentMapper studentMapper2 = context.getBean("StudentMapper2", StudentMapper.class);

        List students = studentMapper2.selectStudent();

        //输出内容
        for (student s : students){
            System.out.println(s);
        }


    }

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

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

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