Hello everyone, in this chapter we add PageHelper page query. If you have any questions, please contact me at [email protected]. Ask for directions of various gods, thank you

What is PageHelper

PageHelper is a free and open source plugin for Mybatis

Physical pages

Supports 12 common databases. Oracle, MySql and MariaDB, SQLite, DB2, PostgreSQL, essentially, etc

Multiple paging methods are supported

Support common RowBounds(PageRowBounds), PageHelper.startPage method call, Mapper interface parameter call

Add the PageHelper dependency

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

Then right-click Maven→Reimport to download the dependency

Add the PageHelper configuration

Add in application.properties

logging.level.com.example.demo.dao=DEBUG
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql
pagehelper.page-size-zero=trueCopy the code

Four: use method

UserInfoMapper.xml

<select id="selectAll" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List"/>
    from user_info
</select>Copy the code

UserInfoMapper

List<UserInfo> selectAll();Copy the code

UserInfoService

PageInfo<UserInfo> selectAll(Integer page,Integer size);Copy the code

UserInfoServiceImpl

@override public PageInfo<UserInfo> selectAll(Integer page, Integer size) { Only the first Mybatis query (Select) method immediately after the PageHelper.startPage method written above the query will be paginated. PageHelper.startPage(page, size); List<UserInfo> userInfoList = userInfoMapper.selectAll(); PageInfo<UserInfo> pageInfo = new PageInfo<>(userInfoList);return pageInfo;
}Copy the code

UserInfoController

@ApiOperation(value = "Query user", notes = "Paging query user owned")
@ApiImplicitParams({
        @ApiImplicitParam(name = "page", value = "Current page number",
                dataType = "Integer", paramType = "query"),
        @ApiImplicitParam(name = "size", value = "Number of items per page",
                dataType = "Integer", paramType = "query")
})
@PostMapping("/selectAll")
public RetResult<PageInfo<UserInfo>> selectAll(@RequestParam(defaultValue = "0") Integer page,
                                      @RequestParam(defaultValue = "0") Integer size) {
    PageInfo<UserInfo> pageInfo = userInfoService.selectAll(page, size);
    return RetResponse.makeOKRsp(pageInfo);
}Copy the code

PageHelper PageInfo member variable

Private int pageNum; Private int pageSize; Private int size; Private int startRow; private int startRow; // The row number of the last element in the current page private int endRow; Private long total; Private int pages; Private List<T> List; // firstPage private int firstPage; Private int prePage; Private Boolean isFirstPage; Private Boolean isLastPage; Private Boolean hasPreviousPage; Private Boolean hasNextPage; Private int navigatePages; Private int[] navigatepageNums;Copy the code

Six: functional testing

Address: http://192.168.1.104:8080/userInfo/selectAll

Case 1: No parameter is sent. The default value is all query

{
    "code": 200,
    "data": {
        "endRow": 2."firstPage": 0."hasNextPage": false."hasPreviousPage": false."isFirstPage": false."isLastPage": true."lastPage": 0."list": [{"id": 1,
                "userName": "Mr_ early morning"
            },
            {
                "id": 2."userName": "Mr_ early morning"}]."navigateFirstPage": 0."navigateLastPage": 0."navigatePages": 8,
        "navigatepageNums": []."nextPage": 0."orderBy": ""."pageNum": 0."pageSize": 0."pages": 0."prePage": 0."size": 2."startRow": 1,
        "total": 2}."msg": "success"
}Copy the code

Page =2&size=1

{
    "code": 200,
    "data": {
        "endRow": 2."firstPage": 1,
        "hasNextPage": false."hasPreviousPage": true."isFirstPage": false."isLastPage": true."lastPage": 2."list": [{"id": 2."userName": "Mr_ early morning"}]."navigateFirstPage": 1,
        "navigateLastPage": 2."navigatePages": 8,
        "navigatepageNums": [1, 2],"nextPage": 0."orderBy": ""."pageNum": 2."pageSize": 1,
        "pages": 2."prePage": 1,
        "size": 1,
        "startRow": 2."total": 2}."msg": "success"
}Copy the code

The project address

Code cloud address: gitee.com/beany/mySpr…

GitHub address: github.com/MyBeany/myS…

Writing articles is not easy, if it is helpful to you, please help click star

At the end

Add PageHelper paging query function has been completed, subsequent functions will be updated in succession, if you have any questions, please contact me at [email protected]. Ask for directions from various gods, thank you.