Many times we write DAO interfaces to write a method to query all the records, but when the data volume is very large, query all the records will be very slow, we need to use paging query. Pagehelper is a great pagination plug-in.

1, configuration,

Pagehelper project address: address

All we need to do is add the following dependencies to Maven:

<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> The < version > 1.3.1 < / version > < / dependency >Copy the code

2. Perform paging query

Here’s the DAO and Mapper XML query:

Mapper XML select node:

<select id="getAll" resultMap="userResultMap">
    select * from `user`
</select>
Copy the code

The DAO:

/** * getAll users */ List<User> getAll();Copy the code

The User class represents a POJO class for a User.

Then write a method to query the user of the specified page in the Service:

@Autowired private UserDAO userDAO; Public Page<User> getUserList(int currentPage, int currentPage, int currentPage) int pageSize) { Page<User> userPage = PageHelper.startPage(currentPage, pageSize).doSelectPage(() -> userDAO.getAll()); return userPage; }Copy the code

Use PageHelper class can be very easy for paging query! The startPage method sets the basic parameters of a pagination query, returns a PageMethod object, and then on top of that, Execute the doSelectPage method of the PageMethod object. This method executes all of our Mybatis query methods through the lambda statement, which automatically completes the paging logic internally and returns the data record for the specified page number.

The result of the query is a Page object, which has the following methods:

  • GetPageNum: Gets the current page number
  • GetPages: Gets the total number of pages
  • GetTotal: indicates the total number of records
  • GetResult: Gets the data record for the current page as a List collection

So in Controller we can call Service to get the resulting Page object, and then call getResult to get that Page:

Userservice.getuserlist (1, 15).getresult ();Copy the code

The first parameter of the startPage method above is written as 0 and 1, which are the same to get the first page.

3. Optimize the paging model

Real business often encapsulates its own class to represent the result of our paging query:

import com.github.pagehelper.Page; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; import java.io.Serializable; import java.util.List; /** * */ @setter @getter @noargsconstructor public class Paging<T> Implements Serializable {/** * Current page. */ @setter @getter @noargsconstructor Public class Paging<T> Implements Serializable {/** * Current page */ private int currentPageNum; / / private int totalPageNum; Private int pageSize; / / private int pageSize; /** * totalCount */ private long totalCount; /** * List<T> dataCurrentPage; Public Paging(Page<T> pageResult) {/** * @param pageResult */ public Paging(Page<T> pageResult) { this.currentPageNum = pageResult.getPageNum(); this.totalPageNum = pageResult.getPages(); this.totalCount = pageResult.getTotal(); this.pageSize = pageResult.getPageSize(); this.dataCurrentPage = pageResult.getResult(); }}Copy the code

Here we encapsulate a Paging class, representing the Paging result class we are querying. Then modify the above Service as follows:

@Autowired private UserDAO userDAO; /** * querying a User ** @param pageNum currentPage number * @param pageSize number of records on a page */ public Paging<User> getUserList(int currentPage, int pageSize) { Page<User> userPage = PageHelper.startPage(currentPage, pageSize).doSelectPage(() -> userDAO.getAll()); Paging<User> userPaging = new Paging<User>(userPage); return userPaging; }Copy the code

It is more convenient to have the Service process the required properties in the Page object for the Paging result we get, and put in our own Paging model, Paging, and return.