Implement paging: there are two ways: traditional paging and classification based on pageHelper plug-in. The latter is recommended
First, create a pageUtil tool manually to record all kinds of information of paging, and then use the tedious way to call data
pageUtil class can be customized as required
package com.imooc.project.util; /** * In order to use the traditional method for paging design, we need to establish an information class for paging to store the paging information * @author lqf * */ import java.util.List; public class PageUtil { private int currentPageNum; //What page do you want to see now private int pageSize=10 ; //Number of entries per page private int totalSize; //Total number of records private int startIndex; //Index of query start record limit ? ? limit Use private int totalPageNum; //PageCount private int nextPageNum; //next page private int prePageNum; //Previous page private List records; //Recordset of current page List<User> records Back to each page user Set //Number of pages used to display navigation on the page user can customize private int startPageNum; // home page private int endPageNum ; // trailer page private String url ; //Use the construction method. Pass the necessary two parameters: the first is the page number, the second is the total number of records to get all other data public PageUtil(int currentPageNum,int totalrecords) { this.currentPageNum=currentPageNum; this.totalSize= totalrecords ; /* * 1 0 - 10 limit(0,10) * 2 10 - 20 limit(10,10) * 3 20 -30 limit(20,30) * (Current page - 1) * 10 = index value of the page startIndex * (currentPageNum-1)*pageSize */ //Calculate index of start record startIndex=(currentPageNum-1)*pageSize; //Calculate total pages totalPageNum=totalSize%pageSize==0?totalSize/pageSize:totalSize/pageSize+1; //Calculate the start and end page numbers, which can be designed by itself 12..9 if(totalPageNum>9) { startPageNum=currentPageNum-4; endPageNum=currentPageNum+4; if(startPageNum<1) { startPageNum=1; endPageNum=startPageNum+8; } if(endPageNum>totalPageNum) { endPageNum=totalPageNum; startPageNum=endPageNum-8; } } else { startPageNum=1; endPageNum=totalPageNum; } } public int getStartPageNum() { return startPageNum; } public void setStartPageNum(int startPageNum) { this.startPageNum = startPageNum; } public int getEndPageNum() { return endPageNum; } public void setEndPageNum(int endPageNum) { this.endPageNum = endPageNum; } //Previous page public int getPrePageNum() { prePageNum=currentPageNum-1; if(prePageNum<=0) { prePageNum=1; } return prePageNum; } // next page public int getNextPageNum() { nextPageNum=currentPageNum+1; if(nextPageNum>totalPageNum) { nextPageNum=totalPageNum; } return nextPageNum; } public int getCurrentPageNum() { return currentPageNum; } public void setCurrentPageNum(int currentPageNum) { this.currentPageNum = currentPageNum; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getTotalSize() { return totalSize; } public void setTotalSize(int totalSize) { this.totalSize = totalSize; } public int getStartIndex() { return startIndex; } public void setStartIndex(int startIndex) { this.startIndex = startIndex; } public int getTotalPageNum() { return totalPageNum; } public void setTotalPageNum(int totalPageNum) { this.totalPageNum = totalPageNum; } public List getRecords() { return records; } public void setRecords(List records) { this.records = records; } public void setPrePageNum(int prePageNum) { this.prePageNum = prePageNum; } public void setNextPageNum(int nextPageNum) { this.nextPageNum = nextPageNum; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } }
Short for web layer
//Get current page String num = req.getParameter("num"); if(num==null) { num="1"; } //call pageUtil tool PageUtil page= null ; try { page=service.getAllUsers(num); } catch (Exception e) { e.printStackTrace(); } //Transfer data into the domain and forward to users.jsp Page req.setAttribute("page", page); req.getRequestDispatcher("/users.jsp").forward(req, resp);
service level
userDao userDao= new userDao() ; public static SqlSessionFactory sqlSessionFactory = null; //Get tool class information public PageUtil getAllUsers(String num) throws Exception{ //Current page int currentPageNum=1 ; // trim() Eliminate leading and trailing spaces if (num != null ) { currentPageNum = Integer.parseInt(num); System.out.println(currentPageNum); } int totalRecords =userDao.totalRecords(); PageUtil pg = new PageUtil(currentPageNum, totalRecords); List<User> users = userDao.getAllUsers(pg.getStartIndex(), pg.getPageSize()); pg.setRecords(users); return pg; }
dao
public class userDao extends BaseDao { //Get all the information public int totalRecords() throws Exception{ // Use jdbc Template as connection layer of data int result = 0; Connection conn = this.getConnection(); PreparedStatement ps = null; ResultSet rs = null; // count(*) Returns the number of rows found count(Column names) Returns the number of data items in a column count(*) count Set alias as count String sql = "select count(*) count from person"; ps = conn.prepareStatement(sql); rs = ps.executeQuery(); if (rs.next()) { result = rs.getInt("count"); } return result; } //Get the information of the corresponding page public List<User> getAllUsers(int startIndex, int pageSize) throws Exception{ //Use array to get data call DBCP Template QueryRunner queryRunner= new QueryRunner(); String sql= "select * from person limit ?,?"; List<User> users= null ; //Get data users=queryRunner.query(this.getConnection(), sql,new BeanListHandler<User>(User.class),new Object[]{startIndex,pageSize}); return users; } }
*************************************************
Use the mybatis pageHelper plug-in to develop
How to use the pageHelper plug-in
This is a tutorial for the developer of pageHelper on GitHub
https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/HowToUse.md
Configuration information of mybatis insert pagehelper plug-in
<! -- configure the page helper plug-ins in the mybatis configuration file in the correct order or an error will be reported -- > <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <! -- set six database types: Oracle, mysql, MariaDB, SQLite, HSQLDB and PostgreSQL -- > <property name="helperDialect" value="mysql"/> </plugin> </plugins>
mapper.xml
web level
pageHelper plug-in is much simpler than the traditional plug-in. After all, it is a plug-in. It is the control of the four objects of mybatis: parameterHandler, resultSetHandler, statementHandler, executor. The enhancement of the function realized by reflection dynamic agent is similar to the function of filter
However, it should be noted that the use of plug-ins will change the underlying design, so we should try to minimize the use of plug-ins