zoukankan      html  css  js  c++  java
  • 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);
        }
  • 相关阅读:
    mysql数据库常用指令
    解决windows的mysql无法启动 服务没有报告任何错误的经验。
    “Can't open file for writing”或“operation not permitted”的解决办法
    启动Apache出现错误Port 80 in use by "Unable to open process" with PID 4!
    如何打开windows的服务services.msc
    常见的HTTP状态码 404 500 301 200
    linux系统常用的重启、关机指令
    (wifi)wifi移植之命令行调试driver和supplicant
    linux(debian)安装USB无线网卡(tp-link TL-WN725N rtl8188eu )
    alloc_chrdev_region申请一个动态主设备号,并申请一系列次设备号
  • 原文地址:https://www.cnblogs.com/wt7018/p/13360110.html
Copyright © 2011-2022 走看看