1.定义Page
public class Page {
private Long totalCount;
private Long pageIndex;
private Long pageSize;
public Page() {
this.pageIndex = 1L;
this.pageSize = 10L;
this.totalCount = 0L;
}
public Page(Long pageIndex) {
this();
pageIndex = pageIndex == null ? 1L : pageIndex;
this.pageSize = 10L;
this.pageIndex = pageIndex;
}
public Page(Long pageIndex, Long pageSize) {
pageIndex = pageIndex == null ? 1L : pageIndex;
pageSize = pageSize == null ? 10L : pageSize;
this.pageIndex = pageIndex;
this.pageSize = pageSize;
}
public Long getPageSize() {
return this.pageSize = this.pageSize < 1L ? 10L : this.pageSize;
}
public void setPageSize(Long pageSize) {
pageSize = pageSize == null ? 10L : pageSize;
this.pageSize = pageSize;
}
public Long getPageIndex() {
return this.pageIndex < 1L ? 1L : this.pageIndex;
}
public void setPageIndex(Long pageIndex) {
pageIndex = pageIndex == null ? 1L : pageIndex;
this.pageIndex = pageIndex;
}
public Long getTotalCount() {
return this.totalCount;
}
public void setTotalCount(Long totalCount) {
if (null == totalCount) {
this.totalCount = 0L;
} else {
this.totalCount = totalCount;
}
}
public Long getPageCount() {
if(null == this.totalCount || null == this.pageSize) {
return 0L;
}
return this.totalCount % this.pageSize > 0L ? this.totalCount / this.pageSize + 1L : this.totalCount / this.pageSize;
}
public RowBounds toRowBounds() {
return new RowBounds(this.pageIndex > 0L ? (int)((this.pageIndex - 1L) * this.pageSize) : 0, this.pageSize > 0L ? this.pageSize.intValue() : 0);
}
public String toString() {
return "Page{totalCount=" + this.totalCount + ", pageIndex=" + this.pageIndex + ", pageSize=" + this.pageSize + '}';
}
}
2.定义Paging
@Data
public class Paging {
@ApiModelProperty(value = "页码", example = "1")
private Integer pageIndex = 1;
@ApiModelProperty(value = "每页条数", example = "20")
private Integer pageSize = 20;
}
3.封装代码
public <T> List<T> getPageInfo(Page respPage, Paging pageInfo, List<T> list) {
respPage.setPageSize(Long.valueOf(pageInfo.getPageSize()));
respPage.setPageIndex(Long.valueOf(pageInfo.getPageIndex()));
respPage.setTotalCount(Long.valueOf(list.size()));
int currentPage = pageInfo.getPageIndex();
int pageSize = pageInfo.getPageSize();
List<T> newList = new ArrayList<>();
if (list != null && list.size() > 0) {
int currIdx = (currentPage > 1 ? (currentPage - 1) * pageSize : 0);
for (int i = 0; i < pageSize && i < list.size() - currIdx; i++) {
newList.add(list.get(currIdx + i));
}
}
return newList;
}