封装返回的页码信息

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.封装代码

    /**
     * 封装返回的页码信息
     * @param respPage
     * @param pageInfo
     * @param list
     * @param <T>
     * @return
     */
    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;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值