溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

JavaWeb分頁(yè)查詢功能怎么實(shí)現(xiàn)

發(fā)布時(shí)間:2022-09-26 14:23:38 來源:億速云 閱讀:139 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容主要講解“JavaWeb分頁(yè)查詢功能怎么實(shí)現(xiàn)”,感興趣的朋友不妨來看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“JavaWeb分頁(yè)查詢功能怎么實(shí)現(xiàn)”吧!

效果:

JavaWeb分頁(yè)查詢功能怎么實(shí)現(xiàn)

實(shí)現(xiàn):

分頁(yè)查詢有幾個(gè)比較重要的參數(shù),pageNum當(dāng)前的頁(yè)數(shù),pageSize每頁(yè)所展示的數(shù)據(jù),totalRecord總共有多少數(shù)據(jù),其他的數(shù)據(jù)可以根據(jù)這三個(gè)數(shù)據(jù)得出,有了這幾個(gè)數(shù)據(jù)就可以分頁(yè)查詢了

public class Page {    //當(dāng)前的頁(yè)數(shù)
    private int pageNum;    //每頁(yè)所展示的數(shù)據(jù)
    private int pageSize;    //總共有多少數(shù)據(jù)
    private int totalRecord;    //總頁(yè)數(shù)
    private int totalPage;    //查詢數(shù)據(jù)的起始位置
    private int startIndex;    //存放查詢到的數(shù)據(jù)
    private List<Book> booklist;    //相對(duì)的頭頁(yè)
    private int start;    //相對(duì)的尾頁(yè)
    private int end; 
    public Page(int pageNum, int pageSize, int totalRecord) {        this.pageNum = pageNum;        this.pageSize = pageSize;        this.totalRecord = totalRecord; 
        if(totalRecord%pageSize == 0){            //如果剛好能夠整除,則totalPage = totalRecord/pageSize
            this.totalPage = totalRecord/pageSize;
        }else{            //如歌不能整除,則需要多加一頁(yè)顯示余下的數(shù)據(jù)
            this.totalPage = (totalRecord/pageSize)+1;
        }        this.startIndex = (pageNum-1)*pageSize;        //初始頭頁(yè)
        this.start = 1;        //初始尾頁(yè)
        this.end = 5;        if(totalPage<5){            //如果總頁(yè)數(shù)小于5頁(yè),則尾頁(yè)為總頁(yè)數(shù)
            this.end = this.totalPage;
        }else{            this.start = this.pageNum-2;            this.end = this.pageNum+2;            if(this.start<0){                this.start = 1;                this.end = 5;
            }            if(this.end>totalPage){                this.end = totalPage;                this.start = this.end-5;
            }
        }
    } 
    //下面的為get/set方法
    public int getPageNum() {        return pageNum;
    } 
    public void setPageNum(int pageNum) {        this.pageNum = pageNum;
    } 
    public int getPageSize() {        return pageSize;
    } 
    public void setPageSize(int pageSize) {        this.pageSize = pageSize;
    } 
    public int getTotalRecord() {        return totalRecord;
    } 
    public void setTotalRecord(int totalRecord) {        this.totalRecord = totalRecord;
    } 
    public int getTotalPage() {        return totalPage;
    } 
    public void setTotalPage(int totalPage) {        this.totalPage = totalPage;
    } 
    public int getStartIndex() {        return startIndex;
    }    public void setStartIndex(int startIndex) {        this.startIndex = startIndex;
    } 
    public List<Book> getBooklist() {        return booklist;
    } 
    public void setBooklist(List<Book> booklist) {        this.booklist = booklist;
    } 
    public int getStart() {        return start;
    } 
    public void setStart(int start) {        this.start = start;
    } 
    public int getEnd() {        return end;
    } 
    public void setEnd(int end) {        this.end = end;
    }
}

pageNum從請(qǐng)求中獲得,pageSize自己設(shè)置,totalRecord通過mybatis查詢獲得,一開始登錄的時(shí)候請(qǐng)求中沒有pageNum,所以進(jìn)行一個(gè)判斷,如果沒有pageNum則賦值為1

 //pageNum 
 String pageNum = request.getParameter("pageNum") == null?"1":request.getParameter("pageNum");//pageSize 
   int pageSize = 7;//totalRecordint totalRecord = bookMapper.BookSize();//mybatis<select id="BookSize" resultType="int">        select count(*) from book
 </select>

有了pageNum和startIndex就可以查詢到特定范圍的數(shù)據(jù),通過使用mysql中的limit函數(shù),limit有兩個(gè)參數(shù),第一個(gè)參數(shù)為起始位置,即從那開始查,第二個(gè)參數(shù)為查詢范圍,即查多少個(gè),如 limit 14,7 為從第14個(gè)數(shù)據(jù)開始往后查七個(gè)。

<select id="Books" parameterType="java.util.Map" resultType="book">        select *from book  limit #{startIndex},#{pageSize}
    </select>
public static Page getPageUser(int pageNum,int pageSize){
        SqlSession sqlSession = GetSqlSession.getSqlSession();
        BookMapper bookMapper = sqlSession.getMapper(BookMapper.class);        int totalRecord = bookMapper.BookSize(); 
        Page page = new Page(pageNum,pageSize ,totalRecord );        int startIndex = page.getStartIndex();
        Map<String,Integer> map = new HashMap<>();        map.put("startIndex",startIndex );        map.put("pageSize",pageSize );
        page.setBooklist(bookMapper.Books(map));        return page;
    }

jsp頁(yè)面:

<a href="index?pageNum=1">首頁(yè)</a>
            <%--當(dāng)前頁(yè)數(shù)為第一頁(yè)--%>
        <c:if test="${sessionScope.page.pageNum == 1}">
            <c:forEach begin="${sessionScope.page.start}" end="${sessionScope.page.end}" step="1" var="i">
                <c:if test="${sessionScope.page.pageNum == i}">
                    <span  style="color: deeppink">${i}</span>
                </c:if>
                <c:if test="${sessionScope.page.pageNum != i}">
                    <a href="index?pageNum=${i}">${i}</a>
                </c:if>
            </c:forEach>
            <a href="index?pageNum=${sessionScope.page.pageNum+1}">下一頁(yè)</a>
        </c:if>
        <%--當(dāng)前頁(yè)數(shù)為中間頁(yè)數(shù)--%>
        <c:if test="${sessionScope.page.pageNum >1 && sessionScope.page.pageNum<sessionScope.page.totalPage}">
            <a href="index?pageNum=${sessionScope.page.pageNum-1}">上一頁(yè)</a>
            <c:forEach begin="${sessionScope.page.start}" end="${sessionScope.page.end}" step="1" var="i">
                <c:if test="${sessionScope.page.pageNum == i}">
                    <span  style="color: deeppink">${i}</span>
                </c:if>
                <c:if test="${sessionScope.page.pageNum != i}">
                    <a href="index?pageNum=${i}">${i}</a>
                </c:if>
            </c:forEach>
            <a href="index?pageNum=${sessionScope.page.pageNum+1}">下一頁(yè)</a>
        </c:if>
        <%--當(dāng)前頁(yè)數(shù)為最后一頁(yè)--%>
        <c:if test="${sessionScope.page.pageNum == sessionScope.page.totalPage}">
            <a href="index?pageNum=${sessionScope.page.pageNum-1}">上一頁(yè)</a>
            <c:forEach begin="${sessionScope.page.start}" end="${sessionScope.page.end}" step="1" var="i">
                <c:if test="${sessionScope.page.pageNum == i}">
                    <span  style="color: deeppink">${i}</span>
                </c:if>
                <c:if test="${sessionScope.page.pageNum != i}">
                    <a href="index?pageNum=${i}">${i}</a>
                </c:if>
            </c:forEach>
        </c:if>
        <a href="index?pageNum=${sessionScope.page.totalPage}">尾頁(yè)</a>

到此,相信大家對(duì)“JavaWeb分頁(yè)查詢功能怎么實(shí)現(xiàn)”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI