前端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”)注解