返回分页数据列表
   
    
    
    问题描述
   
    现服务端数据库有users表如下:
    
    
    
    前端需要获取其分页数据列表。
   
    
    
    实现思路
   
    
    
    1. 接收前端参数
   
使用PageParam类来接收分页请求的参数。
public class PageParam {
    private int beginLine;// 当前行
    private Integer pageSize = 3;
    private Integer currentPage = 0;// 从0开始
    public int getBeginLine() {
        return pageSize * currentPage;
    }
    public Integer getPageSize() {
        return pageSize;
    }
    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }
    public Integer getCurrentPage() {
        return currentPage;
    }
    public void setCurrentPage(Integer currentPage) {
        this.currentPage = currentPage;
    }
}
进一步编写PageParam的子类UserParam。
UserParam类的作用在于,除了可以封装基本的分页请求参数外,还可以封装筛选的条件。(例如,需要获取的是userSex=”Man”的User的列表时,可以用UserParam。)
public class UserParam extends PageParam{
    private String userName;
    private String userSex;
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getUserSex() {
        return userSex;
    }
    public void setUserSex(String userSex) {
        this.userSex = userSex;
    }
}
    
    
    2. 服务端处理分页请求
   
@RestController("/")
public class UserController {
    @Resource
    private UserMapper userMapper;
    
    @GetMapping("users")
    public BaseResult<PageResult<UserEntity>> getUserList(UserParam userParam){
        List<UserEntity> userEntityList = userMapper.getList(userParam);
        long totalNumber = userMapper.getCount(userParam);
        PageResult<UserEntity> pageResult = new PageResult<>(userParam,totalNumber,userEntityList);
        return BaseResult.successWithData(pageResult);
    }
}
注:在本例中,ORM框架为MyBatis。
服务端执行两次SQL操作,分别获取了user集合、分页请求数据的总数。
    
    
    3. 将数据封装好后返回前端
   
    然后,服务端将生成PageResult对象,再将其封装进BaseResult中返回给前端。
    
     (代码见上面)
    
   
    
     PageResult.java
    
   
public class PageResult<E>{
    private  int currentPage = 1;
    private long totalPage;
    private long totalNumber;
    private List<E> list;
    public PageResult(){
    }
    public PageResult(PageParam pageParam,long totalNumber,List<E> list){
        this.currentPage = pageParam.getCurrentPage();
        this.totalPage = totalNumber % pageParam.getPageSize() == 0 ? totalNumber
                / pageParam.getPageSize() : totalNumber / pageParam.getPageSize() + 1;
        this.totalNumber = totalNumber;
        this.list = list;
    }
    public int getCurrentPage() {
        return currentPage;
    }
    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }
    public long getTotalPage() {
        return totalPage;
    }
    public void setTotalPage(long totalPage) {
        this.totalPage = totalPage;
    }
    public long getTotalNumber() {
        return totalNumber;
    }
    public void setTotalNumber(long totalNumber) {
        this.totalNumber = totalNumber;
    }
    public List<E> getList() {
        return list;
    }
    public void setList(List<E> list) {
        this.list = list;
    }
}
    
     BaseResult.java
    
   
/**
 * 通用响应对象
 */
public class BaseResult<T> {
    private static final int SUCCESS_CODE = 0;
    private static final String SUCCESS_MESSAGE = "成功";
    private int code;
    private String msg;
    private T data;
    private BaseResult(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }
    private BaseResult() {
        this(SUCCESS_CODE, SUCCESS_MESSAGE);
    }
    private BaseResult(int code, String msg) {
        this(code, msg, null);
    }
    private BaseResult(T data) {
        this(SUCCESS_CODE, SUCCESS_MESSAGE, data);
    }
    public static <T> BaseResult<T> success() {
        return new BaseResult<>();
    }
    public static <T> BaseResult<T> successWithData(T data) {
        return new BaseResult<>(data);
    }
    public static <T> BaseResult<T> failWithCodeAndMsg(int code, String msg) {
        return new BaseResult<>(code, msg, null);
    }
    public static <T> BaseResult<T> buildWithParam(ResponseParam param) {
        return new BaseResult<>(param.getCode(), param.getMsg(), null);
    }
    public int getCode() {
        return code;
    }
    public void setCode(int code) {
        this.code = code;
    }
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
    public T getData() {
        return data;
    }
    public void setData(T data) {
        this.data = data;
    }
    public static class ResponseParam {
        private int code;
        private String msg;
        private ResponseParam(int code, String msg) {
            this.code = code;
            this.msg = msg;
        }
        public static ResponseParam buildParam(int code, String msg) {
            return new ResponseParam(code, msg);
        }
        public int getCode() {
            return code;
        }
        public void setCode(int code) {
            this.code = code;
        }
        public String getMsg() {
            return msg;
        }
        public void setMsg(String msg) {
            this.msg = msg;
        }
    }
}
    
    
    4. 测试结果
   
    
    
    测试1
   
请求方式及参数:
    
    
    响应结果:
   
    
    
    
     注:currentPage是从0开始的。
    
   
    
    
    测试2
   
请求方式及参数:
    
    
    
     注:此时没有userSex的筛选条件。
    
   
响应结果:
{
    "code": 0,
    "msg": "成功",
    "data": {
        "currentPage": 0,
        "totalPage": 2,
        "totalNumber": 4,
        "list": [
            {
                "id": 4,
                "userName": "Pony",
                "passWord": "QWERTY",
                "userSex": "Man",
                "nickName": "P"
            },
            {
                "id": 3,
                "userName": "Mary",
                "passWord": "QWERTY",
                "userSex": "Man",
                "nickName": "Ma"
            },
            {
                "id": 2,
                "userName": null,
                "passWord": "QWERTY",
                "userSex": "Man",
                "nickName": "Ma"
            }
        ]
    }
}
 
版权声明:本文为a739260008原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
