定义通用返回json对象
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.List;
@Data
public class ResponsePageResult<T> {
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<T> data;
public static <T> ResponsePageResult success(Long total, List<T> rows, Long page, Long pageSize) {
ResponsePageResult<T> 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<Object> 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());