使用employService的自带的一些方法,我们可以超级快的完成增删改查

增加

核心代码:employeeService.save(employee);

@PostMapping
public R<String> save(HttpServletRequest request, @RequestBody Employee employee) {
    employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
    employee.setCreateTime(LocalDateTime.now());
    employee.setUpdateTime(LocalDateTime.now());
    // 获取当前用户id
    Long empId = (Long) request.getSession().getAttribute("employee");
    employee.setCreateUser(empId);
    employee.setUpdateUser(empId);
    // 保存用户,我们用户名是唯一的,因此可能出现异常
    employeeService.save(employee);
    // 返回值
    return R.success("新增用户成功");
}

修改

核心代码:employeeService.updateById(employee);,如果某个属性值是null,就不更新那个属性值

@PutMapping
public R<String> update(HttpServletRequest request,@RequestBody Employee employee) {
    log.info("update", employee.toString());
    Long empId = (Long) request.getSession().getAttribute("employee");
    employee.setUpdateTime(LocalDateTime.now());
    employee.setUpdateUser(empId);
    employeeService.updateById(employee);
    return R.success("更新成功!");
}

查找

核心代码:employeeService.getById(id);

@GetMapping("{id}")
public R<Employee> queryById(@PathVariable Long id) {
    Employee emp = employeeService.getById(id);
    if(emp != null) {
        return R.success(emp);
    }
    return R.error("查询失败");
}

删除

通过id删除一个类别表的一个数据

@DeleteMapping
    public R<String> deleteCategory(Long ids) {
        categoryService.removeById(ids);
        return R.success("删除成功");
    }