表现层开发
编写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测试成功
启动项目
继续编写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(查)
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){
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(查)
getById(查)
update(改)
save(增)
delete(删)
小结
|