第六节.新增注意事项

1 概述

基础数据新增,通用:

新增主要考虑,传入的字符校验,是否需要校验数据重复,是否具备上移,下移,返回对象格式

@LogAnnotation(firstLevel = "基础数据", secondLevel = "国家", noteLevel = "新增")
    @PostMapping(value = "save")
    public Map save(@RequestBody TBaseCountry TBaseCountry, HttpServletRequest request) {
        Map map = new HashMap(4);
        try{
            Map  mapuser= getParametersUserinfo(request);
            String userid=mapuser.get("userid").toString();
            String name=TBaseCountry.getName();// 前端是空 不允许录入到后台 ,此处一定有数据

            boolean  iswd= FilterUtils.verification(name);// 特殊字符校验 如果是 文章内容注意不要校验
            if(!iswd){
                map.put("flag", false);
                map.put("msg", StatusCode.C00511.getMsg());
                map.put("status", StatusCode.C00511.getCode());
                return map;
            }
            //数据验证 该字段是否已经存在数据内 存在的话 就不能录入
            Map mapName = new HashMap(4);
            mapName.put("name",name);
            mapName.put("id",null);
            Long eq= objServices.countById(mapName);
            if(eq !=0){
                map.put("flag", false);
                map.put("msg", StatusCode.C00510.getMsg());
                map.put("status", StatusCode.C00510.getCode());
                return map;
            }
            // 处理 最大值
            com.yinghui.soft.web.baseCountry.model.TBaseCountry objMax=objServices.selectByOrderNumMax();
            if(null==objMax){
                TBaseCountry.setOrderNum(1);// 如果是第一个值默认给1
            }else{
                int ordernUm=objMax.getOrderNum()+1;// 如果是后续新增 增加1
                TBaseCountry.setOrderNum(ordernUm);
            }

            TBaseCountry.setCreateUser(Integer.parseInt(userid));
            TBaseCountry.setCreateTime(new Date());
            TBaseCountry.setDeleteFlag(1);
            int  i =objServices.save(TBaseCountry);
            if (i == 1) {
                map.put("flag", true);
                map.put("msg", StatusCode.C00200.getMsg());
                map.put("status", StatusCode.C00200.getCode());
            } else {
                map.put("flag", false);
                map.put("msg", StatusCode.C00201.getMsg());
                map.put("status", StatusCode.C00201.getCode());
            }
        } catch (Exception e) {
            logger.error(StatusCode.C00400.getMsg(), e);
            map.put("flag", false);
            map.put("msg", StatusCode.C00400.getMsg());
            map.put("status", StatusCode.C00400.getCode());
        }
        return map;
    }

2 从token获取信息

Map  mapuser= getParametersUserinfo(request);  这是获取参数 获取token内的参数
String userid=mapuser.get("userid").toString(); 获取用用户编码
String name=TBaseCountry.getName();// 前端是空 不允许录入到后台 ,此处一定有数据

3 校验特殊字符

boolean  iswd= FilterUtils.verification(name);// 特殊字符校验 如果是 文章内容注意不要校验
if(!iswd){
    map.put("flag", false);
    map.put("msg", StatusCode.C00511.getMsg());
    map.put("status", StatusCode.C00511.getCode());
    return map;
}

4 数据重复校验

//数据验证 该字段是否已经存在数据内 存在的话 就不能录入
Map mapName = new HashMap(4);
mapName.put("name",name);
mapName.put("id",null);
Long eq= objServices.countById(mapName);
if(eq !=0){
    map.put("flag", false);
    map.put("msg", StatusCode.C00510.getMsg());
    map.put("status", StatusCode.C00510.getCode());
    return map;
}

5 处理上下移动获取最大值 赋值

// 处理 最大值
com.yinghui.soft.web.baseCountry.model.TBaseCountry objMax=objServices.selectByOrderNumMax();
if(null==objMax){
    TBaseCountry.setOrderNum(1);// 如果是第一个值默认给1
}else{
    int ordernUm=objMax.getOrderNum()+1;// 如果是后续新增 增加1
    TBaseCountry.setOrderNum(ordernUm);
}

6 保存并返回

  TBaseCountry.setCreateUser(Integer.parseInt(userid));
    TBaseCountry.setCreateTime(new Date());
    TBaseCountry.setDeleteFlag(1);
    int  i =objServices.save(TBaseCountry);
    if (i == 1) {
        map.put("flag", true);
        map.put("msg", StatusCode.C00200.getMsg());
        map.put("status", StatusCode.C00200.getCode());
    } else {
        map.put("flag", false);
        map.put("msg", StatusCode.C00201.getMsg());
        map.put("status", StatusCode.C00201.getCode());
    }
} catch (Exception e) {
    logger.error(StatusCode.C00400.getMsg(), e);
    map.put("flag", false);
    map.put("msg", StatusCode.C00400.getMsg());
    map.put("status", StatusCode.C00400.getCode());
}

如果不是基础数据,按照实际的业务进行组织即可,如下:

注意返回的参数用枚举

/**
     * 保存
     *
     * @return
     */
    @LogAnnotation(firstLevel = "项目管理", secondLevel = "项目管理", noteLevel = "新增")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Map save(@RequestBody TProjectManagement TProjectManagement, HttpServletRequest request) {
        Map map = new HashMap(4);
        try{
            Map  mapuser= getParametersUserinfo(request);
            String userid=mapuser.get("userid").toString();
            System.out.println(userid);
            String name=TProjectManagement.getName();// 前端是空 不允许录入到后台 ,此处一定有数据

            boolean  iswd= FilterUtils.verification(name);// 特殊字符校验 如果是 文章内容注意不要校验
            if(!iswd){
                map.put("flag", false);
                map.put("msg", StatusCode.C00511.getMsg());
                map.put("status", StatusCode.C00511.getCode());
                return map;
            }
            //数据验证
            Map mapName = new HashMap(4);
            mapName.put("name",TProjectManagement.getName());
            mapName.put("id",null);
            Long eq= objServices.countById(mapName);
            if(eq !=0){
                map.put("flag", false);
                map.put("msg", StatusCode.C00510.getMsg());
                map.put("status", StatusCode.C00510.getCode());
                return map;
            }

            TProjectManagement.setCreateUser(Integer.parseInt(userid));
            TProjectManagement.setCreateTime(new Date());
            TProjectManagement.setDeleteFlag(1);

            int  i =objServices.save(TProjectManagement);
            if (i == 1) {
                map.put("flag", true);
                map.put("msg", StatusCode.C00200.getMsg());
                map.put("status", StatusCode.C00200.getCode());
            } else {
                map.put("flag", false);
                map.put("msg", StatusCode.C00201.getMsg());
                map.put("status", StatusCode.C00201.getCode());
            }
        } catch (Exception e) {
            logger.error(StatusCode.C00400.getMsg(), e);
            map.put("flag", false);
            map.put("msg", StatusCode.C00400.getMsg());
            map.put("status", StatusCode.C00400.getCode());
        }
        return map;
    } 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

akglobe

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值