mybatis实现分页查询的功能

基本的步骤就是

(1)查询总共有多少条

(2)分页查询,当前页,一页查多少,一共多少页

(3)外围需要循环调用,获取所有页的数据,或者分页展示

首先写一个分页的基础类

public class Pagination {

    /**
     * 总条数
     */
    private int totalCount;
    /**
     * 页面
     */
    private int pageNo;
    /**
     * 每页条数
     */
    private int pageSize;
    /**
     * 总页数
     */
    private int totalPage;

    /**
     * 列表
     */
    private List list;

    /**
     * 默认每页条数
     */
    private final static int DEFAULT_PAGESIZE = 20;

    /**
     *
     */
    public Pagination() {

    }

    /**
     * 分页
     *
     * @param pageNo
     * @param pageSize
     * @param totalCount
     */
    public Pagination(int pageNo, int pageSize, int totalCount) {
        if (pageNo <= 0) {
            this.pageNo = 1;
        } else {
            this.pageNo = pageNo;
        }

        if (pageSize <= 0) {
            this.pageSize = DEFAULT_PAGESIZE;
        } else {
            this.pageSize = pageSize;
        }

        if (totalCount < 0) {
            this.totalCount = 0;
        } else {
            this.totalCount = totalCount;
        }

        totalPage = (this.totalCount % this.pageSize == 0) ?
                this.totalCount / this.pageSize :
                this.totalCount / this.pageSize + 1;
    }

    /**
     * 分页
     *
     * @param pageNo
     * @param pageSize
     * @param totalCount
     * @param list
     */
    public Pagination(int pageNo, int pageSize, int totalCount, List list) {
        this(pageNo, pageSize, totalCount);
        this.list = list;
    }

    /**
     * get totalCount
     *
     * @return
     */
    public int getTotalCount() {
        return totalCount;
    }

    /**
     * set totalCount
     *
     * @param totalCount
     */
    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    /**
     * get pageNo
     *
     * @return
     */
    public int getPageNo() {
        return pageNo;
    }

    /**
     * set pageNo
     *
     * @param pageNo
     */
    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }

    /**
     * get pageSize
     *
     * @return
     */
    public int getPageSize() {
        return pageSize;
    }

    /**
     * set pageSize
     *
     * @param pageSize
     */
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    /**
     * get totalPage
     *
     * @return
     */
    public int getTotalPage() {
        return totalPage;
    }

    /**
     * set totalPage
     *
     * @param totalPage
     */
    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    /**
     * get list
     *
     * @return
     */
    public List getList() {
        return list;
    }

    /**
     * set list
     *
     * @param list
     */
    public void setList(List list) {
        this.list = list;
    }

    /**
     * 获取前一页
     *
     * @return
     */
    public int getPrevPage() {
        int prevPage = 0;
        if (pageNo <= 1) {
            prevPage = 1;
        } else {
            prevPage = pageNo - 1;
        }

        return prevPage;
    }

    /**
     * 获取下一页
     *
     * @return
     */
    public int getNextPage() {
        int nextPage = 0;
        if (pageNo < totalPage) {
            nextPage = pageNo + 1;
        } else {
            nextPage = totalPage;
        }
        return nextPage;
    }

    /**
     * 判断是否有下一页
     *
     * @return
     */
    public boolean hasNextPage() {
        return pageNo < totalPage;
    }

    /**
     * 列表是否为空
     *
     * @return
     */
    public boolean isEmptyForList() {
        return list == null || list.isEmpty();
    }

针对接口的入参数

public class  Request {

    /**
     * 分页大小,默认值20
     */
    private int pageSize = 20;

    /**
     * 分页起始,默认值 1
     */
    private int pageNo = 1;

   }
//查询总数

        
            
        
        
            
        
        
            
        
    
//分页查询    

        
            
            
            
        
        
            select id,
            table_name,
            from desk
        
        
            
        
    
分页查询 
public Pagination queryTableListByShopId(
            String shopId,int pageNo,int pageSize) {
        int startRow = (pageNo - 1) * pageSize;
        int count = (int)queryTableInfoCountByShopId(shopId);
        List
tableInfoList = new ArrayList<>(); if (count > 0) { List tableInfoModelList =queryTableInfoListByPage(shopId, startRow, pageSize); if (!CollectionUtils.isEmpty(tableInfoModelList)) { tableInfoList = tableInfoModelList.stream().map((s) -> { return convert(s); }).collect(Collectors.toList()); } } Pagination
answerPagination = new Pagination<>(pageNo, pageSize, count, tableInfoList); return answerPagination; }

 

你可能感兴趣的:(java)