5.1Mybatis的分页处理pagehelper

一.在pom引入相关包


    
      com.github.pagehelper
      pagehelper
      4.1.0
    
    
      com.github.miemiedev
      mybatis-paginator
      1.2.17
    
    
      com.github.jsqlparser
      jsqlparser
      0.9.4
    

二.方法参数给出默认值

public ServerResponse getList(HttpSession session, @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,@RequestParam(value = "pageSize",defaultValue = "10") int pageSize) {
    //code
    ...
}

三.service层中实现分页逻辑

public ServerResponse getProductList(int pageNum, int pageSize) {
        //1.startPage--start
        PageHelper.startPage(pageNum,pageSize);
        //2.填充自己的sql查询逻辑
        List productList = productMapper.selectList();

        List productListVoList = Lists.newArrayList();
        for (Product productItem : productList) {
            ProductListVo productListVo = assembleProductListVo(productItem);
            productListVoList.add(productListVo);
        }
        //3.pageHelper--收尾
        PageInfo pageResult = new PageInfo(productList);
        //分页结果重置
        pageResult.setList(productListVoList);
        return ServerResponse.createBySuccess(pageResult);
    }
    //productListVo的组装方法
    private ProductListVo assembleProductListVo(Product product){
        ProductListVo productListVo = new ProductListVo();
        productListVo.setId(product.getId());
        productListVo.setName(product.getName());
        productListVo.setCategoryId(product.getCategoryId());
        productListVo.setImageHost(PropertiesUtil.getProperty("ftp.server.http.prefix","http://img.liuhe.link/"));
        productListVo.setMainImage(product.getMainImage());
        productListVo.setPrice(product.getPrice());
        productListVo.setSubtitle(product.getSubtitle());
        productListVo.setStatus(product.getStatus());
        return productListVo;
    }

你可能感兴趣的:(5.1Mybatis的分页处理pagehelper)