java中事务的使用

本文详细介绍了在Springboot应用中使用@Transactional注解处理同一张表和不同表的事务操作,包括业务代码示例、测试代码和预期结果,以及异常处理情况。
摘要由CSDN通过智能技术生成


前言

本文将介绍在springboot中使用@Transactional注解来完成对数据库事务的操作,保证数据一致性。


一、同一张表

1.业务代码

Controller

@Controller
public class StudentInfoController {
	/**
	 * 相同表之间的事务
	 * @param s1
	 * @param s2
	 * @return
	 */
	public int transactional(StudentInfo s1,StudentInfo s2) {
		return studentInfoService.transactional(s1,s2);
	}
}

Service

@Service
public class StudentInfoService  {
	@Autowired
	private StudentInfoMapper studentInfoMapper;
	/**
	 * 测试事务
	 */
	@Transactional
	public int transactional(StudentInfo s1,StudentInfo s2) {
		int count=0;
		count += studentInfoMapper.insertSelective(s1);
//		if(count==1){
//			throw new RuntimeException("ex");
//		}
		count += studentInfoMapper.insertSelective(s2);
		return count;
	}
}

2.测试代码

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringbootStart.class)
public class SpringbootStartTest {
	@Autowired
    private StudentInfoController studentInfoController;
@Test
    public void test(){
        transactional();
    }
    public void transactional(){
        StudentInfo s1=new StudentInfo();
        s1.setId("t1");
        s1.setName("zs");
        s1.setIdType("sfz");
        s1.setIdNumber("100");
        StudentInfo s2=new StudentInfo();
        s2.setId("t2");
        s2.setName("ls");
        s2.setIdType("sfz");
        s2.setIdNumber("200");
        studentInfoController.transactional(s1,s2);
        System.out.println("success");
    }
}

3.测试结果

正常情况下,程序处理完成,插入了两条数据没问题
在这里插入图片描述

在这里插入图片描述
接下来,打开service中注释,主动抛出异常
在这里插入图片描述
在这里插入图片描述

二、不同表

1.业务代码

Controller

@Controller
public class StudentInfoController extends BaseController{
	/**
	 * 不同表之间的事务
	 * @param s1
	 * @param s2
	 * @return
	 */
	public int diffTransactional(StudentInfo s1, StudentCurriculum s2) {
		return studentInfoService.diffTransactional(s1,s2);
	}
}

Service

@Service
public class StudentInfoService  {
	@Autowired
	private StudentInfoMapper studentInfoMapper;
	@Autowired
	private StudentCurriculumMapper studentCurriculumMapper;
	/**
	 * 测试事务
	 */
	@Transactional
	public int diffTransactional(StudentInfo s1, StudentCurriculum s2) {
		int count=0;
		count += studentInfoMapper.insertSelective(s1);
//		if(count==1){
//			throw new RuntimeException("ex");
//		}
		count += studentCurriculumMapper.insertSelective(s2);
		return count;
	}
}

2.测试代码

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringbootStart.class)
public class SpringbootStartTest {
	@Autowired
    private StudentInfoController studentInfoController;
@Test
    public void test(){
        diffTransactional();
    }
    public void diffTransactional(){
        StudentInfo s1=new StudentInfo();
        s1.setId("t3");
        s1.setName("zs");
        s1.setIdType("sfz");
        s1.setIdNumber("100");
        StudentCurriculum s2=new StudentCurriculum();
        s2.setId(4);
        s2.setCurriculumName("1");
        s2.setTeacher("zjg");
        studentInfoController.diffTransactional(s1,s2);
        System.out.println("success");
    }
}

3.测试结果

正常情况下,程序处理完成,插入了两条数据没问题
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
接下来,打开service中注释,主动抛出异常
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述


总结

回到顶部

  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值