Paging problem is a very common problem, developers will almost encounter, here does not discuss the details of how to page, explain the Web-mode page paging principle. The first is that the query gets a result set (shown as the result of the query database), and if the results are more than we usually do not display all of the data, then the pagination will be used to display some data (such as 20). Because of the stateless nature of HTTP, each commit is treated as a new request, even if the page is changed, the previous result has no effect on the next time.
Here is a summary of three ways to achieve pagination, do not know what else!
1. Each fetch the results of the query, and then display the specified record according to the page number.
2. According to the page to take only one page of data, and then display the page, here to construct the SQL statement.
3. Taking a certain number of pages of data, is the first two kinds of compromise.
Also note here is whether the data is placed in the request or session, and is discussed here
1. Generally will not be placed in the session, because it will occupy a lot of memory, so to put in the request inside.
Advantages: The implementation is relatively simple, the query speed is relatively fast.
Disadvantage: Occupy more memory, network transmission data.
This method is more appropriate for queries with fewer data volumes. Here someone put the data in the session, so the time to change the page do not have to requery, but this is extremely bad, it is strongly recommended not to use this.
2. Certainly not in the session, because there is no point in the session.
Advantages: less memory footprint.
Disadvantage: More trouble, you must first get the total number of query results, because you know how many records to know how many pages. In addition, to construct a paging query statement, it is not the same for different databases.
3. This situation is definitely in the session, or why I take several pages, such an implementation is to reduce the number of database queries, such as I save the 1th to 10th record, then if the page change between 1 to 10 can be directly from session access. If I change to page 11, I can reset the cache 11 to
20 pages of data (or 5 to 15 pages of data), so that you can change it 10 times before you need a database query operation.
Advantages: The use of memory is relatively low, improve the average query speed.
Disadvantage: It is more complex to implement, there may be dirty data, you need to define a cache set. If you are querying for a larger amount of data, consider using this approach.
The following design to get only one page of data each time to reset the total number of queries, how to obtain their own implementation, this is a more common paging implementation.
Design an interface here:
Package treeroot.util;
Import java.util.List;
/**
* This interface is used to implement paging functionality, note that there is no functionality available to modify.
* @author Treerot
* @version 1.0
* @since 2004-9-30/public
interface pageable
{
/**< c11/> * Obtain Data results * @return * * * public
List getresult ();
/**
* Get the total number of inquiries * @return * * public
int getcount ();
/**
* Obtain a record number per page
* @return * * public
int getpagesize ();
/**
* Get current page number
* @return * * * public
int getcurrentpage ();
/**
* Get the total number of pages
* @return * * public
int getpages ();
/**
* Default display record number per page/public
final static int default_pagesize=20;
}
This interface is very simple, including a result list and some of the necessary information pagination, here to note a few:
1. The implementation of this interface represents a page of data for a query that has nothing to do with the last query
2. The implementation of this interface should be read-only, which means it cannot be modified.
The 3.getPages () method is redundant, but this method is still available here.
An abstract implementation is given below:
Package treeroot.util;
Import java.util.List; /** * @author Treerot * @version 1.0 * @since 2004-9-30 * * Public abstract class Abstractpage implements {pageable
ate int currentpage;
private int pageSize;
private int pages;
protected int count;
protected List result; /** * Specifies the current page * @param currentpage * @throws pageexception/public abstractpage (int currentpage) {This (curr
Entpage,pageable.default_pagesize); /** * Specifies the current page and page size * @param currentpage * @param pageSize * @throws pageexception/public abstractpage (int
Currentpage,int pageSize) {this.currentpage=currentpage;
This.pagesize=pagesize; } protected void CheckPage (int currentpage) throws pageexception{if (currentpage<1) | |
(Currentpage>this.getpages ()))
throw new Pageexception ("Page out of range: Total pages are" +this.getpages () + ", current page is" +currentpage ");
/** * This method is used to initialize the quilt class rewrite, that is, to calculate the count and result results, which are called in the constructor of the subclass. * * Abstract protected void init () throws pageexception;
Public List GetResult () {return result;
public int GetCount () {return count;
public int getpagesize () {return pageSize;
public int getcurrentpage () {return currentpage;
public int getpages () {if (pages==0) this.pages= (count+pagesize-1)/pagesize;
return pages;
}
}
This abstract class implements all the methods in the interface, but defines an abstract method init () that must be implemented in subclasses. One of the above interfaces and an abstract class looks simpler, and you may feel like nothing has been done, but it does not do anything, but it can be a great help to development. We can inherit this abstract class according to our own needs, and the data can be obtained in a variety of ways, such as directly through a list, or through jdbc,hibernate and so on, but we all need to encapsulate the results into a list. It is particularly convenient to pass the hibernate.
Pageexception is a custom exception
Package Treeroot.util
/**
* @author treeroot
* @version 1.0 *
@since 2004-9-30
/public class Pageexception extends Exception
{public
pageexception () {
super ();
Public
pageexception (String message) {
super (message);
}
}