在 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