mark一个有点蠢的问题(初学者)
Springboot整合jpa中分页想到pageHelper节省时间
结果调用jpa的findAll()方法时分页怎么也不成功,查询到的都是所有的数据
@RequestMapping("/productList")
String productList(Model model, @RequestParam(required = false,defaultValue = "1")Integer currentPage) {
PageInfo pageResult = productService.findByPage(currentPage, 5);
System.out.println(pageResult);
model.addAttribute("pageResult", pageResult);
return "aruk/backstage/productList";
}
实现层
@Override
public PageInfo findByPage(Integer currentPage,Integer PageSize) {
PageHelper.startPage(currentPage,PageSize);
List productList = productRepository.findAll();
return new PageInfo(productList);
}
原因分析:
保证在 PageHelper 方法调用后必须紧跟 Mybatis 查询方法
解决方案:
改用Mybatis的查询方法,调用Mapper中的查询方法即可
@Select("select * from product")
public List findAll();
测试代码
@Test
public void testPageHelper(){
PageHelper.startPage(1,3);
List productList = productMapper.findAll();
for (Product product : productList) {
System.out.println(product);
}
}
测试通过



