做基本的增删改查功能

在控制类UserController中,要实现rest风格的增删改查功能,我们需要知道常用的四种请求类型:GET,POST,PUT,DELECT。对users进行操作,找的对应的url
@RestController
@RequestMapping("/users")
public class UserController {
}

1.新增,使用POST请求。类型为post,定义的url为 http://localhost/users

@PostMapping
    public R save(@RequestBody User user){
        return new R(userService.save(user));
    }

2.通过元素id删除,使用DELECT请求。类型为DELECT,定义的url为 http://localhost/users/删除元素的id

@DeleteMapping("{id}")
    public R delete(@PathVariable Integer id){
        return new R(userService.removeById(id));
    }

3.通过元素id修改,使用PUT请求。类型为put,定义的url为 http://localhost/users

@PutMapping
    public R update(@RequestBody User user){
        return new R(userService.updateById(user));
    }

4.查找,使用GET请求。类型为get,

一种是查全部,把该项数据库的内容全部查找出来显示到界面上,定义的url为 http://localhost/users

另一种是根据元素id查找,通过id把对应数据查找出来显示到界面上,定义的url为 http://localhost/users/查找元素的id

@GetMapping
    public R getAll(){
        return new R(true,userService.list());
    }

    @GetMapping("{id}")
    public R getById(@PathVariable Integer id){
        return new R(true,userService.getById(id));
    }

 

posted @ 2023-02-12 15:14  一冻不动  阅读(81)  评论(0)    收藏  举报