When there are too many data bars in the database, a page cannot be displayed, this is to set up a paging query, the first thing to do is to use the limit condition of the database SQL statement to implement the group query
The approximate form of the SQL statement is:
SELECT * FROM table limit start index, display number of bars
Block queries are implemented with this statement, and each page displays a fixed number of bars.
First to implement the background paging, we need to know how many pages it has, how many rows per page, this need to know how many rows, call the SQL statement also need to know the start index of each page, the start index is based on the current number of pages, so also need to know the current page, the query will return a list to store the current data. Encapsulating these attributes and getting the set method into a class will have the following page class:
1 Public classPage<t> {2 PrivateList<t> data;//Data List3 Private intPagenum;//Current Page4 Private intPageSize//The current page shows the number of bars5 Private intRows//total number of rows6 PublicPage (intRowsintPagenum,intpagesize) {7 Super();8Data=NewArraylist<>();9 This. rows=rows;Ten This. SetPageSize (pagesize); One This. Setpagenum (Pagenum); A } - PublicPage () { - Super(); the } - Public intGetpagenum () { - returnPagenum; - } + Public voidSetpagenum (intpagenum) { - if(pagenum>gettotalpage ()) + { A This. pagenum=gettotalpage (); at } - Else { - This. Pagenum =Pagenum; - } - if(pagenum<1) - { in This. pagenum=1; - } to } + Public intgetpagesize () { - returnpagesize; the } * Public voidSetPageSize (intpagesize) { $ This. pagesize =pagesize;Panax Notoginseng } - Public intGettotalpage () { the //Calculate Total Pages + if(rows%pagesize==0) A { the returnrows/pagesize; + } - Else { $ returnRows/pagesize+1; $ } - } - Public intgetRows () { the returnrows; - }Wuyi Public voidSetrows (introws) { the This. rows =rows; - } Wu Public intGetindexnum () { - //Get index value About returnpagesize* (pagenum-1); $ } - PublicList<t>GetData () { - returndata; - } A Public voidSetData (list<t>data) { + This. data =data; the } -}Page Class
When initializing, we only need to get the total number of data, the number of pages and each page of the bar. The total number of data bars can be obtained from table with the SQL statement select COUNT (*). You only need to pass in the current number of pages each time you query. Pass the Page object after each query to the JSP foreground page and display it as a div
1 <Div>2 <ahref= "/stuent8.11/stuservlet?pagenum=1">Home</a>3 <ahref= "/stuent8.11/stuservlet?pagenum=${apage.pagenum-1}">Previous page</a>4 <C:choose>5 <C:whenTest= "${apage.totalpage<=5}">6 <C:setvar= "Begin"value= "1"></C:set>7 <C:setvar= "End"value= "${apage.totalpage}"></C:set>8 </C:when>9 <C:whenTest= "${apage.totalpage>5}">Ten <C:setvar= "Begin"value= "1"></C:set> One <C:setvar= "End"value= "5"></C:set> A <c:ifTest= "${apage.pagenum>3}"> - <C:setvar= "Begin"value= "${apage.pagenum-2}"></C:set> - <C:setvar= "End"value= "${apage.pagenum+2}"></C:set> the </c:if> - <c:ifTest= "${end>apage.totalpage}"> - <C:setvar= "Begin"value= "${apage.totalpage-4}"></C:set> - <C:setvar= "End"value= "${apage.totalpage}"></C:set> + </c:if> - </C:when> + </C:choose> A <C:foreachbegin= "${begin}"End= "${end}"Step= "1"var= "num"> at <c:ifTest= "${apage.pagenum==num}"> - [${num}] - </c:if> - <c:ifTest= "${apage.pagenum!=num}"> - <ahref= "/stuent8.11/stuservlet?pagenum=${num}">${num}</a> - </c:if> in </C:foreach> - <ahref= "/stuent8.11/stuservlet?pagenum=${apage.pagenum+1}">Next page</a> to <ahref= "/stuent8.11/stuservlet?pagenum=${apage.totalpage}">Last</a> + </Div>Show Div
One of the Choose is used to specify the maximum number of pages to display, here is 5 pages, that is, when the point to page 4th is, the 1th page will disappear, the 6th page appears.
Java paging Query--pagination display