统一结果返回
1,状态静态常量
package com.mangoubiubiu.commonutils;
public interface ResultCode {
//成功
public static Integer SUCCESS=20000;
//失败
public static Integer ERROR=20001;
}
2,消息调用类,
lombok
//生成getset方法
@Data
//有参构造
@AllArgsConstructor
//无参构造
@NoArgsConstructor
package com.mangoubiubiu.commonutils; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import java.util.HashMap; import java.util.Map; /*** * 统一返回结果 */ @Data public class R { @ApiModelProperty(value = "是否成功") private Boolean success; @ApiModelProperty(value = "返回码") private Integer code; @ApiModelProperty(value = "返回消息") private String message; @ApiModelProperty(value = "返回数据") private Map<String, Object> data = new HashMap<String, Object>(); //构造器私有防止外部调用 private R(){} //成功方法 public static R ok(){ R r=new R(); r.setSuccess(true); r.setCode(ResultCode.SUCCESS); r.setMessage("成功"); return r; } //失败方法 public static R error(){ R r=new R(); r.setSuccess(false); r.setCode(ResultCode.ERROR); r.setMessage("失败"); return r; } public R success(Boolean success){ this.setSuccess(success); return this; } public R message(String message){ this.setMessage(message); return this; } public R code(Integer code){ this.setCode(code); return this; } public R data(String key, Object value){ this.data.put(key, value); return this; } public R data(Map<String, Object> map){ this.setData(map); return this; } }
3.controller调用
package com.mangoubiubiu.edu.controller; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.mangoubiubiu.commonutils.R; import com.mangoubiubiu.edu.entity.Teacher; import com.mangoubiubiu.edu.service.TeacherService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.*; import java.util.List; /** * <p> * 讲师 前端控制器 * </p> * * @author mangoubiubiu * @since 2020-09-22 */ @Api(description = "讲师管理") @RestController @RequestMapping("/eduservice/edu-teacher") public class TeacherController { @Autowired private TeacherService service; //rest风格 /** * 查询所有的教师信息 * @return */ @ApiOperation(value = "所有的教师信息") @GetMapping("findAll") public R findAllTeacher(){ List<Teacher> list= service.list(null); return R.ok().data("items",list); } /** * @DeleteMapping("{id}") 通过路径传递 * @PathVariable String id 得到路径中的id值 * @param id * @return */ @ApiOperation(value = "逻辑删除接口") @DeleteMapping("{id}") public R removeTeacher(@ApiParam(name="id",value = "讲师ID",required = true) @PathVariable String id){ boolean flag=service.removeById(id); if(flag==true){ return R.ok(); }else { return R.error(); } } /** *分页 * @param current 当前页 * @param limit 记录数 * @return */ @ApiOperation(value = "查询教师信息分页接口") @GetMapping("pageTeacher/{current}/{limit}") public R pageListTeacher(@ApiParam(name="current",value = "当前页",required = true) @PathVariable Long current, @ApiParam(name="limit",value = "记录数",required = true) @PathVariable Long limit){ //current:当前页,size:每页的记录数 Page<Teacher> pageInfo=new Page<>(current,limit); //调用方法时底层会做封装,封装到pageInfo里面 service.page(pageInfo,null); long total= pageInfo.getTotal();//总记录数 List<Teacher> records = pageInfo.getRecords();//所有的数据list return R.ok().data("total",total).data("rows",records); } /** * 添加教师接口 * @param * @return */ @PostMapping("addTeacher") public R addTeacher(@RequestBody Teacher eduTeacher){ boolean flag= service.save(eduTeacher); if(flag==true){ return R.ok(); }else { return R.error(); } } // 根据讲师id查询讲师信息 @GetMapping("getTeacher/{id}") public R getTeacher(@PathVariable String id){ Teacher eduTeacher = service.getById(id); return R.ok().data("teacher",eduTeacher); } //讲师修改功能 @PostMapping("updateTeacher") public R updateTeacher(@RequestBody Teacher eduTeacher){ boolean flag= service.updateById(eduTeacher); if(flag==true){ return R.ok(); }else { return R.error(); } } }
4,效果