项目结构

测试代码:https://github.com/mouday/SpringBoot-MyBaits-PageHelper

新建SpringBoot项目

$ tree
.
├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── example
    │   │           └── demo
    │   │               ├── Application.java
    │   │               ├── bean
    │   │               │   ├── RequestPage.java
    │   │               │   └── User.java
    │   │               ├── controller
    │   │               │   └── UserController.java
    │   │               ├── dao
    │   │               │   └── UserDao.java
    │   │               └── service
    │   │                   ├── UserService.java
    │   │                   └── impl
    │   │                       └── UserServiceImpl.java
    │   └── resources
    │       └── application.properties
    └── test

依赖和配置

pom.xml

<!--MyBatis-->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.3</version>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

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

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>

application.properties


spring.datasource.url=jdbc:mysql://localhost:3306/data
spring.datasource.username=root
spring.datasource.password=123456

mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

表结构

CREATE TABLE `user` (
  `id` int(13) NOT NULL AUTO_INCREMENT COMMENT '主键',
  `name` varchar(33) DEFAULT NULL COMMENT '姓名',
  `age` int(3) DEFAULT NULL COMMENT '年龄',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

相关代码

User.java

package com.example.demo.bean;

import lombok.Data;

@Data
public class User {
    private Integer id;
    private String name;
    private Integer age;
}

RequestPage.java

package com.example.demo.bean;

import org.springframework.util.StringUtils;

/**
 * 分页所需参数
 */
public class RequestPage {
    private Integer page;
    private Integer size;

    /**
     * 页码,为非必传参数,默认值为 1
     */
    public Integer getPage() {
        return StringUtils.isEmpty(page) ? 1 : page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    /**
     * 大小,非必传参数,默认值为 10
     */
    public Integer getSize() {
        return StringUtils.isEmpty(size) ? 10 : size;
    }

    public void setSize(Integer size) {
        this.size = size;
    }
}

UserDao.java

package com.example.demo.dao;


import com.example.demo.bean.User;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface UserDao {

    @Select("select * from user")
    Page<User> selectAllUserPage();

    @Select("select * from user")
    List<User> selectAllUserList();
}

UserService.java

package com.example.demo.service;

import com.example.demo.bean.User;
import com.github.pagehelper.Page;

import java.util.List;

public interface UserService {
    Page<User> selectAllUserPage();

    List<User> selectAllUserList();
}

UserServiceImpl.java

package com.example.demo.service.impl;

import com.example.demo.bean.User;
import com.example.demo.dao.UserDao;
import com.example.demo.service.UserService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserDao userDao;

    @Override
    public Page<User> selectAllUserPage() {
        return userDao.selectAllUserPage();
    }

    @Override
    public List<User> selectAllUserList() {
        return userDao.selectAllUserList();
    }
}

UserController.java

package com.example.demo.controller;

import com.example.demo.bean.RequestPage;
import com.example.demo.bean.User;
import com.example.demo.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {

    @Autowired
    UserService userService;

    /**
     * 返回数据列表和分页信息
     * @param requestPage
     * @return
     */
    @GetMapping("selectAllUserPage")
    public PageInfo<User> selectAllUserPage(RequestPage requestPage){
        PageHelper.startPage(requestPage.getPage(), requestPage.getSize());
        Page<User> page = userService.selectAllUserPage();
        PageInfo<User> pageInfo = new PageInfo<>(page);
        return pageInfo;
    }

    /**
     * 返回数据列表
     * @param requestPage
     * @return
     */
    @GetMapping("selectAllUserList")
    public List<User> selectAllUserList(RequestPage requestPage){
        PageHelper.startPage(requestPage.getPage(), requestPage.getSize());
        List<User> list = userService.selectAllUserList();
        return list;
    }
}

访问测试

GET http://localhost:8080/selectAllUserList?page=2&size=1

[
    {
        id: 2,
        name: "Tom",
        age: 25
    }
]

执行sql

==>  Preparing: SELECT count(0) FROM user
==> Parameters: 
<==    Columns: count(0)
<==        Row: 2
<==      Total: 1
==>  Preparing: select * from user LIMIT ?, ?
==> Parameters: 1(Integer), 1(Integer)
<==    Columns: id, name, age, money
<==        Row: 2, Tom, 25, 30.5
<==      Total: 1
GET http://localhost:8080/selectAllUserPage?page=2&size=1

{
    total: 2,
    list: [
        {
            id: 2,
            name: "Tom",
            age: 25
        }
    ],
    pageNum: 2,
    pageSize: 1,
    size: 1,
    startRow: 2,
    endRow: 2,
    pages: 2,
    prePage: 1,
    nextPage: 0,
    isFirstPage: false,
    isLastPage: true,
    hasPreviousPage: true,
    hasNextPage: false,
    navigatePages: 8,
    navigatepageNums: [
        1,
        2
    ],
    navigateFirstPage: 1,
    navigateLastPage: 2
}
==>  Preparing: SELECT count(0) FROM user
==> Parameters: 
<==    Columns: count(0)
<==        Row: 2
<==      Total: 1
==>  Preparing: select * from user LIMIT ?, ?
==> Parameters: 1(Integer), 1(Integer)
<==    Columns: id, name, age, money
<==        Row: 2, Tom, 25, 30.5
<==      Total: 1

参考
springboot整合pagehelper实现分页