通用的自定义相应类,轻便,兼容好,基本没有冗余字段
Result类
import java.io.Serializable;
/**
* Description:
*
* @Author: Yanggc
* DateTime: 10/22 13:52
*/
public class Result<T> implements Serializable {
private static final long serialVersionUID = -3948389268046368059L;
private Integer code;
private String msg;
private T data ;
Result() {
}
private Result(T data) {
this.code = 200;
this.msg = "SUCCESS";
this.data = data;
}
private Result(Integer code, String msg, T data) {
this.code = code;
this.msg = msg;
this.data = data;
}
public static <T> Result<T> success() {
Result result = new Result<T>();
result.setResultCode(ResultCodeEnum.SUCCESS);
return result;
}
public static <T> Result<T> failure(ResultCodeEnum resultCodeEnum) {
Result result = new Result();
result.setResultCode(resultCodeEnum);
return result;
}
//自定义相应失败值
public static <T> Result<T> failure(String failMsg) {
Result result = new Result();
result.setMsg(failMsg);
result.setCode(201);
return result;
}
public void setResultCode(ResultCodeEnum code) {
this.code = code.code();
this.msg = code.message();
}
// 自定义返回数据
public Result<T> data(T data) {
this.setData(data);
return this;
}
// 自定义状态信息
public Result msg(String message) {
this.setMsg(message);
return this;
}
// 自定义状态码
public Result code(Integer code) {
this.setCode(code);
return this;
}
/**
* getter setter
*/
public Integer getCode() {
return code;
}
private void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
private void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
private void setData(T data) {
this.data = data;
}
}
枚举类:ResultCodeEnum
/**
* Description:
*
* @Author: Yanggc
* DateTime: 10/22 13:54
*/
public enum ResultCodeEnum {
/* 状态码 */
UNKNOWN_ERROR(404, "未知错误"),
SUCCESS(200,"成功"),
ERROR (201,"失败");
private Integer code;
private String message;
ResultCodeEnum(Integer code, String message) {
this.code = code;
this.message = message;
}
public Integer code() {
return this.code;
}
public String message() {
return this.message;
}
public static String getMessage(String name) {
for (ResultCodeEnum item : ResultCodeEnum.values()) {
if (item.name().equals(name)) {
return item.message;
}
}
return name;
}
public static Integer getCode(String name) {
for (ResultCodeEnum item : ResultCodeEnum.values()) {
if (item.name().equals(name)) {
return item.code;
}
}
return null;
}
}
自定义分页响应类
/**
* Description:
*
* @Author: Yanggc
* DateTime: 10/23 12:24
*/
public class PageResult<T> extends Result<T> {
//总记录数
private Long total;
//每页记录数
private Long pageSize;
//总页数
private Long totalPages;
//当前页码
private Long currPage;
private PageResult() {
}
public PageResult(Long total, Long pageSize, Long totalPages, Long currPage) {
this.total = total;
this.pageSize = pageSize;
this.totalPages = totalPages;
this.currPage = currPage;
}
public static <T> PageResult<T> success() {
PageResult<T> pageResult = new PageResult<>();
pageResult.setResultCode(ResultCodeEnum.SUCCESS);
return pageResult;
}
public static <T> PageResult<T> failure(ResultCodeEnum resultCodeEnum) {
PageResult<T> pageResult = new PageResult<>();
pageResult.setResultCode(resultCodeEnum);
return pageResult;
}
/**
* getter setter
*/
public Long getTotal() {
return total;
}
public void setTotal(Long total) {
this.total = total;
}
public Long getPageSize() {
return pageSize;
}
public void setPageSize(Long pageSize) {
this.pageSize = pageSize;
}
public Long getTotalPages() {
return totalPages;
}
public void setTotalPages(Long totalPages) {
this.totalPages = totalPages;
}
public Long getCurrPage() {
return currPage;
}
public void setCurrPage(Long currPage) {
this.currPage = currPage;
}
Mybatis-Plus 返回结果二次封装
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
/**
* Description:
* Mybatis-plus 分页条件结果二次封装
*
*
* @Author: Yanggc
* DateTime: 10/12 12:24
*/
public class PageResult<T> {
private Integer code;
private String msg;
private Object data ;
//总记录数
private Long total;
//每页记录数
private Long pageSize;
//总页数
private Long totalPages;
//当前页码
private Long currPage;
public void setResultCode(ResultCodeEnum code) {
this.code = code.code();
this.msg = code.message();
}
private PageResult() {
//设置默认值
this.total = 0L;
this.pageSize = 0L;
this.totalPages = 1L;
this.currPage = 1L;
}
public static <T> PageResult<T> success(Page<T> page) {
PageResult<Object> pageResult = new PageResult<>();
pageResult.setResultCode(ResultCodeEnum.SUCCESS);
pageResult.setData(page.getRecords());
pageResult.setCurrPage(page.getCurrent());
pageResult.setTotal(page.getTotal());
pageResult.setPageSize(page.getSize());
pageResult.setTotalPages(page.getPages());
return (PageResult<T>) pageResult;
}
public static <T> PageResult<T> failure(String failMsg) {
PageResult<T> pageResult = new PageResult<T>();
// 201 自定义失败标识消息
pageResult.setCode(201);
pageResult.setMsg(failMsg);
return pageResult;
}
public static <T> PageResult<T> failure(ResultCodeEnum resultCodeEnum) {
PageResult<T> pageResult = new PageResult<T>();
pageResult.setResultCode(resultCodeEnum);
return pageResult;
}
// 自定义返回数据类型
public PageResult<T> data(Object data) {
this.setData(data);
return this;
}
/**
* getter setter
*/
public Integer getCode() {
return code;
}
private void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public PageResult<T> setMsg(String msg) {
this.msg = msg;
return this;
}
public Object getData() {
return data;
}
private void setData(Object data) {
this.data = data;
}
public Long getTotal() {
return total;
}
private void setTotal(Long total) {
this.total = total;
}
public Long getPageSize() {
return pageSize;
}
private void setPageSize(Long pageSize) {
this.pageSize = pageSize;
}
public Long getTotalPages() {
return totalPages;
}
private void setTotalPages(Long totalPages) {
this.totalPages = totalPages;
}
public Long getCurrPage() {
return currPage;
}
private void setCurrPage(Long currPage) {
this.currPage = currPage;
}
}
使用方法
对查询出来的Page对象进行一键封装
public PageResult onsaleMembercardList(@RequestBody MemcardPageModel memberCardModel){
Page<MemberCardProduct> memberCardProductPage = memcardService.searchMemcard(memberCardModel);
return PageResult.success(memberCardProductPage);
}
{
"code": 200,
"msg": "成功",
"data": [{"id":1,"name": "张三"}],
"total": 30,
"pageSize": 1,
"totalPages": 30,
"currPage": 3
}