springboot 路由 json

一、前提条件

1、开启热加载

2、配置mybatis-plus 见 mybatis的博客

二、路由

1、导包

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.60</version>
</dependency>

2、controller 类中添加 注解

知识点

@Controller     // 用于重定向和返回html
@RestController    // 用于返回字符串
@RequestMapping("/test") // 通用路由
@GetMapping("/json") // get请求的路由
@RequestParam    // 请求参数 ?
@PathVariable("name") // restful 风格的请求参数 参数 在url中

案例

a、注入

    @Autowired // 注入
    private UserMapper userMapper;

b、查找

    /*
    1. restful风格的get请求
    2. 请求url http://localhost:8083/test/json/wt
    3. 用到知识点:
        @RequestMapper 用于做二级路由(Django的叫法)
        @GetMapper get请求的专属路由
        @PathVariable restful 风格传递参数
    */
    @GetMapping("/json/{name}")
    public String selectByName(@PathVariable("name") String name){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("name", name);
        List<User> user = userMapper.selectList(wrapper);
        System.out.println(user);
        return JSON.toJSONString(user);
    }
    /*
    根据 id 查找 用户信息
    * 1. 非restful 风格 的请求
    * 2. 访问路径 http://localhost:8083/test/json?userId=5
    * 3. @RequestParam 请求参数 ?
    * */
    @GetMapping("/json")
    public String selectById(@RequestParam("userId") int id){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        User user = userMapper.selectOne(wrapper);
        return JSON.toJSONString(user);
    }

c、增加

    /*
    普通 post 数据
    1. @PostMapping post请求专用路由
    2. @RequestParam 请求参数 ?
    */
    @PostMapping("/post")
    public String insertUser(
            @RequestParam("username") String name, @RequestParam("age") int age, @RequestParam("email") String email
    )
    {
        User user = new User();
        user.setName(name);
        user.setAge(age);
        user.setEmail(email);
        userMapper.insert(user);
        List<User> userList = userMapper.selectList(null);
        return JSON.toJSONString(userList);
    }

    /*
    restful 风格的 post
    @PostMapping post 路由
    @RequestBody 请求体, 默认为 json格式
    工具 postman
    {
    "name": "erty123",
    "age": 45,
    "email": "094@qq.com"
    }
    */
    @PostMapping("/insert")
    public String insertUser2(@RequestBody User user){
        userMapper.insert(user);
        return JSON.toJSONString(user);
    }

d、修改

 

    /*
    1. restful 更新
    2. @RequestBody 请求体,json格式
    3. @PutMapping restful 专用修改数据路由
    */
    @PutMapping("/update")
    public  String updateUser(@RequestBody User user){
        System.out.println("============================");
        System.out.println(user);
        userMapper.updateById(user);
        return JSON.toJSONString(user);
    }
    
    /*
    非restful 格式
    使用GetMapper更新
    添加乐观锁    
    注意: 数据类型使用 包装类
    */
    @GetMapping("/update1")
    public String UpdateUser2(@RequestParam("id") Long id, @RequestParam("name") String name, @RequestParam("email") String email){
        User user = userMapper.selectById(id);
        user.setName(name);
        user.setEmail(email);
        user.setAge(101);
        userMapper.updateById(user);
        return JSON.toJSONString(user);
    }

e、删除

    /*
    1. restful 格式
    2. @DeleteMapping 删除路由
    */
    @DeleteMapping("/delete/{userId}")
    public String deleteUser(@PathVariable("userId") Long id){
        User user = userMapper.selectById(id);
        userMapper.deleteById(id);
        return JSON.toJSONString(user);
    }
    /*
    非restful
    1.  @RequestParam 请求参数
    */
    @DeleteMapping("/deleteUser")
    public String deleteUser2(@RequestParam("userId") int id){
        System.out.println("====================================================");
        System.out.println(id);
        User user = userMapper.selectById(id);
        userMapper.deleteById(id);
        return JSON.toJSONString(user);
    }

 

posted @ 2020-07-22 13:15  市丸银  阅读(446)  评论(0编辑  收藏  举报