z6189949
z6189949
2019-03-15 00:52

springboot2 PageHelper无法分页

  • spring
  • java
  • intellij-idea

核心代码
Service层
public ServerResponse getProductList(int pageNum, int pageSize) {
PageHelper.startPage(pageNum, pageSize);
List productList = productRepository.findAll();

    List<ProductListVo> productListVoList = new ArrayList<>();
    for (Product productItem : productList) {
        ProductListVo productListVo = assembleProductListVo(productItem);
        productListVoList.add(productListVo);
    }
    PageInfo pageResult = new PageInfo(productListVoList);
    return ServerResponse.createBySuccess(pageResult);
}

private ProductListVo assembleProductListVo(Product product) {
    ProductListVo productListVo = new ProductListVo();
    productListVo.setId(product.getId());
    productListVo.setName(product.getName());
    productListVo.setCategoryId(product.getCategoryId());
    productListVo.setImageHost( "http://img.sunxiaochuan.xyz/");
    productListVo.setMainImage(product.getMainImage());
    productListVo.setPrice(product.getPrice());
    productListVo.setSubtitle(product.getSubtitle());
    productListVo.setStatus(product.getStatus());
    return productListVo;
}

Controller层
@RequestMapping(value = "list", method = RequestMethod.POST)
@ResponseBody
public ServerResponse getList(HttpSession session, @RequestParam(value = "pageNum", defaultValue = "1") int pageNum, @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {

    //填充业务
    return productService.getProductList(pageNum, pageSize);

}

    yml配置
    pagehelper:

helper-dialect: mysql
reasonable: true
support-methods-arguments: true
params: count=countSql

pom引用
  <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper</artifactId>
        <version>5.1.8</version>
    </dependency>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.3.2</version>
    </dependency>
    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper-spring-boot-autoconfigure</artifactId>
        <version>1.2.10</version>
    </dependency>
    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper-spring-boot-starter</artifactId>
        <version>1.2.10</version>
    </dependency>
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答