基于java springboot微服务实现通用分页查询接口

定义通用返回json对象

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.List;
@Data
public class ResponsePageResult {
    private String message = "success";
    private int code;
    @ApiModelProperty(value = "总条数")
    private Long total;
    @ApiModelProperty(value = "页码")
    private Long page = 1L;
    @ApiModelProperty(value = "每页大小")
    private Long pageSize = 10L;
    @ApiModelProperty(value = "总页数")
    private Long totalPages;
    @ApiModelProperty(value = "分页数据")
    private List data;

    public static  ResponsePageResult success(Long total, List rows, Long page, Long pageSize) {
        ResponsePageResult pageResult = new ResponsePageResult();
        pageResult.setTotal(total);
        pageResult.setData(rows);
        pageResult.setPage(page);
        pageResult.setPageSize(pageSize);
        pageResult.setTotalPages(total <= 0 ? 0L : ((total - 1) / pageSize + 1));
        return pageResult;
    }
}

定义Service接口

ResponsePageResult queryPageListDefault(String key, String queryCode, Long pageNum, Long pageSize); 
  

查询全量数据sql

SELECT t.* FROM ${tName} t

lambda分页返回

list = list.stream().skip((pageNum - 1) * pageSize).limit(pageSize).collect(toList());

你可能感兴趣的:(java,java,json,spring)