The SpringBoot project uses MyBatis's paging plug-in pagehelper

For some of the data displayed in the list, because the amount of data may be relatively large, we are accustomed to using paging. At this time, the paging plug-in pagehelper provided by MyBatis can achieve this function.

It is relatively simple to use pagehelper in the springboot project. Before paging is done, our service layer calls the dao layer, and after getting the displayed list data, it directly returns to the front-end page through the controller layer. To implement paging here, you need to change the operations of the controller layer and the service layer, and only two very important classes, PageHelper and PageInfo, are needed.

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

The first step: first introduce pagehelper dependency

<!--分页插件-->
	<dependency>
		<groupId>com.github.pagehelper</groupId>
		<artifactId>pagehelper-spring-boot-starter</artifactId>
		<version>1.2.3</version>
	</dependency>

Perform some configuration (some say that it is useless and does not need to be configured)

#分页插件配置
pagehelper:
  reasonable: true
  helper-dialect: mysql
  support-methods-arguments: true
  params: count=countSql

Step 2: Then at the controller layer.
Under our interface, we need to pass two basic parameters, pageNo (number of pages) and pageSize (how many pieces of data are stored in one). The specific code is as follows

@GetMapping(value = "/product_list")
    public String product_list(Model model, 
                               @RequestParam(value = "pageNo",defaultValue = "1")int pageNo,
                               @RequestParam(value = "pageSize",defaultValue = "6")int pageSize){
    
    
        PageInfo<ProductVo> page = productService.listProcuctVo(pageNo,pageSize);
        model.addAttribute("pageInfo",page);
        return "product_list";
    }

It can be seen here that the service layer returns an object of PageInfo <entity class>. It encapsulates some information of the paging and the returned list data.

Step 3: Writing the service layer
This is the most important step. The service layer accepts the list data from the dao layer and the pageNo page number and pageSize per page number two parameters from the controller layer. The specific code reference is as follows.

  public PageInfo<ProductVo> listProcuctVo(int pageNo, int pageSize) {
    
    
        PageHelper.startPage(pageNo,pageSize);
        List<ProductVo> productVoList = productMapper.selectPorductlist();
        PageInfo<ProductVo> page = new PageInfo<ProductVo>(productVoList);
        return page;
    }

Two important places here

 PageHelper.startPage(pageNo,pageSize);

Set the number of pages and page size.
After receiving the list data from the dao layer, call PageInfo to convert the paging operation data.

PageInfo<ProductVo> page = new PageInfo<ProductVo>(productVoList);

Here you need to pay special attention to the writing of call parameters.
The specific data encapsulated by PageInfo is as follows:

public class PageInfo<T> implements Serializable {
    
    
    private static final long serialVersionUID = 1L;
    private int pageNum;//当前页
    private int pageSize;//当前页数据量
    private int size;
    private int startRow;
    private int endRow;
    private long total;//总数据量
    private int pages;//总页数
    private List<T> list;//封装需要返回的数据列表
    private int prePage;//上一页
    private int nextPage;//下一页
    private boolean isFirstPage;
    private boolean isLastPage;
    private boolean hasPreviousPage;//首页
    private boolean hasNextPage;//尾页
    private int navigatePages;
    private int[] navigatepageNums;//页数数组
    private int navigateFirstPage;
    private int navigateLastPage;
    }

The page here actually encapsulates all the parameters in the paging, the variables that appear in the above code.
Step 4: Page display
When we return the pageInfo object in the model using the name pagInfoe in the controller layer.

 model.addAttribute("pageInfo",page);

The parameter list of the current page we need is in pageInfo.list, and you can directly substitute pageInfo.xx with other parameters.
Partial display
This is the data to get the data list

<tr class="success" th:each="product : ${pageInfo.list}">
        <td th:text="${product.product_name}"></td>
        <td><img th:src="@{
    
    ${product.product_img}}" width="60px" height="60px"/></td>
        <td th:text="${product.product_price}"></td>
        <td th:text="${product.miaosha_price}"></td>
        <td th:text="${product.stock_count}"></td>
        <td> <a th:href="'/product_detail/'+${product.id}">详情</a></td>
    </tr>

This is to get the relevant data of the page

 <ul class="pager">
        <li th:if="${pageInfo.hasPreviousPage}">
            <a th:href="'?pageNo=1'">首页</a>
        </li>
        <li th:if="${pageInfo.hasPreviousPage}">
            <a th:href="'?pageNo='+${pageInfo.prePage}">上一页</a>
        </li>
        <li th:each="nav : ${pageInfo.navigatepageNums}">
            <a th:href="'?pageNo='+${nav}" th:text="${nav}" th:if="${nav != pageInfo.pageNum}"></a>
            <span style="font-weight: bold" th:if="${nav == pageInfo.pageNum}" th:text="${nav}"></span>
        </li>
        <li th:if="${pageInfo.hasNextPage}">
            <a th:href="'?pageNo='+${pageInfo.nextPage}">下一页</a>
        </li>
        <li th:if="${pageInfo.hasNextPage}">
            <a th:href="'?pageNo='+${pageInfo.pages}">尾页</a>
        </li>
        <span>
        当前页号:<span th:text="${pageInfo.pageNum}"> </span> / <span th:text="${pageInfo.pages}"></span>: 总页数</span>&nbsp;&nbsp;&nbsp;&nbsp;
        总结果数:<span th:text="${pageInfo.total}"></span>
    </ul>

Show results
Insert picture description here

Guess you like

Origin blog.csdn.net/Wangdiankun/article/details/106460420