java自定义分页模板
Posted it蛰伏者
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java自定义分页模板相关的知识,希望对你有一定的参考价值。
有些情况下使用分页插件并不合适,这时候需要我们自定义分页插件模板,下面是笔者自己写的分页模板可以根据需要自己更改:
/* * Copyright (C) 2018 Baidu, Inc. All Rights Reserved. */ package com.legal.domain; import java.util.List; /** * 分页 * * @author * @since 2018/11/22 19:01 */ public class PageHelper { // 总条数 private Integer totalCount; // 总页数 private Integer totalPages; // 每页条数 private Integer limit; // 当前页码从1开始 private Integer page; // 前一页 private Integer prePage; // 是否首页 private Boolean firstPage; // 是否末页 private Boolean lastPage; // 下一页 private Integer nextPage; // 偏移量 private Integer offset; // 是否有下页 private Boolean hasNextPage; // 是否有上页 private Boolean hasPrePage; // 当前页从第几条开始查 private Integer startRow; // 返回数据 private List items; public PageHelper pagination(Integer totalCount, Integer limit, Integer page, List items) { PageHelper pageHelper = new PageHelper(); pageHelper.setTotalCount(totalCount); Integer totalPage = totalCount % limit == 0 ? totalCount / limit : totalCount / limit + 1; pageHelper.setTotalPages(totalPage); pageHelper.setPage(page); pageHelper.setLimit(limit); pageHelper.setOffset(limit); pageHelper.setStartRow(limit * (page - 1)); if (page > 1 && page < totalPage) { firstPage = false; hasPrePage = true; nextPage = page + 1; prePage = page - 1; } else if (page == totalPage && page != 1) { lastPage = true; hasNextPage = false; firstPage = false; hasPrePage = true; prePage = page - 1; } else if (page == 1 && page != totalPage) { lastPage = false; hasNextPage = true; firstPage = true; hasPrePage = false; nextPage = page + 1; } else if (page == totalPage && totalPage == 1) { lastPage = true; hasNextPage = false; firstPage = true; hasPrePage = false; } pageHelper.setLastPage(lastPage); pageHelper.setFirstPage(firstPage); pageHelper.setHasNextPage(hasNextPage); pageHelper.setHasPrePage(hasPrePage); pageHelper.setPrePage(prePage); pageHelper.setNextPage(nextPage); int toIndex = 0; if (pageHelper.getStartRow() + pageHelper.getLimit() >= pageHelper.getTotalCount()) { toIndex = pageHelper.getTotalCount(); } else { toIndex = pageHelper.getStartRow() + pageHelper.getLimit(); } if (pageHelper.getStartRow() > toIndex) { this.items = null; } else { this.items = items.subList(pageHelper.getStartRow(), toIndex); } pageHelper.setItems(this.items); return pageHelper; } public Integer getTotalCount() { return totalCount; } public void setTotalCount(Integer totalCount) { this.totalCount = totalCount; } public Integer getTotalPages() { return totalPages; } public void setTotalPages(Integer totalPages) { this.totalPages = totalPages; } public Integer getLimit() { return limit; } public void setLimit(Integer limit) { this.limit = limit; } public Integer getPage() { return page; } public void setPage(Integer page) { this.page = page; } public Integer getPrePage() { return prePage; } public void setPrePage(Integer prePage) { this.prePage = prePage; } public Boolean getFirstPage() { return firstPage; } public void setFirstPage(Boolean firstPage) { this.firstPage = firstPage; } public Boolean getLastPage() { return lastPage; } public void setLastPage(Boolean lastPage) { this.lastPage = lastPage; } public Integer getNextPage() { return nextPage; } public void setNextPage(Integer nextPage) { this.nextPage = nextPage; } public Integer getOffset() { return offset; } public void setOffset(Integer offset) { this.offset = offset; } public Boolean getHasNextPage() { return hasNextPage; } public void setHasNextPage(Boolean hasNextPage) { this.hasNextPage = hasNextPage; } public Boolean getHasPrePage() { return hasPrePage; } public void setHasPrePage(Boolean hasPrePage) { this.hasPrePage = hasPrePage; } public Integer getStartRow() { return startRow; } public void setStartRow(Integer startRow) { this.startRow = startRow; } public List getItems() { return items; } public void setItems(List items) { this.items = items; } }
以上是关于java自定义分页模板的主要内容,如果未能解决你的问题,请参考以下文章