springboot整合pageheler以及pageheler遇到的坑

springboot整合pageheler以及pageheler遇到的坑

一、springboot整合pageheler
1.pom.xml中引入依赖

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

2.application.yml文件中进行配置

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

3.直接在查询方法的前面调用startPage()方法即可

public PageInfo findList(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page orderMasterPage = orderMasterMapper.findAll();
        PageInfo OrderMasterPageInfo = new PageInfo<>(orderMasterPage);
        return OrderMasterPageInfo2OrderDtoPageInfo.convert(OrderMasterPageInfo);
    }

二、使用pageheler时遇到的坑
1.当创建PageInfo对象时传递的参数是一个list时,就会发现返回的总页数总是等于每页的记录数。
列如:PageInfo OrderMasterPageInfo = new PageInfo<>(list);
通过查看pageheler的源代码可发现,当传递的参数是list和page对象时pageinfo属性的值是不一样的。所以在创建pageinfo对象的时候参数应该传递的是page对象。

public PageInfo(List list, int navigatePages) {
        super(list);
        this.isFirstPage = false;
        this.isLastPage = false;
        this.hasPreviousPage = false;
        this.hasNextPage = false;
        if (list instanceof Page) {
            Page page = (Page)list;
            this.pageNum = page.getPageNum();
            this.pageSize = page.getPageSize();
            this.pages = page.getPages();
            this.size = page.size();
            if (this.size == 0) {
                this.startRow = 0;
                this.endRow = 0;
            } else {
                this.startRow = page.getStartRow() + 1;
                this.endRow = this.startRow - 1 + this.size;
            }
        } else if (list instanceof Collection) {
            this.pageNum = 1;
            this.pageSize = list.size();
            this.pages = this.pageSize > 0 ? 1 : 0;
            this.size = list.size();
            this.startRow = 0;
            this.endRow = list.size() > 0 ? list.size() - 1 : 0;
        }

        if (list instanceof Collection) {
            this.navigatePages = navigatePages;
            this.calcNavigatepageNums();
            this.calcPage();
            this.judgePageBoudary();
        }

    }

2.当controller需要实体类型和mapper中查询到的实体类型不一致时,不仅要将实体进行转换,还要将pageinf的属性进行拷贝,负责获取到的总页数等不准确。
列如:我的controller需要的是 PageInfo类型,而mapper中查询到的是PageInfo类型,那就需要进行转换。(代码如下)

service层方法:
    public PageInfo findList(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page orderMasterPage = orderMasterMapper.findAll();
        PageInfo OrderMasterPageInfo = new PageInfo<>(orderMasterPage);
        return OrderMasterPageInfo2OrderDtoPageInfo.convert(OrderMasterPageInfo);
    }
controller层方法:
public ModelAndView findList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                 @RequestParam(value = "pageSize", defaultValue = "1") Integer pageSize,
                                 Map map) {
        PageInfo pageInfo = orderService.findList(pageNum, pageSize);
        map.put("pageInfo", pageInfo);
        map.put("currentPage", pageNum);
        map.put("pageSize", pageSize);
        return new ModelAndView("order/list", map);
    }
转换工具类
public class OrderMasterPageInfo2OrderDtoPageInfo {

    public static PageInfo convert(PageInfo orderMasterPageInfo){
        PageInfo orderDtoPageInfo = new PageInfo<>();
        BeanUtils.copyProperties(orderMasterPageInfo,orderDtoPageInfo);
        List orderDtoList = OrderMasterConvert2OrderDto.convert(orderMasterPageInfo.getList());
        orderDtoPageInfo.setList(orderDtoList);
        return orderDtoPageInfo;
    }
}


public class OrderMasterConvert2OrderDto {

    public static OrderDto convert(OrderMaster orderMaster){
        OrderDto orderDto = new OrderDto();
        BeanUtils.copyProperties(orderMaster,orderDto);
        return orderDto;
    }

    public static List convert(List orderMasterList){
        return orderMasterList.stream().map(e -> convert(e)).collect(Collectors.toList());
    }
}

你可能感兴趣的:(springboot整合pageheler以及pageheler遇到的坑)