1.在SpringBoot项目中增加相关依赖,完整pom.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>cn.edu.tju</groupId>
<artifactId>springboot-mybatis-pagehelper</artifactId>
<version>1.0.0</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>

<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.13</version>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

</plugins>
</build>


</project>

2.创建启动类:

package cn.edu.tju;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("cn.edu.tju.mapper")
public class Start {
public static void main(String[] args) {
SpringApplication.run(Start.class,args);
}
}

3.在resources目录下创建主配置文件application.properties:

#端口号
server.port=8087
#数据库配置
spring.datasource.url=jdbc:mysql://139.198.xx.xx/test
spring.datasource.username=root
spring.datasource.password=YourPassword
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#数据库字段下划线和实体类驼峰映射配置
mybatis.configuration.map-underscore-to-camel-case=true
#分页插件pagehelper配置
pagehelper.helper-dialect=mysql
pagehelper.reasonable=true
pagehelper.support-methods-arguments=true
pagehelper.page-size-zero=false

4.在MySQL的 test数据库执行如下建表&写数据脚本

create table student_info(id int auto_increment primary key, full_name varchar(50),age int);
insert into student_info(full_name,age) values('paul',23);
insert into student_info(full_name,age) values('david',25);
insert into student_info(full_name,age) values('micheal',19);
insert into student_info(full_name,age) values('cindy',22);
insert into student_info(full_name,age) values('richard',28);
insert into student_info(full_name,age) values('henry',20);
insert into student_info(full_name,age) values('newton',17);
insert into student_info(full_name,age) values('sharp',25);
insert into student_info(full_name,age) values('kate',21);
insert into student_info(full_name,age) values('bill',22);

5.在项目中创建实体类:

package cn.edu.tju.domain;

public class StudentInfo {
private int id;
private String fullName;
private int age;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getFullName() {
return fullName;
}

public void setFullName(String fullName) {
this.fullName = fullName;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}
}

6.在项目中创建mapper类:

package cn.edu.tju.mapper;

import cn.edu.tju.domain.StudentInfo;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface StudentInfoMapper {
@Select("select * from student_info")
List<StudentInfo> getStudentList();
}

7.创建service类,注入mapper类

package cn.edu.tju.service;

import cn.edu.tju.domain.StudentInfo;
import cn.edu.tju.mapper.StudentInfoMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentInfoService {
@Autowired
private StudentInfoMapper studentInfoMapper;
public List<StudentInfo> getStudentInfoListByPage(int page,int size){
//分页在下面这行代码来实现!!!
PageHelper.startPage(page,size);
List<StudentInfo> studentInfoList=studentInfoMapper.getStudentList();
return studentInfoList;
}
}

8.创建控制器类,注入service类,

package cn.edu.tju.controller;

import cn.edu.tju.domain.StudentInfo;
import cn.edu.tju.mapper.StudentInfoMapper;
import cn.edu.tju.service.StudentInfoService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@CrossOrigin
public class StudentInfoController {
@Autowired
private StudentInfoService studentInfoService;

@RequestMapping("/getStudentListByPage/{page}/{size}")
public List<StudentInfo> getStudentListByPage(@PathVariable int page,@PathVariable int size){

List<StudentInfo> studentInfoList=studentInfoService.getStudentInfoListByPage(page,size);
return studentInfoList;
}

}

9.运行启动类,在浏览器访问
​​​ http://localhost:8088/getStudentListByPage/2/3​​​ 也就是每页3条数据,第2页,
输出结果如下:
SpringBoot: MyBatis+PageHelper实现分页查询_java