SpringBoot整合PageHelper插件

SpringBoot整合pageHelper插件的时候主要分为以下几步?

1.在pom.xml中引入依赖


    com.github.pagehelper
    pagehelper-spring-boot-starter
    1.2.3

2.在Application启动类中添加或者直接在application.properties中去配置

  application.properties

#分页插件
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql
//配置mybatis的分页插件pageHelper
@Bean
public PageHelper pageHelper(){
    System.out.println("开始配置数据分页插件");
    PageHelper pageHelper = new PageHelper();
    Properties properties = new Properties();
    properties.setProperty("offsetAsPageNum","true");
    properties.setProperty("rowBoundsWithCount","true");
    properties.setProperty("reasonable","true");
    //配置mysql数据库的方言
    properties.setProperty("dialect","mysql");
    pageHelper.setProperties(properties);
    return pageHelper;
}

3.构建PageVo类(这个PageVo可以根据个人情况进行拓展,添加相应的字段)

 

public class PageVo implements Serializable{
    private Integer count;
    private Integer pageSize;
    private Integer page;
    private List data;

    public Integer getCount() {
        return count;
    }

    public void setCount(Integer count) {
        this.count = count;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public List getData() {
        return data;
    }

    public void setData(List data) {
        this.data = data;
    }


}

4.下面就是封装数据了,我以我最近写的查询租赁列表的接口为例

1)Controller层

 

@Autowired
private ProductService productService;

@ApiOperation(value = "租赁商品列表", notes = "租赁商品列表")
@ApiImplicitParams({
        @ApiImplicitParam(name="pageSize",value = "每页的条数",required = false,dataType = "Integer",paramType = "query"),
        @ApiImplicitParam(name="page",value = "当前页数",required = false,dataType = "Integer",paramType = "query")
})
@RequestMapping(value = "/rest/products/list", method = RequestMethod.GET)
public ResponseEntity list(@RequestParam(value = "pageSize",required = false,defaultValue = "10")Integer pageSize,
                                              @RequestParam(value = "page",required = false,defaultValue = "1")Integer page) {
    ProductEntity params = new ProductEntity();
    params.setType(ProductEnum.TYPE_RENT.getCode());
    params.setIsDelete(ProductEnum.NOT_DELETE.getCode());
    params.setIsSale(ProductEnum.SALE.getCode());
    PageVo result = productService.list(params,pageSize,page);
    return ResponseEntity.ok(JsonResultUtils.success(result, MessageEnum.STATUS_OK.getCode(), MessageEnum.STATUS_OK.getMessage()));
}

2)Service层(注意PageHelper做分页一定要在查询之前)

 

@Autowired
private ProductMapper productMapper;
@Override
public PageVo list(ProductEntity params, Integer pageSize, Integer page) {
    PageVo pageVo=new PageVo();
    pageVo.setPageSize(pageSize);
    pageVo.setPage(page);
    PageHelper.startPage(page,pageSize);
    List data = productMapper.listProduct(params);
    List resultAddCoverPre = new ArrayList();
    for(ProductEntity productEntity:data) {
        productEntity.setCover(img_base_url + productEntity.getCover());
        resultAddCoverPre.add(productEntity);
    }
    pageVo.setData(resultAddCoverPre);
    Integer count=productMapper.getCount();
    pageVo.setCount(count);
    return pageVo;
}

3)mapper以及mapper.xml

 

select id="listProduct" parameterType="com.taozugong.dao.entity.ProductEntity" resultMap="BaseResultMap">
    SELECT
    
    FROM product
    
        
            type = #{type}
        
        
            AND is_delete = #{isDelete}
        
        
            AND is_sale = #{isSale}
        
    

运行即可成功!

你可能感兴趣的:(工程化专题)