SpringBoot integrates PageHelper to implement paging query

foreword

This article introduces the MyBatis paging plugin PageHelper. If you are also using MyBatis, it is recommended to try this paging plugin. It must be the most convenient paging plugin to use. The paging plugin supports any complex single-table and multi-table paging.

Official documentation: https://pagehelper.github.io/Project
address: https://github.com/pagehelper/Mybatis-PageHelper

Instructions

import dependencies

Search in the central repository sonatypepageHelper , pagehelper-spring-boot-starter
insert image description here
find the optional version, and select the dependency coordinates corresponding to the dependency management tool you are using. For example, the dependency management tool I use is Maven, then I will select the dependency coordinates corresponding to Maven.

Added pagehelper-spring-boot-starterMaven dependencies

<dependency>
  <groupId>com.github.pagehelper</groupId>
  <artifactId>pagehelper-spring-boot-starter</artifactId>
  <version>1.4.1</version>
</dependency>

After adding this dependency, there is no need to add the dependency of MyBatis, because the dependency depends on MyBatis by default:

write configuration file

Use yaml format:

pagehelper:
  helper-dialect: mysql
  reasonable: true
  support-methods-arguments: true
  params: count=countSql

Use the properties format:

# 指定分页插件的方言
pagehelper.helper-dialect=mysql
# 分页合理化
pagehelper.reasonable=true
# 支持方法参数
pagehelper.support-methods-arguments=true
#配置参数映射
pagehelper.params=count=countSql

Parameter Description:

Refer to official documentation

Prepare the data sheet

Prepare the Mapper interface

Test query all data


operation result:

The default SQL statement is to query all data

PageHelper paging query

Use the PageHelper.startPage(pageNum,pageSize)method to set the paging information, which are the current page number and the total number of records displayed on each page.

Note : The paging information must be set before the method in the mapper interface is executed.

Running result:

The PageHelper.startPage(pageNum,pageSize) method only takes effect for the next query

There are 2 queries for all data below, and the PageHelper.startPage(pageNum,pageSize)method is inserted before the first query of all data, so the first query is a paging query, and there is no insert method before the second query PageHelper.startPage(pageNum,pageSize), so it is not a paging query.

Running result:

Verify that all the data in the database are indeed only 7:

Guess you like

Origin blog.csdn.net/qq_31762741/article/details/123227631