PageHelper implements paging

PageHelper implements paging

If you are also using MyBatis, it is recommended to try the paging plugin, which must be the most convenient paging plugin. The paging plugin supports any complicated single-table and multi-table paging. ——PageHelper official website.

Backend implementation

Introduce dependencies

Before using PageHelper, we must first introduce its dependent files, or jar packages. The maven project demo is used here, so the dependency file needs to be introduced first.

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>5.1.11</version>
</dependency>

Add plugin to MyBatis configuration file

To use PageHelper, you need to add the following configuration to the global configuration file of MyBatis. The dialect of the database needs to be configured according to the situation of using the database.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<plugins>
		<!-- com.github.pagehelper 为 PageHelper 类所在包名 -->
		<plugin interceptor="com.github.pagehelper.PageHelper">
			<!-- 支持数据库类型: Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL -->
			<property name="dialect" value="mysql"/>
		</plugin>
	</plugins>
</configuration>

Create PageResult class

When using paging, the total number of pages and the content of each page are often passed to the front end, ie total and List . In view of the fact that we will use paging heavily in the program, the first step in implementing paging is to create a PageResult class.

This class contains two attributes, total and rows. Because rows in this class do not refer to any type, you cannot specify generics.

If you want to use RPC technology such as dubbo, please be sure to implement the Serializable interface.

package entity;

import java.io.Serializable;
import java.util.List;

/**
 * Created by [email protected] Luna on 2020/4/13 0:44
 */
public class PageResult implements Serializable {
    //总记录数
    private Long total;
    //当前页内容
    private List rows;

    public Long getTotal() {
        return total;
    }

    public void setTotal(Long total) {
        this.total = total;
    }

    public List getRows() {
        return rows;
    }

    public void setRows(List rows) {
        this.rows = rows;
    }

    public PageResult(Long total, List rows) {
        this.total = total;
        this.rows = rows;
    }

    public PageResult() {
    }
}

Write code

First write the Controlelr layer code

  • First create a findPage () method to receive the page number (PageNo) and the entry of each page of data (PageSize) passed by the front end.
    @RequestMapping("/findPage")
    public PageResult findPage(Integer pageNo,Integer pageSize){
        return brandService.findPage(pageNo,pageSize);
    }

It is also very simple in the Service layer

  • PageHelper.startPage(pageNo,pageSize);Paging query can be realized by adding directly to the previous line of all the query statements .
  • When querying all, you need to force the return result to Page Object.
      @Override
    public PageResult findPage(Integer pageNo, Integer pageSize) {
        //开启分页
        PageHelper.startPage(pageNo,pageSize);
        //查询数据
        Page<User> page = (Page<Brand>) userMapper.selectByExample(null);
        //返回结果
        return new PageResult(page.getTotal(),page.getResult());
    }

test

At this time, localhost:8080/user/findPage?pageNo=1&pageSize=5the five pieces of data on the first page can be obtained by calling directly on the front end .

Guess you like

Origin www.cnblogs.com/zhangruifeng/p/12689011.html