Pagehelper 在count时过滤order by,导致返回total不正确

  • 自定义Page

import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

public class Page {
    private List list;
    private long total;
    private int pageNum;
    private int pageSize;

    public static void startPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
    }

    public static  Page from(List data) {
        PageInfo page = new com.github.pagehelper.PageInfo(data);
        Page ret = new Page<>();
        ret.list = data;
        ret.total = page.getTotal();
        ret.pageNum = page.getPageNum();
        ret.pageSize = page.getPageSize();
        return ret;
    }

    public Page() {
    }

    public Page(List list) {
        this.list = list;
    }

    public Page(List list, int pageNum, int pageSize, long total) {
        this.list = list;
        this.total = total;
        this.pageNum = pageNum;
        this.pageSize = pageSize;
    }

    public List getList() {
        return list;
    }

    public long getTotal() {
        return total;
    }

    public int getPageNum() {
        return pageNum;
    }

    public int getPageSize() {
        return pageSize;
    }
}

 

  •  创建Model: RiskFundLog
  • 创建Vo: RiskFunkLogVo
  • Service如下
  @Override
    public Page getRiskLog(int pageNum, int pageSize) {
        Page.startPage(pageNum, pageSize);
        List pageList = riskFundLogDao.search(new RiskFundLog());

        try {
            if (CollectionUtils.isNotEmpty(pageList)) {
                List ret = new ArrayList<>(pageList.size());
                for (RiskFundLog item : pageList) {
                    RiskFundLogVo vo = new RiskFundLogVo();
                    BeanUtils.copyProperties(vo, item);

                    ret.add(vo);
                }

                com.github.pagehelper.Page page = (com.github.pagehelper.Page)pageList;
                return new Page(ret, page.getPageNum(), page.getPageSize(), page.getTotal());
            }
        } catch (Exception e) {
            logger.error("RiskFundLogServiceImpl#getRiskLog exception occurred:", e);
        }

        List emptyList = new ArrayList<>(1);
        RiskFundLogVo emptyObj = new RiskFundLogVo();
        emptyList.add(emptyObj);
        return new Page<>(emptyList);
    }

 

你可能感兴趣的:(mysql,java,SpringCloud,spring,PageHelper)