1. 程式人生 > >mybatis實現分頁查詢的功能

mybatis實現分頁查詢的功能

基本的步驟就是

(1)查詢總共有多少條

(2)分頁查詢,當前頁,一頁查多少,一共多少頁

(3)外圍需要迴圈呼叫,獲取所有頁的資料,或者分頁展示

首先寫一個分頁的基礎類

public class Pagination<T> {

    /**
     * 總條數
     */
    private int totalCount;
    /**
     * 頁面
     */
    private int pageNo;
    /**
     * 每頁條數
     */
    private int pageSize;
    /**
     * 總頁數
     */
    private int totalPage;

    /**
     * 列表
     */
    private List<T> 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<T> 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<T> getList() {
        return list;
    }

    /**
     * set list
     *
     * @param list
     */
    public void setList(List<T> 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;

   }
//查詢總數
<operation name="queryTableInfoCount" multiplicity="one">
        <extraparams>
            <param name="shopId" javatype="java.lang.String"/>
        </extraparams>
        <sql>
            <![CDATA[
            select count(*) from desk
            ]]>
        </sql>
        <sqlmap>
            <![CDATA[
			 select count(*) from desk
             where shop_id = #shopId#
			]]>
        </sqlmap>
    </operation>
//分頁查詢    
<operation name="queryTableInfoListByPage" multiplicity="many">
        <extraparams>
            <param name="shopId" javatype="java.lang.String"/>
            <param name="startRow" javatype="java.lang.Integer"/>
            <param name="endRow" javatype="java.lang.Integer"/>
        </extraparams>
        <sql>
            select id,
            table_name,
            from desk
        </sql>
        <sqlmap>
            <![CDATA[
			 select id,
             table_name
            from kbdesk_info
            where shop_id=#shopId#
            limit #startRow#,#endRow#
			]]>
        </sqlmap>
    </operation>
分頁查詢 
public Pagination<Table> queryTableListByShopId(
            String shopId,int pageNo,int pageSize) {
        int startRow = (pageNo - 1) * pageSize;
        int count = (int)queryTableInfoCountByShopId(shopId);
        List<Table> tableInfoList = new ArrayList<>();
        if (count > 0) {
            List<TableInfo> tableInfoModelList =queryTableInfoListByPage(shopId, startRow, pageSize);
            if (!CollectionUtils.isEmpty(tableInfoModelList)) {
                tableInfoList = tableInfoModelList.stream().map((s) -> {
                    return convert(s);
                }).collect(Collectors.toList());
            }
        }
        Pagination<Table> answerPagination = new Pagination<>(pageNo, pageSize,
                count, tableInfoList);

        return answerPagination;
    }