Java事务操作

Java事务操作业务需要,需要用Java写一个实物,多个sql一起要么都执行,要么都不执行,写法如下,记录一下。首先,在Springboot的启动类,或者某个@Configuration的类上加上@EnableTransactionManagement开启事务。Conntrol/***@Descripti

大家好,欢迎来到IT知识分享网。

业务需要,需要用Java写一个实物,多个sql一起要么都执行,要么都不执行,写法如下,记录一下。

 

首先,在Springboot的启动类,或者某个@Configuration的类上加上@EnableTransactionManagement开启事务。

Java事务操作

  

Conntrol

    /**
     * @Description:下发立体库出库计划(事务)
     * @Param: [id]
     * @return: java.util.List<org.muyuan.slaughter.workshop.convertApi.vo.threeStage.store.MySpLiTiKuOrderStatusVO>
     * @Author: 孟海鹏
     * @Date: 2022/9/18
     */
    @ApiOperation(value = "立体库出库计划(三代)(下发立体库出库计划)")
    @GetMapping("/UpdateLiTiKuHouseOutPlanStatus")
    public R UpdateLiTiKuHouseOutPlanStatus(@NotEmpty String issuer, @NotEmpty String issueDate, @NotEmpty String outPlanNo) {
        mySpLitikuhouseoutplanService.updateLiTiKuHouseOutPlanStatus(issuer, issueDate, outPlanNo);
        return R.success("下发成功!");
    }

Services

只要在需要使用事务的方法上加上@Transactional 关键字开启事务支持。

这里事务分为2步,第一步先根据where条件判断是否满足更新条件,若不满足直接用throw new RuntimeException抛出异常,自动执行回滚操作。若满足之后再执行第二步更新操作。

    /**
     * @Description: 下发立体库出库计划(事务)
     * @Param: [FIssuer, FIssueDate, FOutPlanNo]
     * @return: java.util.List<org.muyuan.slaughter.workshop.convertApi.vo.threeStage.store.MySpLiTiKuHouseOutPlanStatusVO>
     * @Author: 孟海鹏
     * @Date: 2022/9/18
     */ @Transactional public void updateLiTiKuHouseOutPlanStatus(String issuer, String issueDate, String outPlanNo) {
        //1.先校验是否有满足更新条件的数据
        List<MySpLitikuhouseoutplanEntity> entities = baseMapper.selectList(Wrappers.<MySpLitikuhouseoutplanEntity>query().lambda()
                .eq(MySpLitikuhouseoutplanEntity::getOutPlanNo, outPlanNo)
                .eq(MySpLitikuhouseoutplanEntity::getDeleteStatus, CommonConstant.STATUS_INIT));
        if (CollectionUtils.isEmpty(entities)) throw new RuntimeException("下发失败");

        List<String> ids = entities.stream().map(MySpLitikuhouseoutplanEntity::getId).collect(Collectors.toList());
        List<MySpLitikuhouseoutplanDetailEntity> detailEntities = litikuhouseoutplanDetailMapper.selectList(Wrappers.<MySpLitikuhouseoutplanDetailEntity>query().lambda()
                .eq(MySpLitikuhouseoutplanDetailEntity::getDeleteStatus, CommonConstant.STATUS_INIT)
                .in(MySpLitikuhouseoutplanDetailEntity::getInterID, ids));
        if (CollectionUtils.isEmpty(detailEntities)) throw new RuntimeException("下发失败");

        List<TBusinessAsrsOutbillEntity> asrsOutbillEntities = businessAsrsOutbillMapper.selectList(Wrappers.<TBusinessAsrsOutbillEntity>query().lambda()
                .eq(TBusinessAsrsOutbillEntity::getOutBillNo, outPlanNo)
                .eq(TBusinessAsrsOutbillEntity::getSyncStatus, CommonConstant.DATE_DEL));
        if (CollectionUtils.isEmpty(asrsOutbillEntities)) throw new RuntimeException("下发失败");

        //2.更新状态
        baseMapper.UpdateLiTiKuHouseOutPlanStatus(issuer, issueDate, outPlanNo);
        baseMapper.UpdateLiTiKuHouseOutPlanStatus2(issuer, issueDate, outPlanNo);
        baseMapper.UpdateLiTiKuHouseOutPlanStatus3(issuer, issueDate, outPlanNo);
    }

Mapper

    /**
     * @Description:下发立体库出库计划-sql1(事务)
     * @Param: [FIssuer, FIssueDate, FOutPlanNo]
     * @return: java.util.List<org.muyuan.slaughter.workshop.convertApi.vo.threeStage.store.MySpLiTiKuHouseOutPlanStatusVO>
     * @Author: mhp
     * @Date: 2022/9/18
     */
    Integer UpdateLiTiKuHouseOutPlanStatus(String issuer, String issueDate, String outPlanNo);

    /**
     * @Description: 下发立体库出库计划-sql2(事务)
     * @Param: [issuer, issueDate, outPlanNo]
     * @return: java.lang.Integer
     * @Author: mhp
     * @Date: 2022/9/29
     */
    Integer UpdateLiTiKuHouseOutPlanStatus2(String issuer, String issueDate, String outPlanNo);

    /**
     * @Description: 下发立体库出库计划-sql3(事务)
     * @Param: [issuer, issueDate, outPlanNo]
     * @return: java.lang.Integer
     * @Author: mhp
     * @Date: 2022/9/29
     */
    Integer UpdateLiTiKuHouseOutPlanStatus3(String issuer, String issueDate, String outPlanNo);

XML

    <update id="UpdateLiTiKuHouseOutPlanStatus">
      update MY_SP_LiTikuHouseOutPlan set
      FCheck=1,
      exchange_status=0,
      FIssuer= #{issuer},
      FIssueDate=#{issueDate}
      where FOutPlanNo=#{outPlanNo} and FDelete=0;
    </update>

    <update id="UpdateLiTiKuHouseOutPlanStatus2">
        update MY_SP_LiTikuHouseOutPlan_Detail set
        FCheck=1,
        exchange_status=0
        where FInterID in(select FID from MY_SP_LiTikuHouseOutPlan where FOutPlanNo=#{outPlanNo} and FDelete=0);
    </update>

    <update id="UpdateLiTiKuHouseOutPlanStatus3">
        update t_business_asrs_outbill set
        exchange_status=0,
        sync_status=0
        where out_bill_no=#{outPlanNo} and sync_status=-1;
    </update>

 

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/27782.html

(0)
上一篇 2023-09-19 16:00
下一篇 2023-09-19 16:45

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

关注微信