分頁查詢的總結

最近剛把前端的基礎學完,做了一個簡單的客戶管理系統鞏固一下前面的基礎知識,設計的知識之前已有各種小結,再次主要是總結一下分頁查詢相關的知識。

1.首先需要頂一個兩個實體類 Customer和Pagenation用來封裝客戶信息,和分頁查詢的信息;

主體思想還是jsp前段頁面提交數據到web層servlet,servlet對數據做簡單的判斷和封裝,再調用業務層相關的方法進行數據查詢,具體調用業務層的哪些方法,需要根據實際情況需要。
關於實體類的封裝,customer實體類主要是封裝一些客戶的信息,比如姓名,年齡,種類等等;JDBCUtil可以根據相應的setXXX()方法,把數據往裏面注入;

public class Customer {

    private String id;// id

    private String name;// 姓名

    private String gender;// 性別

    private String birthday;// 生日

    private String phone;// 電話

    private String email;// 郵箱 非空不可以重複

    private String hobby;// 愛好

    private String type;// 客戶類型

    private String description;// 客戶描述
}

注意:對於多選框到後臺的是一個字符數組,jdbcutil默認只會封裝第一元素,所以需要我們手動

Pagenation類的封裝是封裝了分頁查詢需要的信息,同時把每頁需要顯示的數據加入一個集合中,把集合本身當做一個屬性定義;在pageNation中根據已知的一些參數可以直接算出一些相關的參數,在前段做了封裝,這樣到業務層就會變得非常簡單方便。

說明:最根本的是調用mysql中的分頁查詢語句:

select * from customers limit ? ,?;
第一個問號:起始頁 pageNum
第二個問號:每頁的長度pageSize
前面的工作都是爲了把這兩個參數簡單合理的傳遞到dao層,數據庫的查詢不是一次查出所有數據的,是根據jsp頁面每次提交的pageNum再去查詢,查詢後pageNation裏面的屬性也不是全部都注入值得,一些可以通過計算得到的值,在jsp顯示頁面調用的時候纔會去計算。

<a href="javascript:void(0)" onclick="go(${pageNation.beforePage})">上一頁</a>

<a href="javascript:void(0)" onclick="go(${pageNation.nextPage})">下一頁</a>

<a href="javascript:void(0)" onclick="go(${pageNation.totalPages})">尾頁</a>

通過jsp傳遞參數pageNum,到後臺處理

function go(pageNum){

    /* //非數字 負數,但是小數搞不定
    if(isNaN(pageNum)||parseInt(pageNum)<=0){

    } */

    if(parseInt(pageNum)!=pageNum||parseInt(pageNum<=0)){
        alert("請輸入有效的數值");

        return ;
    }
    //如果輸入的數值超出最大頁碼 js去獲取request裏面的數據
    //在這裏不可以用el表達式,那比較麻煩
    //javaScript使用le表達式獲取數據,加個雙引號就可以了,別的不可以直接拿
    if(parseInt(pageNum)>parseInt("${pageNation.totalPages}")){

        alert("超出最大頁碼"+parseInt("${pageNation.totalPages}"));
        //document.getElementById("num").value="${pageNation.totalPages}";//最大頁碼
        //document.getElementById("num").value="${pageNation.pageNum}";//當前頁碼
        return ;
    }


    location.href="${pageContext.request.contextPath}/listAllCustomerServlet?pageNum="+pageNum;

}

如何在JSP代碼中利用el表達式,加雙引號不加是不可以用的!!!

alert("超出最大頁碼"+parseInt("${pageNation.totalPages}"));
        //document.getElementById("num").value="${pageNation.totalPages}";//最大頁碼
        //document.getElementById("num").value="${pageNation.pageNum}";//當前頁碼
public class Pagenation {

    private int pageNum = 1;// 當前頁碼,默認從第一頁開始

    private int pageSize = 10;// 每頁顯示的數目,默認是10,模仿百度

    private int totalPages;// 一共多少頁

    private int beforePage;// 上一頁

    private int nextPage;// 下一頁

    private int pageBar[];// 每次查詢分頁欄顯示的頁碼

    private List<Customer> customers;// 每頁查詢的分頁記錄的數據

    private long totalCount;// 總工多少條數據//來自數據查詢,業務層封裝

    private int startIndex;// 起始頁

    public int getStartIndex() {

        this.startIndex = (this.pageNum - 1) * this.pageSize;

        return this.startIndex;
    }

    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 getTotalPages() {

        this.totalPages = ((int) this.totalCount + this.pageSize - 1) / this.pageSize;
        return totalPages;
    }

    /**
     * 上一頁
     * 
     * 
     * @return
     */
    public int getBeforePage() {

        this.beforePage = this.pageNum - 1;

        // 如果當前頁已經是第一頁,那麼上一頁也是第一頁本身
        if (this.pageNum == 1) {
            this.beforePage = 1;
        }
        return this.beforePage;
    }

    /**
     * 下一頁
     * 
     * @return
     */
    public int getNextPage() {

        this.nextPage = this.pageNum + 1;

        // 如果當前頁已經是最後一頁,那麼下一頁就是它本身
        if (this.nextPage >= this.totalPages) {
            this.nextPage = this.totalPages;
        }

        return this.nextPage;
    }

    public int[] getPageBar() {

        int beginPage;// 起始頁碼

        int endPage;// 結束頁碼

        // 如果總頁數少於10頁
        if (this.totalPages <= 10) {
            beginPage = 1;
            endPage = this.totalPages;
        } else {

            beginPage = this.pageNum - 5;
            endPage = this.pageNum + 4;

        }

        // 如果起始頁小於0
        if (beginPage <= 0) {

            beginPage = 1;
            endPage = beginPage + 9;
        }

        // 如果結束頁 大於總頁數
        if (endPage >= this.totalPages) {

            endPage = this.totalPages;
            beginPage = this.totalPages - 9;
        }

        this.pageBar = new int[endPage - beginPage + 1];
        int index = 0;
        for (int i = beginPage; i <= endPage; i++) {

            this.pageBar[index++] = i;
        }

        return this.pageBar;
    }

    public List<Customer> getCustomers() {
        return customers;
    }

    public void setCustomers(List<Customer> customers) {
        this.customers = customers;
    }

    public long getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(long totalCount) {
        this.totalCount = totalCount;
    }

}

業務層的方法解析:業務層知識查詢了總的記錄數,和分頁查詢,其他的都是進行計算的!!!

public void pageQueryCustomers(Pagenation pageNation) {
//查詢所有的數據條數       pageNation.setTotalCount(customerDao.getTotalCounts());
// select * from Customers limit ?,? ;
//pageNation.setCustomers(customerDao.pageQuery(pageNation.getPageNum(),
// pageNation.getPageSize()));
//根據當前頁碼,和每頁大小進行分頁查詢          pageNation.setCustomers(customerDao.pageQuery(pageNation.getStartIndex(), pageNation.getPageSize()));
    }

關於頁碼最後的跳轉框
1.默認情況下是顯示當前的頁碼
2.當有輸入的時候根據輸入的頁碼傳遞給後臺進行查詢

<input type="text" value="${ pageNation.pageNum}" id="num" style="width: 30px ">
        <input type="button" value="確認" onclick="go(document.getElementById('num').value);">
function go(pageNum){

    /* //非數字 負數,但是小數搞不定
    if(isNaN(pageNum)||parseInt(pageNum)<=0){

    } */

    if(parseInt(pageNum)!=pageNum||parseInt(pageNum<=0)){
        alert("請輸入有效的數值");

        return ;
    }
    //如果輸入的數值超出最大頁碼 js去獲取request裏面的數據
    //在這裏不可以用el表達式,那比較麻煩
    //javaScript使用le表達式獲取數據,加個雙引號就可以了,別的不可以直接拿
    if(parseInt(pageNum)>parseInt("${pageNation.totalPages}")){

        alert("超出最大頁碼"+parseInt("${pageNation.totalPages}"));
        //document.getElementById("num").value="${pageNation.totalPages}";//最大頁碼
        //document.getElementById("num").value="${pageNation.pageNum}";//當前頁碼
        return ;
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章