表现层开发
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2mR9TfOP-1651741701827)(springboot.assets/image-20220504231506825.png)]](https://img-blog.csdnimg.cn/57219f413e12415b87f933e49d1f88d7.png)
编写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<User> getAll() {
return userService.list();
}
}
postman测试成功
启动项目
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Ot06QPlq-1651741701829)(springboot.assets/image-20220504232326859.png)]](https://img-blog.csdnimg.cn/66e3753b01ec4d15817776d214e5ed45.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cfHCm6vo-1651741701830)(springboot.assets/image-20220504232804331.png)]](https://img-blog.csdnimg.cn/eec16c3092254c2abcaa8c31d5bef46e.png)
继续编写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<User> getAll() {
return userService.list();
}
@PostMapping
public Boolean save(@RequestBody User user){
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);
}
@GetMapping("{id}")
public User getById(@PathVariable Integer id){
return userService.getById(id);
}
}
postman测试
getById(查)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MedGT90x-1651741701830)(springboot.assets/image-20220504234605007.png)]](https://img-blog.csdnimg.cn/800da51b590f4cf7806081339180d247.png)
post(增)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nqhlmddl-1651741701831)(springboot.assets/image-20220505091340757.png)]](https://img-blog.csdnimg.cn/afd519d0b9264da49cd4d0092032dfdc.png)
update(改)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mbjhHV2J-1651741701832)(springboot.assets/image-20220505000243061.png)]](https://img-blog.csdnimg.cn/3a22b64180304ca0a155480203df5488.png)
delete(删)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5Od1oYOl-1651741701833)(springboot.assets/image-20220505091446493.png)]](https://img-blog.csdnimg.cn/6c6348a6b33b4710b13e38a4565e5edc.png)
小结
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-gLJHvyIL-1651741701834)(springboot.assets/image-20220505091612880.png)]](https://img-blog.csdnimg.cn/3c967bf36d62485287fc49b97818cefe.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kcGwXj0m-1651741701835)(springboot.assets/image-20220505091633599.png)]](https://img-blog.csdnimg.cn/6c5e4c39edf84b3288e60eb4d45c17f4.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TM2zgs1B-1651741701835)(springboot.assets/image-20220505094056831.png)]](https://img-blog.csdnimg.cn/0484a1be249244d0972c6ffd2d6a8f07.png)
补充-分页查询
service接口-MP
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UHFBfgo7-1651741701836)(springboot.assets/image-20220505092027365.png)]](https://img-blog.csdnimg.cn/9a1d7b854e9841a69d5e678328db5a21.png)
service实现类-MP
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OojfqW3d-1651741701836)(springboot.assets/image-20220505093433515.png)]](https://img-blog.csdnimg.cn/4aa40b049dd0471eb4e3efa93d9b2497.png)
controller.java
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LVcETueS-1651741701836)(springboot.assets/image-20220505093926757.png)]](https://img-blog.csdnimg.cn/4b717d2021d74b6db06caeb0a75235ad.png)
测试成功
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-iWtdeTQi-1651741701836)(springboot.assets/image-20220505094011027.png)]](https://img-blog.csdnimg.cn/2747fa4b764b4233b40e21121d31209f.png)
表现层消息一致性处理(前后端分离)
统一格式
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-D32JEv7F-1651741701837)(springboot.assets/image-20220505120645189.png)]](https://img-blog.csdnimg.cn/7efc710bdbc94d54a79233be541c7bbb.png)
使用data
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0yzvH1IN-1651741701837)(springboot.assets/image-20220505120825661.png)]](https://img-blog.csdnimg.cn/b741d0f1905540ef9d83d414ffbd1dcf.png)
注意查询结果为null时情况
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-M5JjCJHP-1651741701838)(springboot.assets/image-20220505121006605.png)]](https://img-blog.csdnimg.cn/c7d6244dc34242d59d562e597603ae30.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5D47IB6O-1651741701839)(springboot.assets/image-20220505121309695.png)]](https://img-blog.csdnimg.cn/1c3c275262354d80a5469808cbc9ceb5.png)
案例演示
编写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,使启动服务器时访问不冲突
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-c7a942tZ-1651741701839)(springboot.assets/image-20220505122023305.png)]](https://img-blog.csdnimg.cn/add19bffe6114c93b2bd3292b94c7e03.png)
编写其中一个
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){
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));
}
@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(查)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jHoEMVjd-1651741701840)(springboot.assets/image-20220505165846472.png)]](https://img-blog.csdnimg.cn/741e268ee74c4e79994e3ebb291f360d.png)
getById(查)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rulP7om1-1651741701840)(springboot.assets/image-20220505165951730.png)]](https://img-blog.csdnimg.cn/eb3fa6a5f0ed4cf0a6c518fb8c1ceacb.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4ziGN1XY-1651741701841)(springboot.assets/image-20220505170001698.png)]](https://img-blog.csdnimg.cn/22de0b62fa84457a84541c8f30d77b1b.png)
update(改)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-yt4fpUFb-1651741701841)(springboot.assets/image-20220505170155717.png)]](https://img-blog.csdnimg.cn/36384f9cd0334a59aa64102002763ce3.png)
save(增)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7S3hPbpQ-1651741701841)(springboot.assets/image-20220505170735159.png)]](https://img-blog.csdnimg.cn/acf62ca9893d4587aea3bfc02e80e95a.png)
delete(删)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RjdWkqzj-1651741701842)(springboot.assets/image-20220505170814910.png)]](https://img-blog.csdnimg.cn/4a9674ca2b67411cb76ab532d28aa5aa.png)
小结
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Yj7xHh06-1651741701842)(springboot.assets/image-20220505170230457.png)]](https://img-blog.csdnimg.cn/a8a59f05609f49b78b6596c5aee6a9f9.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BpJ0dWUm-1651741701843)(springboot.assets/image-20220505170257063.png)]](https://img-blog.csdnimg.cn/aa536ee235734b7aa83267f9485a736a.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Idiy6RKJ-1651741701843)(springboot.assets/image-20220505170315624.png)]](https://img-blog.csdnimg.cn/29cced5cbb864e588942de9c68132ee2.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-t1y7P7bY-1651741701843)(springboot.assets/image-20220505170323883.png)]](https://img-blog.csdnimg.cn/f6bb32049ac54512872f79d850279c8b.png)
|