在 pom.xml 中添加以下依赖:git
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>github
能够这样调用,PageHelper.startPage(1,10)表示从第一页开始,每页10条记录,返回值为Page<>。spring
@RestController public class ProductController { @Autowired ProductMapper productMapper; @RequestMapping("getProduct") public Page<Product> getProduct(){ PageHelper.startPage(1,10); Page<Product> productList = (Page<Product>) productMapper.selectAll(); return productList; } }
这是直接经过mapper获取DO数据能够直接使用,List<>能够被直接强转为Page<>。app
若是要转换为DTO或VO,须要经过下面的方式spring-boot
1 @RequestMapping("getProduct") 2 public PageInfo<ProductDTO> getProduct(){ 3 PageHelper.startPage(1,10); 4 5 List<ProductDTO> productDTOS = new ArrayList<>(); 6 List<Product> productList = productMapper.selectAll(); 7 PageInfo<Product> pageInfo = new PageInfo<>(productList); 8 9 for (Product product : productList) { 10 ProductDTO productDTO = new ProductDTO(); 11 BeanUtils.copyProperties(product,productDTO); 12 productDTOS.add(productDTO); 13 } 14 15 PageInfo pageResult = new PageInfo(productDTOS); 16 return pageResult; 17 }
返回值为PageInfo<>类型,由于List<DTO>为ArrayList类,不能直接转为Page<>,因此放在PageInfo<>中。spa
前端取list中的内容便可,其他为参数信息。code
PageHelper.startPage
静态方法调用除了 PageHelper.startPage
方法外,还提供了相似用法的 PageHelper.offsetPage
方法。xml
在你须要进行分页的 MyBatis 查询方法前调用 PageHelper.startPage
静态方法便可,紧跟在这个方法后的第一个MyBatis 查询方法会被进行分页。blog