表现层开发
数据层和业务层都做完了
我们首先得创建我们表现层对应的包和类
Controller层 控制层
先写的是查询所有信息
控制层调用业务层
使用的是依赖注入的方法
package com.bigdata1421.ssmp.controller;
import com.bigdata1421.ssmp.domain.User;
import com.bigdata1421.ssmp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/users")
public class UserController {
//注入业务层
@Autowired
private UserService userService;
//规定请求方式
@GetMapping
public List<User> getAll(){
return userService.geTAll();
}
@PostMapping
public Boolean save(@RequestBody User user){
return userService.save(user);
}
@PutMapping
public Boolean update(@RequestBody User user){
return userService.update(user);
}
@DeleteMapping("{id}")
public Boolean delete(@PathVariable Integer id){
return userService.delete(id);
}
@GetMapping("{id}")
public User getById(@PathVariable Integer id){
return userService.getById(id);
}
}
post是更新数据,put是替换数据
改写
我们通过save update异步提交发送的话参数是通过请求体传json数据过来
@RequestBody注解 代表请求体参数
删除和查单个 我们一般是传单个参数过来
我们使用路径参数传参
用postman发起请求
对应的语句
数据库中已经成功写入数据
查看一下全部
试一下查看刚才添加的数据
查看一下
删掉
这样就删掉了
回顾一下
设置公共的请求路径前缀 users
之后用postman测试数据
完善分页查询
在接口里添加方法
接口的实现类重写接口的方法
在控制层书写代码
用postman测试接口
发送请求
总结
表现层消息统一处理
但是我们返回给前端的数据
有各种各样的
比如说json数组 json 布尔类型的数值
我们得想办法和前端配合在一起工作
我们要修改数据
用data封装数据
但是还是要处理null
查询id
不存在 返回的是null
查询过程中抛出异常 catch中返回null
所以我们要告诉前端人员我们查询成功还是失败
前后端数据协议
就是为了后端发送给前端时数据统一
协议联调
定义Result类 R类
package com.bigdata1421.ssmp.controller.utils;
import lombok.Data;
@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;
}
}
修改控制层的实现类
先把返回值类型都换成Result
先书写带参构造 无参构造
因为一般的话
接收的参数的是boolean 然后数据是null
改写
在书写其他的构造方法
然后去改
package com.bigdata1421.ssmp.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bigdata1421.ssmp.controller.utils.Result;
import com.bigdata1421.ssmp.domain.User;
import com.bigdata1421.ssmp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/users")
public class UserController {
//注入业务层
@Autowired
private UserService userService;
//规定请求方式
@GetMapping
public List<User> getAll(){
return userService.geTAll();
}
@PostMapping
public Result save(@RequestBody User user){
return new Result(userService.save(user));
}
@PutMapping
public Result update(@RequestBody User user){
return new Result(userService.update(user));
}
@DeleteMapping("{id}")
public Result delete(@PathVariable Integer id){
return new Result(true,userService.delete(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去测试
返回了统一响应结果
统一表现层的数据模式
设计表现层的返回结果数据模型
小结
这是后端到前端的数据协议
标签:return,SSMP,id,controller,及用,Result,userService,import,public From: https://blog.csdn.net/qq_30500575/article/details/139320155