参数校验三要素:
1.查询对应实体的需要校验的参数
2.判断查询出的实体是否为空
3.判断传进来的值与实体类的值是否相等
示例:对某某家属名字做唯一校验
---此处用到的mybatisPlus中的函数式接口获取字段名称---
控制层代码:
@GetMapping("/checkName") public Response checkName(@RequestParam @NotEmpty(message = "名称不能为空")String name, @RequestParam(value = "id", required = false)Long id){ return Service.checkName(name,id); } service层代码
@Override public Response checkName(String name, Long id) { if (checkNames(name,id)){ throw new BizException(String.format("该名称【%s】己存在", name)); } return Response.buildSuccess(); } private boolean checkName(String name,Long id){
Parent parent = lambdaQuery(). eq(Parent::getName, name) .select(Parent::getId) .one(); if (parent==null){ return false; } return !Objects.equals(Prent.getId(),id);