引言
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/301b46a11ee200fce26953c1a5bbfec9.png#pic_center)
实现
- 首先需要引入需要的依赖:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.13</version>
</dependency>
- 在application.properties中配置分页相关信息:
# pagehelper配置
pagehelper.helper-dialect=mysql
pagehelper.reasonable=true
pagehelper.support-methods-arguments=true
pagehelper.params=count=countSql
- 在controller中实现我们的分页方法如下:
@ResponseBody
@GetMapping("/course/list2")
public Object list2() {
PageHelper.startPage(0, 3);
List<VipCourse> all = courseDao.findAll();
PageInfo<VipCourse> pageInfo = new PageInfo<>(all);
return pageInfo;
}
- 这里由于使用了Mybatis,所以需要我们自己写Mapper接口:
public interface CourseDao {
@Select("select course_id courseId, course_name courseName, price, description from vip_course")
List<VipCourse> findAll();
}
- 效果:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/16d0bb382a4bb3daee6e62fb411fab3a.png#pic_center)
附
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/9191a039809d21fc069cd89f561bea3b.png#pic_center)