Front-end code. html
varPageSize = 10;//set the number of bars to display per page varTotal//total number of data bars functionpagination () {$ ("#Pagination"). Pagination (total, {callback:pagecallback, Prev_text:' Previous page ', Next_text:' Next page ', Items_per_page:pagesize, num_display_entries:4,//number of page entries displayed in the main part of the continuous pagingNum_edge_entries:1//Number of entries for the end-to-end page displayed on both sides }) } functionPagecallback (index, JQ) {//The previous index value that represents the page number of pages that you are currently clicking, and the next parameter represents the loading container. LoadData (index); } functionLoadData (pageIndex) {$.ajax ({type:"Get", URL:"Servlet/bookservlet?p=0&pagesize=" +pageSize+ "&pageindex=" +PageIndex, Async:false, DataType:"JSON", Success:function(objdata) {varLists =Objdata.data; Total=Objdata.recordstotal; $("#tbodyList"). empty (); $("#listsTemplate"). Tmpl (lists). AppendTo ("#tbodyList"); } }) }$(function() {LoadData (0);//Paging InitializationPagination ();//Pagination Display})
<id= "Pagination" class= "pagination"> <!--- </div>
Pagehelper Help Class
PackageCom.rongjie.common;Importjava.util.ArrayList;Importjava.util.List;/*** Pagination Help class *@authorCao * *@param<T> JavaBean class*/ Public classPagehelper<t> { PrivateList<t>list;//return Data collectionPrivate inttotal;//query Data Total PublicList<t>getList () {returnlist; } Public voidSetlist (list<t>list) { This. List =list; } Public intgettotal () {returnTotal ; } Public voidSettotal (intTotal ) { This. Total =Total ; } /*** Paging Algorithm *@paramList Total data *@parampage Current Page *@paramrows per page capacity *@returnList * Note: The secondary method is used in cases where the data volume is relatively small, whereas the reverse is slow*/ PublicList<t> Paginationbook (list<t> List,intPageintrows) {List<T> small =NewArraylist<t>(); intBeginindex = rows *page; intEndIndex; if(Rows * (page + 1) >list.size ()) {EndIndex=list.size (); } Else{EndIndex= rows * (page + 1); } for(inti = Beginindex; i < EndIndex; i++) {Small.add (List.get (i)); } returnSmall; }}
The servlet code is part of the non-generic code. Self-adjusting according to the project
protected voiddoget (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {response.setcharacterencoding ("UTF-8"); String PageIndex= Request.getparameter ("PageIndex");//Current page number intpage = (Regexpvalidatorutils.isnumber (pageIndex) = =true) ? Integer.parseint (PageIndex): 0; String pageSize= Request.getparameter ("PageSize");//How many bars are displayed per page introws = (Regexpvalidatorutils.isnumber (pageSize) = =true) ? Integer.parseint (pageSize): 0; Bookviewinfo Bookviewinfo=NULL;//This is a view bean that can be modified according to your own needs Pagehelper<BookInfo> plist =NULL;//Pagination helper class plist=BLL. Index (page, rows);//Call the business layer method List<BookViewInfo> bookviewinfolist =NewArraylist<bookviewinfo>(); //Load View Model for(BookInfo bookInfo:plist.getList ()) {//This part is the loading object and is modified according to the different items Bookviewinfo=NewBookviewinfo (); BOOKVIEWINFO.SETBOOK_ID (bookinfo.getbook_id ());String statustemp =bookinfo.getbook_status ();Bookviewinfo.setbook_status (statustemp); Bookviewinfo.setbook_title (Bookinfo.getbook_title ()); Bookviewinfo.setbook_category (Bookinfo.getbook_category ()); Bookviewinfo.setbook_saleprice (Bookinfo.getbook_saleprice ()); Bookviewinfo.setbook_createtime (Sdf.format (Bookinfo.getbook_createtime ())); Bookviewinfo.setbook_creator (Bookinfo.getbook_creator ()); Bookviewinfo.setcategory_name (Bookinfo.getcategoryinfo (). Getcategory_name ()); Bookviewinfolist.add (Bookviewinfo); } collections.reverse (bookviewinfolist);//ReverseString jsonstr = "{\" recordstotal\ ":" + plist.gettotal () + ", \" recordsfiltered\ ":" +bookviewinfolist.size ()+ ", \" data\ ":" + jsonarray.fromobject (bookviewinfolist) + "}"The Net.sf.json.JSONArray is used here to do JSON serialization Response.getwriter (). write (JSONSTR); }
Business Layer Code: This part of the non-generic code. Self-adjusting according to the project
PrivateBookdao DAO =NewBookdao (); /*** Paging Data * *@paramPage * Current page *@paramrows * Shows how many data per page *@return */ PublicPagehelper<bookinfo> Index (intPageintrows) {Pagehelper<BookInfo> Pagehelper =NewPagehelper<bookinfo>(); List<BookInfo> alllist =DAO. Index (); Pagehelper.settotal (Alllist.size ());//Total Datalist<bookinfo> list =Pagehelper.paginationbook (alllist, page, rows); Pagehelper.setlist (list);//data per load in rowsCollections.reverse (list);//Reverse returnPagehelper; }
DAO layer Code This part of the non-generic code. Self-adjusting according to the project
/*** Check All books * *@returnList*/ PublicList<bookinfo>Index () {String SQL= "SELECT * FROM table"; Try{jdbcutils=Newjdbcutils (); Connection= Jdbcutils.getconnection ();//connecting to a databaseList<BookInfo> list =findAll (SQL); returnlist; } Catch(Exception e) {Throw Newdaoexception (e); } finally{jdbcutils.releaseconn (connection,NULL); } }/*** Paging Query/Query All information * *@paramSQL * Query statement *@paramparams *@return * @throwsSQLException *@throwsparseexception*/ Privatelist<bookinfo> findAll (String sql, Object ... params)throwsSQLException, parseexception {List<BookInfo> list =NewArraylist<bookinfo>(); Pstmt=connection.preparestatement (SQL); SetParam (pstmt, params); ResultSet=Pstmt.executequery (); //Timestamp book_createtime = new//Timestamp (System.currenttimemillis ()); //book_createtime = timestamp.valueof (BOOK_CREATETIMESTR); while(Resultset.next ()) {BookInfo BookInfo=NewBookInfo (); CategoryInfo CategoryInfo=NewCategoryInfo (); Bookinfo.setid (Resultset.getint ("id")); BOOKINFO.SETBOOK_ID (Resultset.getint ("BOOK_ID")); Bookinfo.setbook_category (Resultset.getint ("Book_category")); Bookinfo.setbook_publisher (Resultset.getint ("Book_publisher")); Bookinfo.setbook_publishtime (Resultset.getdate ("Book_publishtime")); Bookinfo.setbook_title (Resultset.getstring ("Book_title")); //constructs a variable of type BigDecimal with a value of string content Book_priceBigDecimal Book_price =NewBigDecimal (resultset.getstring ("Book_price")); BigDecimal Book_saleprice=NewBigDecimal (resultset.getstring ("Book_saleprice")); //Set the number of decimal digits, the first variable is the number of decimal digits, the second is the trade-off method (rounding)Book_price = Book_price.setscale (2, BIGDECIMAL.ROUND_HALF_UP); Book_saleprice= Book_saleprice.setscale (2, BIGDECIMAL.ROUND_HALF_UP); Bookinfo.setbook_price (Book_price); Bookinfo.setbook_saleprice (Book_saleprice); BOOKINFO.SETBOOK_ISBN (Resultset.getstring ("BOOK_ISBN")); Bookinfo.setbook_supplier (Resultset.getstring ("Book_supplier")); Bookinfo.setbook_keyword (Resultset.getstring ("Book_keyword")); Bookinfo.setbook_summary (Resultset.getstring ("Book_summary")); Bookinfo.setbook_cover (Resultset.getstring ("Book_cover")); Bookinfo.setbook_preview (Resultset.getstring ("Book_preview")); Bookinfo.setbook_download (Resultset.getstring ("Book_download")); Bookinfo.setbook_creator (Resultset.getint ("Book_creator")); Bookinfo.setbook_createtime (Resultset.gettimestamp ("Book_createtime")); Bookinfo.setbook_status (Resultset.getstring ("Book_status")); Categoryinfo.setcategory_name (Resultset.getstring ("Category_name")); Bookinfo.setcategoryinfo (CategoryInfo); List.add (BookInfo); } jdbcutils.releaseconn (connection, ResultSet); returnlist; }
Ajax paging plugin pagination from previous jquery to back end Java summary