Springmvc+hibernate has become a framework integration that many people use now. Recently, I have been learning and exploring. Since many projects in development projects use list paging function, here we refer to some information on the Internet, and use springmvc4+hibnerate4 to summarize while learning and summarizing it, and to obtain the paging function code. Although it is not necessarily universal, it is of reference value for beginners.
The basic process of pagination implementation:
1. Pagination tool category
Ideas:
1. Write a Page class and define attributes, which should include: query result set, total number of query records, number of records displayed per page, and which page is currently available.
2. Write the Page class and define the method, which should include: total number of pages, starting recording of the current page, home page, next page, previous page, last page and other methods
The code is as follows:
package cn.myic.model;import java.util.List;public class Page<E> { // Result Set private List<E> list; // Total number of query records private int totalRecords; // How many records per page private int pageSize; // Which page private int pageNo; /** * @return Total number of pages* */ public int getTotalPages(){ return (totalRecords+pageSize-1)/pageSize; } /** * Calculate the current page start record* @param pageSize Number of records per page* @param currentPage Which page is currently* @return Current page start record number*/ public int countOffset(int currentPage,int pageSize){ int offset = pageSize*(currentPage-1); return offset; } /** * @return Home page* */ public int getTopPageNo(){ return 1; } /** * @return Previous page* */ public int getPreviousPageNo(){ if(pageNo<=1){ return 1; } return pageNo-1; } /** * @return Next page* */ public int getNextPageNo(){ if(pageNo>=getBottomPageNo()){ return getBottomPageNo(); } return pageNo+1; } /** * @return Last page* */ public int getBottomPageNo(){ return getTotalPages(); } public List<E> getList() { return list; } public void setList(List<E> list) { this.list = list; } public int getTotalRecords() { return totalRecords; } public void setTotalRecords(int totalRecords) { this.totalRecords = totalRecords; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getPageNo() { return pageNo; } public void setPageNo(int pageNo) { this.pageNo = pageNo; }} 2. Layer method
Idea: Define a pagination query method, set parameters: when the page number and how many records are displayed on each page
The code is as follows:
/** * Pagination query* @param hql query conditions* @param offset Start recording* @param length Query several records at a time* @return Return the query record collection*/ @SuppressWarnings("unchecked") @Override public List<Course> queryForPage(int offset, int length) { // TODO Auto-generated method stub List<Course> entitylist=null; try{ Query query = getSession().createQuery("from Course"); query.setFirstResult(offset); query.setMaxResults(length); entitylist = query.list(); }catch(RuntimeException re){ throw re; } return entitylist; } 3. Service layer method
Ideas:
1. Define a pagination query method and set parameters: when the page number and how many records are displayed on each page, the pagination object (Page) that returns the query result
2. Through the Dao layer, get the total number of records of the query entity
3. Get the number of records starting on the current page
4. Through the Dao layer, get the pagination query result set
5. Set into page object
The code is as follows:
/** * Pagination query* @param currentPage Current page number: the number of pages displayed now* @param pageSize Number of records displayed per page* @return Bean that encloses the pagination information (including the record set list) * */ @SuppressWarnings("unchecked") @Override public Page queryForPage(int currentPage,int pageSize) { // TODO Auto-generated method stub Page page = new Page(); // Total number of records int allRow = courseDao.getAllRowCount(); // The current page starts recording int offset = page.countOffset(currentPage,pageSize); //Pagination query result set List<Course> list = courseDao.queryForPage(offset, pageSize); page.setPageNo(currentPage); page.setPageSize(pageSize); page.setTotalRecords(allRow); page.setList(list); return page; } 4. Controller layer method
The design of the Controller layer, when operating page turn query, you only need to pass the current page number parameter.
The code is as follows:
@RequestMapping(value = "/showAll.do") public String findAllCourse(HttpServletRequest request, HttpServletResponse response) { try { String pageNo = request.getParameter("pageNo"); if (pageNo == null) { pageNo = "1"; } Page page = courseService.queryForPage(Integer.valueOf(pageNo), 10); request.setAttribute("page", page); List<Course> course = page.getList(); request.setAttribute("courses", course); } catch (Exception e) { e.printStackTrace(); } return "course/course_list"; } 5. View layer jsp display
Several buttons for paging jsp pages are displayed according to the judgment of the current page number.
The code is as follows:
<td colspan="6" align="center" bgcolor="#5BA8DE">Total${page.totalRecords} recordsTotal${page.totalPages}The current page<br> <a href="${path}/course/showAll.do?pageNo=${page.topPageNo }"><input type="button" name="fristPage" value="Home" /></a> <c:choose> <c:when test="${page.pageNo!=1}"> <a href="${path}/course/showAll.do?pageNo=${page.previousPageNo }"><input type="button" name="previousPage" value="Previous page" /></a> </c:when> <c:otherwise> <input type="button" disabled="disabled" name="previousPage" value="Previous page" /> </c:otherwise> </c:choose> <c:choose> <c:when test="${page.pageNo != page.totalPages}"> <a href="${path}/course/showAll.do?pageNo=${page.nextPageNo }"><input type="button" name="nextPage" value="nextpage" /></a> </c:when> <c:otherwise> <input type="button" disabled="disabled" name="nextPage" value="nextPage" /> </c:otherwise> </c:choose> <a href="${path}/course/showAll.do?pageNo=${page.bottomPageNo }"><input type="button" name="lastPage" value="Last Page" /></a> </td> </tr> </tr>Page effect:
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.