public class R { @ApiModelProperty(value = "是否成功") private Boolean success; @ApiModelProperty(value = "返回码") private Integer code; @ApiModelProperty(value = "返回消息") private String message; @ApiModelProperty(value = "返回数据") private Object data; private R(){} public static R notfind(){ R r = new R(); r.setSuccess(true); r.setCode(ResultCode.NOTFIND); r.setMessage("没有找到数据"); return 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(Object value){ this.setData(value); return this; } }
public class PageBean<T> { private Integer currPage;//当前页数 private Integer pageSize;//每页显示的记录数 private Integer total;//总记录数 private Integer totalPage;//总页数 private List<T> records;//每页的显示的数据 }
public class PageUtils { public static <T> PageBean<T> listTopage(Integer currentPage, Integer pageSize, List<T> list){ if (null == currentPage){ currentPage = 1; } if (null == pageSize){ pageSize = 20; } PageBean<T> pageBean = new PageBean<T>(); //页面对象 pageBean.setCurrPage(currentPage); //封装当前页数 pageBean.setPageSize(pageSize);//每页显示的数据数目 //封装总记录数 int totalCount = list.size(); pageBean.setTotal(totalCount); //封装总页数 double tc = totalCount; Double num=Math.ceil(tc/pageSize);//向上取整 pageBean.setTotalPage(num.intValue()); //封装该页list int start=(currentPage-1)*pageSize; if (start + pageSize > totalCount) pageBean.setRecords(list.subList(start, totalCount)); else pageBean.setRecords(list.subList(start, start + pageSize)); return pageBean; } } 使用示例: public R getList(Integer pageNo, Integer pageSize) { List<Object> data= firesceneService.getList(); PageBean<Object> page = PageUtils.listTopage(pageNo, pageSize, data); return R.ok().data(page); }