package com.itheima.controller;
//此类表示和前端的数据协议
public class Reoult {
private Object data;
private Integer code;
private String massge;
public Reoult(Integer integer) {
}
public Reoult( Integer code,Object data) {
this.data = data;
this.code = code;
}
public Reoult(Integer code,Object data, String massge) {
this.data = data;
this.code = code;
this.massge = massge;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMassge() {
return massge;
}
public void setMassge(String massge) {
this.massge = massge;
}
}
package com.itheima.controller;
//表示成功失败的状态码
public class Code {
//1表示成功
public static final Integer SAVE_OK=2001;
public static final Integer UPDATE_OK=2001;
public static final Integer DELETE_OK=2001;
public static final Integer GET_OK=2001;
//0表示成功
public static final Integer SAVE_ERR=2000;
public static final Integer UPDATE_ERR=2000;
public static final Integer DELETE_ERR=2000;
public static final Integer GET_ERR=2000;
}
package com.itheima.controller;
import com.itheima.dao.userDao;
import com.itheima.service.Userservice;
import org.apache.ibatis.annotations.Delete;
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;
@PostMapping
public Reoult save(@RequestBody userDao userDao) {
boolean flag = userservice.save(userDao);
return new Reoult(flag ? Code.SAVE_OK : Code.SAVE_ERR,flag);
}
@PutMapping
public Reoult update(@RequestBody userDao userDao) {
boolean flag = userservice.update(userDao);
return new Reoult(flag ? Code.UPDATE_OK: Code.UPDATE_ERR,flag);
}
@Delete("/{id}")
public Reoult delete(@PathVariable Integer id) {
boolean flag = userservice.delete(id);
return new Reoult(flag ? Code.DELETE_OK: Code.DELETE_ERR,flag);
}
@GetMapping("/{id}")
public Reoult getbyid(@PathVariable Integer id) {
userDao userDao = userservice.getbyid(id);
Integer code = userDao !=null ?Code.GET_OK :Code.GET_ERR;
String massge = userDao !=null ? "":"查询失败,请重试";
return new Reoult(code,userDao,massge);
}
@GetMapping
public Reoult getall() {
List<userDao> getall = userservice.getall();
Integer code = getall !=null ?Code.GET_OK :Code.GET_ERR;
String massge = getall !=null ? "":"查询失败,请重试";
return new Reoult(code,getall,massge);
}
}