paging function (Pagenation Class)
Package + Implementation of paging function:
Paged SQL statement + pagination Tool class Pagenation details + pagination principle
One, paged SQL statement:
SELECT * FROM (Selectt.*,row_number ()-Over (order by ROWNUM ASC) rn from goods t) where rn>=? Andrn<=?
? Goods: For the table name, the data is found in the goods
? The first one? : Starting line number
? The second one? : End Line number
Second, the Pagination tool class Pagenation details:
Pagenation class : A JavaBean-like tool class that encapsulates the details after pagination,
main role : Package background Paging details, foreground call paging details.
Summary : You need to pass in 3 parameters when instantiating, and then the class internally calculates all the paging details
Note : You will also need to call the setlist () method later to deposit the paging results.
usage : Background with this kind of package paging results, request to front desk, front desk display.
incoming Parameters : Current page (pagenum) + size per page + record Total rows (rowCount)
Three, paging principle:
1. Background Sevlet Call the paging service () method according to the parameters, return the value of the object of a Pagenation class, encapsulating the paging information currently required;
2. Service () method processing: According to the three parameters passed in, initialize the Pagenation class, and then invoke the DAO package under the paging query class, the return value is a list, the list is also stored in the Pagenation object, return to return the object;
3. The returned Pagenation object contains all the contents of the page, placing the object in the request and uploading it to the foreground;
4. In the foreground to remove the relevant information pagenation, at this time the Pagenation object contains paging information, with El and Jstl display paging information.
Four, the code implementation:
1. Paging Tool Class (core class in Core):
2. Other code:
(1), front-end display code:
Package com.test.util;
Import java.util.List;
/**
* Pagenation page Tool class: Paging for data
* Based on the incoming pagenum parameter to determine from which page to start the paging
* Determine the number of records per page according to the size parameter passed in
* Determine the total number of data bars according to the rowcount parameters passed in
* @author Alvin Xing
*/
public class Pagenation {
/* The specified parameter */
private int pagenum; Current page number
private int size; Page size: How many data are displayed per page
/* data found in DB */
Private long RowCount; Total number of data: How many data are there?
Private list List; Data content
/* Properties computed by the above properties */
private int PageCount; Total pages
private int startrow; Current page start line, first behavior line No. 0
private int first = 1; First page number
private int last; Last page number
private int prev; Previous Page No.
private int Next; Next page number
private int startnav; Navigation bar Start Page number
private int endnav; Navigation bar End Page number
private int navcount = 10; Navigation bar Length page number display, up to display Numcount+1 bar, here Show 11 page
/**
* Constructor: Initialize the basic three parameters (Pagenum, size, rowCount), other parameters are calculated from
*/
Public pagenation (int pagenum, int size, long RowCount) {
Initialize Basic parameters
This.pagenum = Pagenum;
this.size = size;
This.rowcount = RowCount;
Other parameters are calculated
This.pagecount = (int) Math.ceil (this.rowcount/(double) size);
This.last = PageCount;
This.pagenum = Math.min (Pagenum, PageCount); In general, Pagenum will be equal to the incoming pagenum, but when the incoming pagenum is greater than the total number of pages, Pagenum is equal to the maximum number of pages, the last page of pages
This.pagenum = Math.max (1, this.pagenum); In general, Pagenum will be equal to the incoming pagenum, but when the incoming pagenum is less than 1, then pagenum equals 1, that is, the number of pages on the first page
This.startrow = pagenum*size-(size-1);
This.prev = (this.pagenum-1>1)? (this.pagenum-1): 1; If < previous > is the first page, 1 is displayed, otherwise {page number 1}
This.next = (this.pagenum+1<this.pagecount)? (this.pagenum+1): This.pagecount; If the < next page > is the last page, the {last page of pages} is returned, otherwise {page +1}
Navigation processing
// This.startnav = (this.pagenum-5>1)? (this.pagenum-5): 1; Navigate to Start button {Current page-5}, if the current page is less than 5, the navigation bar Start button is {page 1th} button
This.startnav = (this.pagenum-(THIS.NAVCOUNT/2) >1)? (this.pagenum-(THIS.NAVCOUNT/2)): 1; The optimized Start button so that the current page is in the middle of the navigation bar, the Start button is not {current page 5}, but {current pages-half the total length of the navigation bar}
This.endnav = (this.startnav+navcount<this.last)? (this.startnav+navcount): This.last; Navigation bar End button is {start Page + navigation bar length}, if {Start Page + navigation bar length} exceeds total number of pages, the end button is {last page}
}
Public pagenation () {//empty constructor
}
/**
* Setter () and Getter ()
*/
public int Getpagenum () {
return pagenum;
}
public void Setpagenum (int pagenum) {
This.pagenum = Pagenum;
}
public int GetSize () {
return size;
}
public void setSize (int size) {
this.size = size;
}
Public long GetRowCount () {
return rowCount;
}
public void Setrowcount (long rowCount) {
This.rowcount = RowCount;
}
Public List getList () {
return list;
}
public void setlist (list list) {
This.list = list;
}
public int Getpagecount () {
return PageCount;
}
public void Setpagecount (int pagecount) {
This.pagecount = PageCount;
}
public int Getstartrow () {
return startrow;
}
public void Setstartrow (int startrow) {
This.startrow = StartRow;
}
public int GetFirst () {
return first;
}
public void Setfirst (int first) {
This.first = First;
}
public int GetLast () {
return last;
}
public void Setlast (int. last) {
This.last = Last;
}
public int GetPrev () {
return prev;
}
public void Setprev (int prev) {
This.prev = prev;
}
public int GetNext () {
return next;
}
public void Setnext (int next) {
This.next = Next;
}
public int Getstartnav () {
return startnav;
}
public void Setstartnav (int startnav) {
This.startnav = Startnav;
}
public int Getendnav () {
return endnav;
}
public void Setendnav (int endnav) {
This.endnav = Endnav;
}
public int Getnavcount () {
return navcount;
}
public void Setnavcount (int navcount) {
This.navcount = Navcount;
}
}
(2), Background processing servlet code: see the network disk information
(3), processing paging service method class: See the network disk information
Page-by-Package--java implementation (Oracle-based)