事务手动回滚

    @Override
    public BaseResponse transferStuPlan(GxyPlanTeacherStudentEntity teacherStudentEntity) {

      
        boolean  stuIHaveJob = false;
        List<GxyPlanTeacherStudentEntity> deleteStu = new ArrayList<>();
        for(String stuId:teacherStudentEntity.getStuIds()){
            GxyPlanTeacherStudentEntity teacherStudentDto = planTeacherStudentMapper.selectPlanIdAndStuId(teacherStudentEntity.getPlanId(),stuId,teacherStudentEntity.getSnowFlakeId());
         
                Integer countReport = reportService.countReport(reportEntity);
                if(countReport != null && countReport>0){
                    stuIHaveJob = true;
                    break;
                }

                planTeacherStudentMapper.delete(new QueryWrapper<GxyPlanTeacherStudentDto>().eq("plan_teacher_student_id",teacherStudentDto.getPlanTeacherStudentId()).eq("snow_flake_id",teacherStudentEntity.getSnowFlakeId()));
                deleteStu.add(teacherStudentDto);
           
        }

        if(stuIHaveJob){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            if(teacherStudentEntity.getStuIds().size()>1){
                return BaseResponse.error("无法删除");
            }
        }else{
            updatePlanIdIntoNewPlan(deleteStu,teacherStudentEntity.getNewPlanId(),teacherStudentEntity.getSnowFlakeId());
        }
        return BaseResponse.ok();
    }
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();   手动回滚。
stuIHaveJob 为true 时,上面的 planTeacherStudentMapper.delete 会回滚。
当 stuIHaveJob 为false 时,上面的 planTeacherStudentMapper.delete 不会回滚,继续执行 updatePlanIdIntoNewPlan。(当此方法中有异常时,也会回滚)
原文地址:https://www.cnblogs.com/z360519549/p/11757506.html