Note: I have seen a variety of paging online, many of which claim to be common, but often they are not satisfactory: some add URL address information to the paging class, and some also perform paging actions in the paging class (this action is entirely a matter of operating the database). Now, after my summary and refinement:
Whether you paging manually yourself or using the help of a framework. This tool class can help you achieve stable paging effects (including navigation page numbering function), and the usage method is relatively simple: OK, less nonsense, the code is as follows:
package test.dao; import java.util.List; /** * Tool class for pagination* @author Don't take the net name*/ public class Pager<T> { private List<T> list; // Object record result set private int total = 0; // Total records private int limit = 20; // Number of records displayed per page private int pages = 1; // Total pages private int pageNumber = 1; // Current page private boolean isFirstPage=false; // Whether it is the first page private boolean isLastPage=false; // Whether it is the last page private boolean hasPreviousPage=false; //Is there a previous page private boolean hasNextPage=false; //Is there a next page private int navigationPages=8; //Number of navigation page numbers private int[] navigationPageNumbers; //All navigation page numbers public Pager(int total, int pageNumber, limit); } public Pager(int total, int pageNumber, int limit) { init(total, pageNumber, limit); } private void init(int total, int pageNumber, int limit) { init(total, pageNumber, limit); } private void init(int total, int pageNumber, int limit) { //Set the basic parameters this.total=total; this.limit=limit; this.pages=(this.total-1)/this.limit+1; //Originally correct according to the current number that may be incorrect if(pageNumber<1){ this.pageNumber=1; }else if(pageNumber>this.pages){ this.pageNumber=this.pages; }else{ this.pageNumber=pageNumber; } //After the basic parameters setting, calculate the navigation page calcNavigatePageNumbers(); //As well as the determination of the page boundary judgePageBoudary(); } /** * Calculate the navigation page*/ private void calcNavigatePageNumbers(){ //When the total number of pages is less than or equal to the number of navigation pages if(pages<=navigatePages){ navigatePageNumbers=new int[pages]; for(int i=0;i<pages;i++){ navigatePageNumbers[i]=i+1; } }else{ //When the total number of pages is greater than the number of navigation pages navigatePageNumbers=new int[navigatePages]; int startNum=pageNumber-navigatePages/2; int endNum=pageNumber+navigatePages/2; if(startNum<1){ startNum=1; //(The first navigatePages page for(int i=0;i<navigatePages;i++){ navigatePageNumbers[i]=startNum++; } }else if(endNum>pages){ endNum=pages; //The last navigatePages page for(int i=navigatePages-1;i>=0;i--){ navigatePageNumbers[i]=endNum--; } }else{ //All intermediate pages for(int i=0;i<navigatePages;i++){ navigatePageNumbers[i]=startNum++; } } } } } /** * Determine the page boundary*/ private void judgePageBoudary(){ isFirstPage = pageNumber == 1; isLastPage = pageNumber == pages && pageNumber!=1; hasPreviousPage = pageNumber > 1; hasNextPage = pageNumber < pages; } public void setList(List<T> list) { this.list = list; } /** * Get the content of the current page* @return {List} */ public List<T> getList() { return list; } /** * Get the total number of records* @return {int} */ public int getTotal() { return total; } /** * Get how many records are displayed per page* @return {int} */ public int getLimit() { return limit; } /** * Get the total number of pages* @return {int} */ public int getPages() { return pages; } /** * Get the current page number* @return {int} */ public int getPageNumber() { return pageNumber; } /** * Get all navigation page numbers* @return {int[]} */ public int[] getNavigatePageNumbers() { return navigatePageNumbers; } public boolean isFirstPage() { return isFirstPage; } public boolean isLastPage() { return isLastPage; } public boolean hasPreviousPage() { return hasPreviousPage; } public boolean hasNextPage() { return hasNextPage; } public String toString(){ StringBuffer sb=new StringBuffer(); sb.append("[") .append("total=").append(total) .append(",pages=").append(pages) .append(",pageNumber=").append(pageNumber) .append(",limit=").append(limit) .append(",isFirstPage=").append(isFirstPage) .append(",isLastPage=").append(isLastPage) .append(",hasPreviousPage=").append(hasPreviousPage) .append(",hasNextPage=").append(hasNextPage) .append(",navigatePageNumbers="); int len=navigatePageNumbers.length; if(len>0)sb.append(navigatePageNumbers[0]); for(int i=1;i<len;i++){ sb.append(" "+navigatePageNumbers[i]); } sb.append(",list.size="+list.size()); sb.append("]"); return sb.toString(); } } About usage: The steps are as follows:
1). Regardless of whether the conditional query is used, first count the corresponding total number of entries.
2). Construct a Pager class (the limit parameters can be made according to their own foreground)
3). Obtain the pageNumber parameter that has been automatically corrected according to the constructed Pager class, multiply -1 and limit to serve as the location of the first record to be queried.
4). Set all records to be checked from the starting position until the limit. (If you are manually paging, it is also possible that the second parameter is the position offset of the end record, depending on the database)
5). Set the list queryed by [condition] into the pager object and return.
6). In the presentation layer, you can use this page object through Servlet or Action or directly get the pager object. The specific matter of splicing the URL should also be left to the presentation layer to complete.
int totalCount=Integer.valueOf(queryCount.uniqueResult().toString()); Pager pager=new Pager(totalCount, pageNumber,limit); queryList.setFirstResult((pager.getPageNumber()-1)*limit); //Fault-tolerant processing queryList.setMaxResults(limit); pager.setList(queryList.list()); return pager;
The above code is a simple example using hibernate. If you are using manual pages, follow the steps to use.
Of course, the pagination can be completed in the dao layer or in the service layer. The Servlet or Action calls the service layer code. Personally, I think: paging is actually a business need, so it can be placed in the business level. Because the DAO layer provides the corresponding [condition query] list and [condition] statistics count, the business layer can make method calls according to its own needs (it only needs to be followed by established steps. Of course, many times, the business has established process steps).
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.