1. 程式人生 > 其它 >統一返回結果R

統一返回結果R

技術標籤:Mybatis-plus

//同意返回結果
@Data
public class R {

    //這個註解是swagger的註解
    @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() {}//構造方法私有化 防止在外部new 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; } //下面四個方法返回this是鏈式程式設計 可以對類中的每一個屬性單獨設定 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; } }
public interface ResultCode {

    public static Integer SUCCESS = 20000;//成功

    public static Integer ERROR = 20001;//失敗


}