zoukankan      html  css  js  c++  java
  • 主表-明细表-修改操作业务代码流程

    需求描述:

      对一个主表记录的明细表记录进行批量更新操作!

          比如要修改某个学生A的多个兴趣爱好信息~

      学生A 原来兴趣爱好有3个, 现在把 "唱歌" 改成 "rap",  "篮球" 不喜欢了, 并增加一个爱好 "追星"~ 

     保存操作的时候会对明细表同时操作insert/update/delete操作;

    代码流水线:

    下面总结了一套明细表操作的流水线操作, 代码也可以copy ;

    (主表更新操作比较简单,这里忽略 )

    /**
         * update details
         * <p>
         * it may contains three operating types 'add' ,'edit', 'delete'
         * </p>
         *
         * @param operVo
         */
        private void doEditDetails(RealInCapitalHeaderOperVo operVo) {
            Long headerId = operVo.getId();
            List<RealInCapitalDetailVo> newDetails = operVo.getDetails();  //前端传过来的修改后的明细
            List<RealInCapitalDetail> oldDetails = findDetails(operVo.getId());  //查询明细数据库记录
           //准备三个集合用于后面承载数据, 这里的Vo, 根据业务调整
            List<RealInCapitalDetailVo> inserts = new ArrayList<>();
            List<RealInCapitalDetailVo> updates = new ArrayList<>();
            List<RealInCapitalDetail> deletes = new ArrayList<>();
    
    //明细记录数据list转map Map
    <Long, RealInCapitalDetail> oldMap = Maps.newHashMap(); Set<Long> newIdsSet = Sets.newHashSet(); //list convert map if (CollectionUtils.isNotEmpty(oldDetails)) { oldMap = oldDetails.stream().collect(Collectors.toMap(RealInCapitalDetail::getId, e -> e)); } //find the inserts and updates 找出新增和更新数据 if (CollectionUtils.isNotEmpty(newDetails)) { newDetails.forEach(e -> { if (e.getId() == null) { inserts.add(e); } else { newIdsSet.add(e.getId()); updates.add(e); } }); } //find the deletes 找出删除的数据 if (!oldMap.isEmpty()) { oldMap.forEach((key, value) -> { if (!newIdsSet.contains(key)) { deletes.add(value); } }); } //do insert 执行新增 addDetails(headerId, inserts); //do update 执行更新 updateDetails(updates); //do delete 执行删除 deleteDetails(deletes); }

     上面某些代码可根据JDK版本自行调整~  

      具体的DB操作自行对应~

    总结:  我们不生产代码~ 只做代码的搬运工!

  • 相关阅读:
    洛谷 P1074 靶形数独 Label:search 不会
    TYVJ P3522 &&洛谷 P1135 奇怪的电梯 Label:bfs
    洛谷 P1160 队列安排 Label:链表 数据结构
    uestc 1073 秋实大哥与线段树 Label:线段树
    TYVJ P3407 佳佳的魔法照片 Label:语文很重要 语文很重要 语文很重要
    TYVJ P1103 多项式输出 Label:模拟 有点儿坑
    A+B Problem 详细解答 (转载)
    如何批量修改文件名
    c++ 在windows下获取时间和计算时间差的几种方法总结
    SQL Server 2008在Windows 10上不支持
  • 原文地址:https://www.cnblogs.com/coloz/p/13280780.html
Copyright © 2011-2022 走看看