1. page.java
java 代码
- package com.sclh.rsp.registercenter.common;
- public class Page {
- /** imply if the page has previous page */
- private boolean hasPrePage;
- /** imply if the page has next page */
- private boolean hasNextPage;
- /** the number of every page */
- private int everyPage;
- /** the total page number */
- private int totalPage;
- /** the number of current page */
- private int currentPage;
- /** the begin index of the records by the current query */
- private int beginIndex;
- private int totalCount;
- /** The default constructor */
- public Page() {
- }
- /** construct the page by everyPage
- * @param everyPage
- * */
- public Page(int everyPage) {
- this.everyPage = everyPage;
- }
- /** The whole constructor */
- public Page(boolean hasPrePage, boolean hasNextPage, int everyPage,
- int totalPage, int currentPage, int beginIndex,int totalCount) {
- this.hasPrePage = hasPrePage;
- this.hasNextPage = hasNextPage;
- this.everyPage = everyPage;
- this.totalPage = totalPage;
- this.currentPage = currentPage;
- this.beginIndex = beginIndex;
- this.totalCount = totalCount;
- }
- /**
- * @return
- * Returns the beginIndex.
- */
- public int getBeginIndex() {
- return beginIndex;
- }
- /**
- * @param beginIndex
- * The beginIndex to set.
- */
- public void setBeginIndex(int beginIndex) {
- this.beginIndex = beginIndex;
- }
- /**
- * @return
- * Returns the currentPage.
- */
- public int getCurrentPage() {
- return currentPage;
- }
- /**
- * @param currentPage
- * The currentPage to set.
- */
- public void setCurrentPage(int currentPage) {
- this.currentPage = currentPage;
- }
- /**
- * @return
- * Returns the everyPage.
- */
- public int getEveryPage() {
- return everyPage;
- }
- /**
- * @param everyPage
- * The everyPage to set.
- */
- public void setEveryPage(int everyPage) {
- this.everyPage = everyPage;
- }
- /**
- * @return
- * Returns the hasNextPage.
- */
- public boolean getHasNextPage() {
- return hasNextPage;
- }
- /**
- * @param hasNextPage
- * The hasNextPage to set.
- */
- public void setHasNextPage(boolean hasNextPage) {
- this.hasNextPage = hasNextPage;
- }
- /**
- * @return
- * Returns the hasPrePage.
- */
- public boolean getHasPrePage() {
- return hasPrePage;
- }
- /**
- * @param hasPrePage
- * The hasPrePage to set.
- */
- public void setHasPrePage(boolean hasPrePage) {
- this.hasPrePage = hasPrePage;
- }
- /**
- * @return Returns the totalPage.
- *
- */
- public int getTotalPage() {
- return totalPage;
- }
- /**
- * @param totalPage
- * The totalPage to set.
- */
- public void setTotalPage(int totalPage) {
- this.totalPage = totalPage;
- }
- public int getTotalCount() {
- return totalCount;
- }
- public void setTotalCount(int totalCount) {
- this.totalCount = totalCount;
- }
- }
2. PageUtil.java
java 代码
- package com.sclh.rsp.registercenter.common;
- public class PageUtil {
- /**
- * Use the origin page to create a new page
- *
- * @param page
- * @param totalRecords
- * @return
- */
- public static Page createPage(Page page, int totalRecords) {
- return createPage(page.getEveryPage(), page.getCurrentPage(),
- totalRecords);
- }
- /**
- * the basic page utils not including exception handler
- *
- * @param everyPage
- * @param currentPage
- * @param totalRecords
- * @return page
- */
- public static Page createPage(int everyPage, int currentPage,
- int totalRecords) {
- everyPage = getEveryPage(everyPage);
- currentPage = getCurrentPage(currentPage);
- int beginIndex = getBeginIndex(everyPage, currentPage);
- int totalPage = getTotalPage(everyPage, totalRecords);
- boolean hasNextPage = hasNextPage(currentPage, totalPage);
- boolean hasPrePage = hasPrePage(currentPage);
- return new Page(hasPrePage, hasNextPage, everyPage, totalPage,
- currentPage, beginIndex, totalRecords);
- }
- private static int getEveryPage(int everyPage) {
- return everyPage == 0 ? 10 : everyPage;
- }
- private static int getCurrentPage(int currentPage) {
- return currentPage == 0 ? 1 : currentPage;
- }
- private static int getBeginIndex(int everyPage, int currentPage) {
- return (currentPage - 1) * everyPage;
- }
- private static int getTotalPage(int everyPage, int totalRecords) {
- int totalPage = 0;
- if (totalRecords % everyPage == 0)
- totalPage = totalRecords / everyPage;
- else
- totalPage = totalRecords / everyPage + 1;
- return totalPage;
- }
- private static boolean hasPrePage(int currentPage) {
- return currentPage == 1 ? false : true;
- }
- private static boolean hasNextPage(int currentPage, int totalPage) {
- return currentPage == totalPage || totalPage == 0 ? false : true;
- }
- }
3. 应用
java 代码
- ......
- /**
- * @throws Exception
- * @see com.sclh.rsp.servicecenter.service.IBaseService#queryList(java.lang.String, int)
- */
- public Result queryList(String hql, int currentPage, int everyPage){
- List list = null;
- Result result = null;
- Page page = new Page();
- page.setEveryPage(everyPage);
- page.setCurrentPage(currentPage);
- int totalRecords = 0;
- try {
- totalRecords = adjunctDataDAO.getCount(hql);
- if (totalRecords != 0) {
- page = PageUtil.createPage(page, totalRecords);
- list = adjunctDataDAO.getByPage(hql, page);
- result = new Result(page, list);
- } else {
- // throw new ObjectNotFoundException("adjunctDataNotExist",
- // null);
- }
- } catch (Exception e) {
- log.error(this.getClass()
- + "中 queryList(String hql, int currentPage) 操作失败!"
- + e.getMessage());
- }
- return result;
- }
- ................