Paging encapsulation-java Implementation (based on oracle), javaoracle

Source: Internet
Author: User

Paging encapsulation-java Implementation (based on oracle), javaoracle

Paging Function(Pagenation class)

 

Encapsulation + Implementation of the paging function:

Paging SQL statement+Pagenation details+Paging Principle

 

I,Paging SQL statement:

Select * from (selectt. *, row_number () over (order by rownum asc) rn from goods t) where rn >=? Andrn <=?

Ø goods: The table name. The data is retrieved from goods.

Ø first? : Starting line number

Ø second? : End line number

 

II,Pagenation details:

Pagenation class: Similar to the JavaBean tool class, it encapsulates the details after pagination,

Main functions: Encapsulate the page details in the background, and call the page details in the foreground.

Introduction: Three parameters need to be input during instantiation, and all page details are calculated within the class.

Note:: You also need to call the setList () method to save the paging results.

Usage: This type is used in the background to encapsulate the paging results. The request is sent to the front-end and displayed on the front-end.

Input parameters: Current page (pageNum) + page size (size) + total number of records (rowCount)

 

III,Paging principle:

1. The background sevlet calls the paging service () method based on parameters, returns a Pagenation class object, and encapsulates the page information currently required;

2. service () method processing process: Initialize the Pagenation Class Based on the input three parameters, and then call the paging Query Class under the dao package. The returned value is a List, which is also saved to the Pagenation object, returns the object;

3. The returned Pagenation object contains all the content related to the page. Put this object in the request and transfer it to the foreground to jump to the foreground;

4. Retrieve Information about Pagenation from the console. In this case, Pagenation objects store paging information and display paging information using EL and JSTL.

 

IV,Code implementation:

1. Paging tool class (core class ):

 

2. Other code:

(1) Front-end display code:

Package com. test. util;

Import java. util. List;

/**
* Pagenation paging tool: used for data Paging
* Determine the page from which paging starts based on the input pageNum parameter.
* Determine the number of records per page based on the input size parameter
* Determines the total number of data entries based on the input rowCount parameter.
* @ Author Alvin Xing
*/
Public class Pagenation {

/* Specified parameter */
Private int pageNum; // the current page number.
Private int size; // page size: how much data is displayed on each page
/* Data searched in DB */
Private long rowCount; // total number of data records: Total number of data records
Private List list; // data content
/* Attributes calculated from the preceding attributes */
Private int pageCount; // the total number of pages.
Private int startRow; // The row starting the current page. The first row is 0th rows.
Private int first = 1; // The first page number
Private int last; // The last page number.
Private int prev; // The number of the previous page
Private int next; // The number of the next page
Private int startNav; // The start page of the navigation bar.
Private int endNav; // The end page number of the navigation bar
Private int navCount = 10; // number of page numbers displayed in the navigation bar. A maximum of numCount + 1 entries are displayed. Page 11 is displayed here.


/**
* Constructor: Initialize the basic three parameters (pageNum, size, rowCount). Other parameters are calculated.
*/
Public Pagenation (int pageNum, int size, long rowCount ){
// Initialize Basic parameters
This. pageNum = pageNum;
This. size = size;
This. rowCount = rowCount;

// Calculate other parameters
This. pageCount = (int) Math. ceil (this. rowCount/(double) size );
This. last = pageCount;

This. pageNum = Math. min (pageNum, pageCount); // generally, pageNum is equal to the incoming pageNum. However, if the incoming pageNum is greater than the total number of pages, pageNum is equal to the maximum number of pages, the number of pages on the last page.
This. pageNum = Math. max (1, this. pageNum); // generally, pageNum is equal to the incoming pageNum, but if the incoming pageNum is smaller than 1, pageNum is equal to 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 Page> is the first page, 1 is displayed; otherwise it is {page-1}
This. next = (this. pageNum + 1 <this. pageCount )? (This. pageNum + 1): this. pageCount; // If <next page> is the last page, {number of last pages} is returned; otherwise, {Number of pages + 1} is returned}

// Navigation
// This. startNav = (this. pageNum-5> 1 )? (This. pageNum-5): 1; // navigation to start button is {current page number-5}, if the current page number is less than 5, the navigation bar start button is {1st page} button
This. startNav = (this. pageNum-(this. navCount/2)> 1 )? (This. pageNum-(this. navCount/2): 1; // After optimization, the current page is located in the middle of the navigation bar. the start button is no longer {current page-5 }, but {current page number-half of the total length of the navigation bar}
This. endNav = (this. startNav + navCount <this. last )? (This. startNav + navCount): this. last; // The end button of the navigation bar is {start page number + navigation bar length}. If {start page number + navigation bar length} exceeds the total page number, 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) servlet code processed in the background: see Network Disk information.

(3) service method for processing paging: see Network Disk Data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.