栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot -基于SpringBoot的SSMP整合案例(四)-表现层开发(前后端数据协议统一)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot -基于SpringBoot的SSMP整合案例(四)-表现层开发(前后端数据协议统一)

文章目录
  • 表现层开发
    • 编写Controller类
      • postman测试成功
      • 继续编写Controller
      • postman测试
        • getById(查)
        • post(增)
        • update(改)
        • delete(删)
    • 小结
        • 补充-分页查询
  • 表现层消息一致性处理(前后端分离)
    • 统一格式
      • 使用data
    • 案例演示
      • 编写Result.java
      • 备份一份UserController.java
      • postman测试
        • getAll(查)
        • getById(查)
        • update(改)
        • save(增)
        • delete(删)
    • 小结

表现层开发

编写Controller类
package com.taotao.controller;

import com.taotao.domain.User;
import com.taotao.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@SuppressWarnings({"all"})
@RestController
@RequestMapping("/users")
public class UserController {
    //注入业务层
    @Autowired
    private IUserService userService;

    @GetMapping 
    public List getAll() {
        return userService.list();
    }
}
postman测试成功

启动项目

继续编写Controller

增添其他方法

package com.taotao.controller;

import com.taotao.domain.User;
import com.taotao.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@SuppressWarnings({"all"})
@RestController
@RequestMapping("/users")
public class UserController {
    //注入业务层
    @Autowired
    private IUserService userService;

    //获取所有数据
    @GetMapping
    public List getAll() {
        return userService.list();
    }

    //存储数据
    @PostMapping

    public Boolean save(@RequestBody User user){    //@RequestBody请求体参数
        return userService.save(user);
    }

    //更新数据
    @PutMapping
    public Boolean update(@RequestBody User user){
        return userService.modify(user);
    }

    //删除数据
    @DeleteMapping("{id}")
    public Boolean delete(@PathVariable Integer id){
        return userService.delete(id);
    }

    //http://localhost:80/users/2
    //根据Id查询
    @GetMapping("{id}")
    public User getById(@PathVariable Integer id){
        return userService.getById(id);
    }

}

postman测试 getById(查)

post(增)

update(改)

delete(删)

小结

补充-分页查询

service接口-MP

service实现类-MP

controller.java

测试成功

表现层消息一致性处理(前后端分离) 统一格式

使用data

注意查询结果为null时情况

案例演示 编写Result.java
package com.taotao.controller.utils;

import lombok.Data;


@SuppressWarnings({"all"})
@Data
public class Result {
    private boolean flag;
    private Object data;

    public Result(){

    }

    public Result(Boolean flag){
        this.flag = flag;
    }
    
    public Result(Boolean flag,Object data){
        this.flag = flag;
        this.data = data;
    }
}

备份一份UserController.java

注销其中一个的@RestController注解,使此类不再加载为bean,使启动服务器时访问不冲突

编写其中一个

package com.taotao.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.taotao.controller.utils.Result;
import com.taotao.domain.User;
import com.taotao.service.IUserService;
import com.taotao.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@SuppressWarnings({"all"})
@RestController
@RequestMapping("/users")
public class UserController2 {
    //注入业务层
    @Autowired
    private IUserService userService;

    //获取所有数据
    @GetMapping
    public Result getAll() {
        return new Result(true,userService.list());
    }

    //存储数据
    @PostMapping
    public Result save(@RequestBody User user){    //@RequestBody请求体参数
        return new Result(userService.save(user));
    }

    //更新数据
    @PutMapping
    public Result update(@RequestBody User user){
        return new Result(userService.modify(user));
    }

    //删除数据
    @DeleteMapping("{id}")
    public Result delete(@PathVariable Integer id){
        return new Result(userService.delete(id));
    }

    //http://localhost:80/users/2
    //根据Id查询
    @GetMapping("{id}")
    public Result getById(@PathVariable Integer id){
        return new Result(true,userService.getById(id));
    }

    @GetMapping("{currentPage}/{pageSize}")
    public Result getPage(@PathVariable int currentPage,@PathVariable int pageSize){
        return new Result(true,userService.getPage(currentPage,pageSize));
    }
}

postman测试 getAll(查)

getById(查)

update(改)

save(增)

delete(删)

小结

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/862800.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号