本文共 1444 字,大约阅读时间需要 4 分钟。
第一步:在pom.xml引入PageHelper的依赖
com.github.pagehelper pagehelper-spring-boot-starter 1.2.3
第二步:在application.yml文件中配置PageHelper分页插件
#分页插件#pagehelper.dialect=com.github.pagehelper.dialect.helper.MySqlDialectpagehelper.helperDialect=mysqlpagehelper.reasonable=truepagehelper.supportMethodsArguments=truepagehelper.params=count=countSql
第三步:在service层中运用
接口:
List
实现类:
@Override public List
测试:
@Test void TestMybatis(){ Listlist = videoService.getVideoListByPage(1,10); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } PageInfo page = new PageInfo<>(list); System.out.println(page.getList()); }
注:关键的三步是在查询数据方法前先使用:
PageHelper.startPage(pageNum, pageSize);
如果你的list中存的是实体类对象很有可能直接打印list并看不到具体的数据记录。返回的list其实已经是分页之后的结果,但是直接打印list并看不到具体的数据记录,只能看到一些分页信息:
想要看到详细的分页数据,可以通过循环遍历list数据:
for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); }
如果list中存的是Map对象可以直接通过page.getList()方法获取数据集。
转载地址:http://mfuqb.baihongyu.com/