前端C# + 后端java 实现批量更新

前端C# + 后端java 实现批量更新

前端:

 #region 函数-判定合格不合格批量合格
        private RespReturnDataList<EBoardEntity> judge()
        {
            RespReturnDataList<EBoardEntity> result = new RespReturnDataList<EBoardEntity>();
            List<EBoardEntity> list = new List<EBoardEntity>();
            try
            {
                int checkedRows = 0;
                int endIndex = detailGrid.Rows;
                for (int i = 1; i < endIndex; i++)
                {
                    if (detailGrid.Cell(i, 2).Text == "1")
                    {
                        checkedRows++;
                        if (detailGrid.Cell(i, 8).Text.Equals("待检验"))
                        {
                            //EBoardEntity 不能写在循环外,否则每次对eBoardList添加数据都会更新上一次写入的数据,导致更新时只更新最后一条
                            EBoardEntity eBoard = new EBoardEntity();
                            eBoard.id = detailGrid.Cell(i, 1).Text.ToString();
                            eBoard.checkStatus = "2";
                            eBoard.checkUser = "获取当前检验员";
                            list.Add(eBoard);
                        }
                    }
                }
                if (checkedRows != 0)
                {
                    if (list.Count>0 && list.Count==checkedRows)
                    {
                        result.dataList = list;
                        result.flag = "1";
                    }
                    else
                    {
                        result.flag = "0";
                        result.errorMessage = "选中数据状态不全为待检验";
                    }
                }
                else
                {
                    result.flag = "0";
                    result.errorMessage = "未选中任何行";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return result;
        }
        #endregion

        #region 按钮-合格
        private void tsb_Qualified_Click(object sender, EventArgs e)
        {
            try
            {
                RespReturnDataList<EBoardEntity> resultMain = this.judge();
                if (resultMain.flag == "1")
                {
                    //发起请求
                    ServiceClass service = new ServiceClass();
                    string url = "http://localhost:8017/*****/eBoard/qualified";
                    string a = ConvertBaseTools.FromEntitylistToJson<EBoardEntity>(resultMain.dataList);
                    RespReturnOptFlag result = service.CallRestfulPost(url, a, "POST");
                    if (!result.flag.Equals("1"))
                    {
                        throw new Exception(result.errorMessage);
                    }
                    this.dataListLoad(); 
                }
                else
                {
                    MessageBox.Show(resultMain.errorMessage, "合格", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
                
        }
        #endregion

记录:在这里遇到了一个问题,报错未将引用对象设置到引用实例经过排查后发现是for遍历问题,detailGrid.Rows比数据多了一行,所以在detailGrid.Cell(i, 2)取了不存在的行,所以要注意循环终止条件。

后端:
Controller:

    @ResponseBody
    @RequestMapping(value = "/qualified",method = RequestMethod.POST)
    public ReqReturnOptFlag qualified(@RequestBody List<EBoardEntity> eBoardEntityList) {
        ReqReturnOptFlag jsonResult = new ReqReturnOptFlag();
        try {
            eBoardService.qualified(eBoardEntityList);
            jsonResult.setFlag(ReqReturnOptFlag.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setFlag(ReqReturnOptFlag.ERROR);
            jsonResult.setErrorMessage(e.getMessage());
        }
        return jsonResult;
    }

Mapper:

@Mapper
@Component
public interface EBoardMapper extends BaseMapper<EBoardEntity,Long> {
    List<EBoardEntity> select(EBoardEntity eBoardEntity);
    List<EBoardEntity> selectImport(EBoardEntity eBoardEntity);
    void qualified(@Param("eBoardEntityList") List<EBoardEntity> eBoardEntityList);
}

Mapping:

<update id="qualified" parameterType="java.util.List">
        update SM_AFTERSALE_EBOARD
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="EBOARD_CHECKSTATUS =case" suffix="end,">
                <foreach collection="eBoardEntityList" item="i" index="index">
                    <if test="i.checkStatus!=null">
                        when (EBOARD_ID=#{i.id}) then #{i.checkStatus}
                    </if>
                </foreach>
            </trim>
            <trim prefix="EBOARD_CHECKUSER =case" suffix="end,">
                <foreach collection="eBoardEntityList" item="i" index="index">
                    <if test="i.checkUser!=null and i.checkUser!=''">
                        when (EBOARD_ID=#{i.id}) then #{i.checkUser}
                    </if>
                </foreach>
            </trim>
        </trim>
        where
        <foreach collection="eBoardEntityList" separator="or" item="i" index="index">
            (EBOARD_ID =#{i.id})
        </foreach>
    </update>

注意:Mapping里的collection="eBoardEntityList"必须要在Mapper接口为相应参数加上@Param(“eBoardEntityList”)注解

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值