Java -- paging, java Paging

Source: Internet
Author: User

Java -- paging, java Paging
1.Why use paging?

A) when the data volume is large, one-time query of all data may cause some problems.

B) database pressure will be high

C) data display will be slow, resulting in low user experience

D) In order to display data more conveniently and reduce the pressure on the database, the paging technology is used.

2. How to Implement paging Query

A) implementation of paging query in MySql

-- Limit is used for paging query, followed by two numbers

-- The first number indicates the index at the start of the query (starting from 0)

-- The second number indicates the number of data queried each time.

Select * from emp limit 10, 10;

 

-- The current page number (size) must be known)

-- Select * from emp limit (page-1) * size, size;

3. Code implementation by PAGE

A) defines the paging object class, which is used to encapsulate paging-related attributes for subsequent paging implementation. The Code is as follows (the Getter and Setter methods are omitted)

PackageCom. bjsxt. pojo;

 

ImportJava. util. List;

 

Public ClassPagination <T> {

 

// Current page number

Private IntPage;

// How many data entries are displayed on each page

Private IntSize;

// Total number of records

Private IntTotalCount;

// Total number of pages

Private IntTotalPage;

// Start position of each query

Private IntStart;

// Whether the previous page exists

Private BooleanHasPrev;

// Previous Page

Private IntPrevPage;

// Whether the next page exists

Private BooleanHasNext;

// Next page

Private IntNextPage;

// Homepage

Private IntFirstPage;

// Last page

Private IntLastPage;

// Number of paging navigation Blocks

Private IntNavCount;

// Start position of the navigation

Private IntNavBegin;

// End position of the navigation

Private IntNavEnd;

// List set corresponding to the query result

PrivateList <T> list;

 

PublicPagination (String page, String size,IntTotalCount ){

// Current page number

This. Page =Null! = Page? Integer.ParseInt(Page): 1;

// How many data entries are displayed on each page

This. Size =Null! = Size? Integer.ParseInt(Size): 10;

// Total number of records

This. TotalCount = totalCount;

// Total number of pages

This. TotalPage = (Int) Math.Ceil(This. TotalCount * 1.0/This. Size );

// The current page number cannot exceed the total page number

If(This. Page>This. TotalPage &&This. TotalPage> 0 ){

This. Page =This. TotalPage;

}

// Start position of each query

This. Start = (This. Page-1 )*This. Size;

If(This. Page> 1 ){

// Whether the previous page exists

This. HasPrev =True;

// Previous Page

This. PrevPage =This. Page-1;

}

If(This. Page <This. TotalPage ){

// Whether the next page exists

This. HasNext =True;

// Next page

This. NextPage =This. Page + 1;

}

// Homepage

This. FirstPage = 1;

// Last page

This. LastPage =This. TotalPage;

// Number of paging navigation Blocks

This. NavCount = 10;

// Start position of the navigation

// If (this. page> this. totalPage-this. navCount ){

// This. navBegin = this. totalPage-this. navCount + 1;

//} Else {

This. NavBegin =This. Page-This. NavCount/2 <1? 1:This. Page-This. NavCount/2;

//}

// End position of the navigation

This. NavEnd =This. NavBegin +This. NavCount-1>This. TotalPage?This. TotalPage

:This. NavBegin +This. NavCount-1;

}

// The getter and setter methods are omitted.

}

 

B) In the object class, a constructor is provided, which has three parameters: the current page, the number of lines displayed on each page, size, and total number of records totalCount. These three parameters are provided, the reason is that all the data related to the page can be determined as long as you know the three data items.

C) Use of the paging object class. The Servlet receives relevant parameters and encapsulates them as object class objects. Then, the service is called and dao is called. The Code is as follows:

Public VoidQueryAll (HttpServletRequest req, HttpServletResponse resp)ThrowsServletException, IOException {

String page = req. getParameter ("page ");

String size = req. getParameter ("size ");

IntTotalCount = es. queryCount ();

// Create a paging object

Pagination <Emp> p =NewPagination <Emp> (page, size, totalCount );

// Call the method in the service. After the query is completed, the query result is automatically set to the p object.

Es. queryAll (p );

// Store the p object to the scope for use in jsp

Req. setAttribute ("p", p );

// Request forwarding

Req. getRequestDispatcher ("/emplist. jsp"). forward (req, resp );

}

@ Override

Public VoidQueryAll (Pagination <Emp> p ){

// Call dao

Dao. queryAll (p );

}

@ Override

Public VoidQueryAll (Pagination <Emp> p ){

P. setList (query (Emp.Class, "Select * from emp limit ?,? ", P. getStart (), p. getSize ()));

}

D) implementation code of pagination in jsp

<Font color ="Blue">$ {P. totalCount} </font> record, current <font color ="Blue">$ {P. page} </font> page, total <font color ="Blue">$ {P. totalPage} </font> page

<Table>

<Tr>

<Td> NO. </td>

<Td> name </td>

<Td> salary </td>

<Td> date of employment </td>

<Td> operation </td>

</Tr>

<C: forEach items ="$ {P. list}"Var ="E">

<Tr>

<Td >$ {e. empno} </td>

<Td >$ {e. ename} </td>

<Td >$ {e. sal} </td>

<Td >$ {e. hiredate} </td>

<Td> <a href ="Query. sxt? Op = queryByEmpno & empno =$ {E. empno}& Page =$ {P. page}& Amp; size =$ {P. size}"> Modify </a>/<a href ="Query. sxt? Op = deleteEmp & empno =$ {E. empno}& Page =$ {P. page}& Amp; size =$ {P. size}"Onclick = "return confirm ('Are you sure you want to delete it? '); "> Delete </a> </td>

</Tr>

</C: forEach>

</Table>

<A href ="Query. sxt? Op = queryAll & page =$ {P. firstPage}& Amp; size =$ {P. size}"> Homepage </a> |

<A href ="Query. sxt? Op = queryAll & page =$ {P. prevPage}& Amp; size =$ {P. size}"Onclick ="Return$ {P. hasPrev?True:False}"> Previous page </a> |

<C: forEach begin ="$ {P. navBegin}"End ="$ {P. navEnd}"Var ="Each">

<C: choose>

<C: when test ="$ {P. page = each}">

<A href ="Query. sxt? Op = queryAll & page =$ {Each}& Amp; size =$ {P. size}"> <Font color ="Red"Size ="5">$ {Each} </font> </a>

</C: when>

<C: otherwise>

<A href ="Query. sxt? Op = queryAll & page =$ {Each}& Amp; size =$ {P. size}">$ {Each} </a>

</C: otherwise>

</C: choose>

</C: forEach> |

<A href ="Query. sxt? Op = queryAll & page =$ {P. nextPage}& Amp; size =$ {P. size}"Onclick ="Return$ {P. hasNext?True:False}"> Next page </a> |

<A href ="Query. sxt? Op = queryAll & page =$ {P. lastPage}& Amp; size =$ {P. size}"> Last page </a>

4. Deleting,   Paging Problems

A) after the deletion is successful, how can I continue to stay on the current page? The current page number and the number of records per page can be carried during the deletion? The Code is as follows:

<A href ="Query. sxt? Op = deleteEmp & empno =$ {E. empno}& Page =$ {P. page}& Amp; size =$ {P. size}"Onclick = "return confirm ('Are you sure you want to delete it? '); "> Delete </a>

B) after deleting the last data record of the last page, make sure to return to the previous page. In fact, the current page number cannot exceed the total page number and the total page number must be greater than 0.

// The current page number cannot exceed the total page number

If(This. Page>This. TotalPage &&This. TotalPage> 0 ){

This. Page =This. TotalPage;

}

5. Paging during Modification

A) Ensure that the modification is left on the current page. Because you want to jump to two pages, you need to pass the value twice.

<A href ="Query. sxt? Op = queryByEmpno & empno =$ {E. empno}& Page =$ {P. page}& Amp; size =$ {P. size}"> Modify </a>

<Input type ="Hidden"Name ="Page"Value ="$ {Param. page}"/>

<Input type ="Hidden"Name ="Size"Value ="$ {Param. size}"/>

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.