PageHelper插件,报错“java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.github.page”

分页插件版本如下:


   com.github.pagehelper
   pagehelper
   5.3.0

分页代码如下:

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service
public class ComplaintServiceImpl implements ComplaintService{

    @Override
    public BasePageData getByMap(Integer pageNum, Integer pageSize, Map map) {

        if (pageNum == null) {
            pageNum = 0;
        }
        if (pageSize == null) {
            pageSize = 10;
        }

        // 核心分页代码
        PageHelper.startPage(pageNum, pageSize);
        List companyComplaints = companyComplaintMapper.getByMap(map);
        
        BasePageData basePageData = new BasePageData();
        basePageData.setResults(companyComplaints);
        int total = (int) ((Page) companyComplaints).getTotal();
        basePageData.setPageSize(pageSize);
        basePageData.setPageNum(pageNum);
        basePageData.setTotalCount(total);
        return basePageData;
    }
}

高版本分页插件不支持强转,换一种方式即可:

    @Override
    public BasePageData getByMap(Integer pageNum, Integer pageSize, Map map) {

        if (pageNum == null) {
            pageNum = 0;
        }
        if (pageSize == null) {
            pageSize = 10;
        }
        BasePageData basePageData = new BasePageData();
        // 核心分页代码
        PageHelper.startPage(pageNum, pageSize);
        List companyComplaints = companyComplaintMapper.getByMap(map);
        // 创建一个PageInfo对象
        PageInfo pageInfo = new PageInfo<>(companyComplaints);
        int total = (int) pageInfo.getTotal();
        basePageData.setResults(companyComplaints);
        basePageData.setPageSize(pageSize);
        basePageData.setPageNum(pageNum);
        basePageData.setTotalCount(total);
        return basePageData;
    }

你可能感兴趣的:(java,github,开发语言)