用SQL_CALC_FOUND_ROWS 和 FOUND_ROWS实现分页(附上手动分页)

一、我们在工作很多时候需要使用手动分页,这里写出SQL_CALC_FOUND_ROWS 和 FOUND_ROWS 在工作具体使用场景,做一个参考
(1)定义一个pageInfo

public class PageInfo {
    private Collection data;
    private int pageSize;
    private int totalNum;
    private int totalPageNum;
    private int currentPage;

    public PageInfo() {
    }

    public PageInfo(int pageSize) {
        this.pageSize = pageSize;
        this.totalNum = 0;
        this.totalPageNum = 0;
        this.currentPage = 0;
    }

    public PageInfo(Collection data, int totalNum) {
        this.data = data;
        this.totalNum = totalNum;
    }

    public PageInfo(Collection data, int pageSize, int totalNum, int totalPageNum, int currentPage) {
        this.data = data;
        this.pageSize = pageSize;
        this.totalNum = totalNum;
        this.totalPageNum = totalPageNum;
        this.currentPage = currentPage;
    }

    public PageInfo(Collection data, int pageSize, int totalNum, int currentPage) {
        this.data = data;
        this.pageSize = pageSize;
        this.totalNum = totalNum;
        this.currentPage = currentPage;
        this.totalPageNum=counttotalPage(totalNum, pageSize);
    }

    public int counttotalPage(int tnum, int psize) {
        int totalNumber = tnum < 0 ? 0 : tnum;
        pageSize = psize < 1 ? 1 : psize;
        int totalPage = totalNumber / psize;
        int surplus = totalNumber % psize;
        if (surplus > 0) {
            ++totalPage;
        }
        return totalPage;
    }

    public  PageInfo getPageData(List list ){
        PageInfo dataTemp = new PageInfo<>();
        dataTemp.setData(list);
        dataTemp.setTotalNum(this.totalNum);
        dataTemp.setPageSize(this.pageSize);
        dataTemp.setCurrentPage(this.currentPage);
        dataTemp.setTotalPageNum(this.totalPageNum);
        return dataTemp;
    }

    public Collection getData() {
        return data;
    }

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

    public int getPageSize() {
        return pageSize;
    }

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

    public int getTotalNum() {
        return totalNum;
    }

    public void setTotalNum(int totalNum) {
        this.totalNum = totalNum;
    }

    public int getTotalPageNum() {
        return totalPageNum;
    }

    public void setTotalPageNum(int totalPageNum) {
        this.totalPageNum = totalPageNum;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }
    
}

(2) 定义一个请求参数

public class RequestVO {
    private Integer pageNum;
    private Integer pageSize;
    private Integer startNo;

    public Integer getPageNum() {
        return pageNum;
    }

    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }

    public Integer getPageSize() {
        return pageSize;
    }

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

    public Integer getStartNo() {
        return startNo;
    }

    public void setStartNo(Integer startNo) {
        this.startNo = startNo;
    }
}

(3) 这里将使用sql分页和手动分页写出方便以后参考

@Service
public class OrderServiceImpl implements OrderService {

    private static final Integer PAGE_SIZE = 10;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private OrderMapper orderMapper;
    @Override
    public PageInfo getOrderList(RequestVO requestVO) {
        changeRequest(requestVO);
        return orderDao.getOrderList(requestVO);

    }
	//转化请求参数
    private void changeRequest(RequestVO requestVO){
        Integer pageSize = requestVO.getPageSize();
        pageSize = pageSize == null ? PAGE_SIZE : pageSize;
        Integer pageNum = requestVO.getPageNum();
        int startNo = (pageNum == null || pageNum < 1) ? 0 : (pageNum - 1) * pageSize;
        requestVO.setStartNo(startNo);
        requestVO.setPageSize(pageSize);
    }
	
	
    /**
     * 用于手动分页
     * @param list
     * @param pageSize
     * @param currentPage
     * @return
     */
    private PageInfo getPageInfo(List list,Integer pageSize,Integer currentPage){
        int size = list.size();
        pageSize = (pageSize == null || pageSize < 1) ? PAGE_SIZE : pageSize;
        currentPage = (currentPage == null || currentPage < 1) ? 1 : currentPage;
        int endSub = currentPage * pageSize > size ? size : currentPage * pageSize;
        int startSub = (currentPage - 1) * pageSize > size ? size:(currentPage - 1) * pageSize ;
        List resList = list.subList(startSub, endSub);
        return new PageInfo<>(resList, pageSize, size, currentPage);
    }
 

}

(4) 定义dao类的实现,这里使用 SQL_CALC_FOUND_ROWS 和 FOUND_ROWS
在使用方法上面 一定需要打上@Transcational

@Component
public class OrderDaoImpl implements OrderDao {
    @Autowired
    private OrderMapper orderMapper;
    @Override
    @Transactional
    public PageInfo getOrderList(RequestVO requestVO) {
        List orderList = orderMapper.getOrderListLimt(requestVO);
        Integer totalNum=orderMapper.getSameSession();
        return new PageInfo<>(orderList,requestVO.getPageSize(),totalNum,requestVO.getPageNum());
    }
}

(5)最后就是xml文件使用,这里使用 FOUND_ROWS 在复杂sql可以降低运行时间

 

    

你可能感兴趣的