基于Hibernate实现的分页技术
基于hibernate实现的分页技术:
1 Query query = session.createQuery("from Entity"); 2 //设置每页显示多少个 3 query.setMaxResults(page.getPageSize()); 4 //设置起点 5 query.setFirstResult(page.getBeginIndex()); 6 7 return query.list();
上面关键代码是 setMaxResults(),和setFirstResult(),即设置每页显示值和起点。
Page.java
1 package xh.util; 2 3 public class Page { 4 // 1.每页显示数量(pageSize) 5 private int pageSize; 6 // 2.总记录数(totalCount) 7 private int totalCount; 8 // 3.总页数(totalPage) 9 private int totalPage; 10 // 4.当前页(currentPage) 11 private int currentPage; 12 // 5.起始点(beginIndex) 13 private int beginIndex; 14 // 6.是否有上一页(hasPrePage) 15 private boolean hasPrePage; 16 // 7.是否有下一页(hasNextPage) 17 private boolean hasNextPage; 18 19 20 // 构造函数,默认 21 public Page() { 22 } 23 24 // 构造方法,对所有属性进行设置 25 public Page(int pageSize, int totalCount, int totalPage, int currentPage, 26 int beginIndex, boolean hasPrePage, boolean hasNextPage) { 27 this.pageSize = pageSize; 28 this.totalCount = totalCount; 29 this.totalPage = totalPage; 30 this.currentPage = currentPage; 31 this.beginIndex = beginIndex; 32 this.hasPrePage = hasPrePage; 33 this.hasNextPage = hasNextPage; 34 } 35 36 public int getPageSize() { 37 return pageSize; 38 } 39 40 public void setPageSize(int pageSize) { 41 this.pageSize = pageSize; 42 } 43 44 public int getTotalCount() { 45 return totalCount; 46 } 47 48 public void setTotalCount(int totalCount) { 49 this.totalCount = totalCount; 50 } 51 52 public int getTotalPage() { 53 return totalPage; 54 } 55 56 public void setTotalPage(int totalPage) { 57 this.totalPage = totalPage; 58 } 59 60 public int getCurrentPage() { 61 return currentPage; 62 } 63 64 public void setCurrentPage(int currentPage) { 65 this.currentPage = currentPage; 66 } 67 68 public int getBeginIndex() { 69 return beginIndex; 70 } 71 72 public void setBeginIndex(int beginIndex) { 73 this.beginIndex = beginIndex; 74 } 75 76 public boolean isHasPrePage() { 77 return hasPrePage; 78 } 79 80 public void setHasPrePage(boolean hasPrePage) { 81 this.hasPrePage = hasPrePage; 82 } 83 84 public boolean isHasNextPage() { 85 return hasNextPage; 86 } 87 88 public void setHasNextPage(boolean hasNextPage) { 89 this.hasNextPage = hasNextPage; 90 } 91 92 }
PageUtil.java
1 package xh.util; 2 3 4 public class PageUtil { 5 public static Page createPage(int pageSize, int totalCount, int currentPage) { 6 pageSize = getpageSize(pageSize); 7 currentPage = getCurrentPage(currentPage); 8 int totalPage = getTotalPage(pageSize, totalCount); 9 int beginIndex = getBeginIndex(pageSize, currentPage); 10 boolean hasPrePage = getHasPrePage(currentPage); 11 boolean hasNextPage = getHasNextPage(totalPage, currentPage); 12 13 return new Page(pageSize, totalCount, totalPage, currentPage, 14 beginIndex, hasPrePage, hasNextPage); 15 } 16 17 public static Page createPage(Page page, int totalCount) { 18 int pageSize = getpageSize(page.getPageSize()); 19 int currentPage = getCurrentPage(page.getCurrentPage()); 20 int totalPage = getTotalPage(pageSize, totalCount); 21 int beginIndex = getBeginIndex(pageSize, currentPage); 22 boolean hasPrePage = getHasPrePage(currentPage); 23 boolean hasNextPage = getHasNextPage(totalPage, currentPage); 24 25 return new Page(pageSize, totalCount, totalPage, currentPage, 26 beginIndex, hasPrePage, hasNextPage); 27 } 28 29 // 设置每页显示记录数 30 public static int getpageSize(int pageSize) { 31 return pageSize == 0 ? 10 : pageSize; 32 } 33 34 // 设置当前页 35 public static int getCurrentPage(int currentPage) { 36 return currentPage == 0 ? 1 : currentPage; 37 } 38 39 // 设置总页数,需要总记录数,每页显示多少 40 public static int getTotalPage(int pageSize, int totalCount) { 41 int totalPage = 0; 42 if (totalCount % pageSize == 0) { 43 totalPage = totalCount / pageSize; 44 } else { 45 totalPage = totalCount / pageSize + 1; 46 } 47 return totalPage; 48 } 49 50 // 设置起始点,需要每页显示多少,当前页 51 public static int getBeginIndex(int pageSize, int currentPage) { 52 return (currentPage - 1) * pageSize; 53 } 54 55 // 设置是否有上一页,需要当前页 56 public static boolean getHasPrePage(int currentPage) { 57 return currentPage == 1 ? false : true; 58 } 59 60 // 设置是否有下一个,需要总页数和当前页 61 public static boolean getHasNextPage(int totalPage, int currentPage) { 62 return currentPage == totalPage || totalPage == 0 ? false : true; 63 } 64 }