1. 程式人生 > >如何實現從資料庫中取出的列表資料進行分頁功能?

如何實現從資料庫中取出的列表資料進行分頁功能?

一、你需要先建立一個bean物件(也就是page物件)

  1. 創建出你需要的屬性比如在我的專案中我需要用到的:有當前的頁碼currentPage
  2. 當前的頁顯示的數量currentCount
  3. 你從資料庫中查詢得到的所有資料總和totalCount(這個你需要在dao層定義方法實現並把得到的資料返回給service層)
  4. 一共需要分多少頁totalPage(這個需要你去計算通過查詢所有的資料總和/currentCount,還用到了math.ceil()方法)
  5. 最後生成setter和getter方法

二、在servlet層去呼叫service層實現已經寫好的方法

   public void
getCategoryList(HttpServletRequest request, HttpServletResponse response) throws SQLException, ServletException, IOException { //為了是實現分頁功能,這裡需要從前端頁面得到兩個引數currentPage,currentCount 然後轉為int型別 int currentPage = Integer.parseInt(request.getParameter("currentPage")); int currentCount = Integer.parseInt(request.getParameter("currentCount"
)); //最後如果網頁第一次沒有給我們傳入引數值的時候,我們需要自己設定預設值 if (currentPage==0){ currentPage=1; } if (currentCount==0){ currentCount=10; } // 1 呼叫service中的查詢方法 CategoryService service = new CategoryService(); Page page= service.findPageCategory(currentPage,currentCount);//這個地方返回的也是一個page類
if (page!=null) { //這個部分肯定有問題 request.setAttribute("page",page); request.getRequestDispatcher("/category-list.jsp").forward(request,response); }else { request.getRequestDispatcher("/category-list.jsp").forward(request,response); } }
  1. 先建立一個service層的物件
  2. 然後在service層去建立一個方法 findPageCategory,這個方法就是為了獲取到跟分頁有關的所有的屬性(也就是totalPage,totalCount)

(1)如何得到totalPage,totalCount這兩個引數呢?(service層)

  1. 需要先建立一個dao層的categoryDao物件來從資料庫中取出資料
  2. totalCount可以直接得到結果,而totalPage需要計算(用totalCount/currentCount)
  3. 把所得到的四個page屬性都設定給page實體類(用setAttribute方法)
  4. 到這還需要重新定義一個startPosition(也就是在後面用SQL語句的limit中需要用到的那個引數,每次查詢的起始位置)
  5. 規律就是規律就是:每次查詢的起始位置=(當前的的頁面-1)*每頁固定顯示的資料
public Page findPageCategory(int currentPage,int currentCount ) throws SQLException {

        Page page = new Page();

        CategoryDao dao = new CategoryDao();
        int totalCount = dao.queryCount();
        /*也是為了根據總共資料的總數除以 當前顯示的總頁數,來得到一共的頁碼數
這裡用到了一個math函式的一個方法,就是隻要這個數是小數,就要讓這個數往上加一,如1.2  就是2  0.8  就是1
        *   總數   每頁顯示的數目   總頁數
        *   9           10        0.9      1
        *   14          10        1.4      2
        */
        //要保證ceil跟的是一個double型別
        int  totalPage = (int) Math.ceil(1.0*totalCount / currentCount);
        //然後使用page的實體類,去把每一個屬性設定進去
            page.setCurrentCount(currentCount);
            page.setCurrentPage(currentPage);
            page.setTotalCount(totalCount);
            page.setTotalPage(totalPage);


        /*
        * 這裡使用到了一個SELECT  * FROM category LIMIT 1,3(1表示開始查詢的位置從0開始,3表示要查詢多少條資料)
        *   頁數          每頁顯示的資料         查詢的起始位置
        *   1               10                      0
        *   2                10                     10
        *   3               10                      20
        *   規律就是(當前的的頁面-1)*每頁固定顯示的資料 = 每次查詢的起始位置
        *   (currentPage-1)*currentCount = 起始位置
        * */
        //計算出每次查詢的起始位置
        int startPosition = (currentPage-1)*currentCount;
        //分頁查詢資料,拿到DAO層的返回生鮮種類的集合,在service層進行呼叫,返回的也是一個生鮮的種類集合
        List<Category> categories = dao.queryPageCategoryList(startPosition, currentCount);
        //把集合封裝到page類中
        page.setList(categories);

        return page;
    }

三、在Dao層去實現兩個方法

  1. 是查詢資料庫中的所有資料總和
public int queryCount() throws SQLException {

        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        QueryRunner queryRunner=new QueryRunner(dataSource);
        String sql = "select count(*) category ";
        //如果使用聚合函式的話,就必須new出來一個 ScalarHandler<>()來儲存資料,最後返回的還是一個Long型別的值
        Long query = queryRunner.query(sql, new ScalarHandler<>());
        //還需要把Long型別轉為int型別
        return query.intValue();
    }
  1. 分頁查詢資料(也就是使用SQL中聚合語句limit去查詢資料limit資料後面的第一個資料是查詢的起始位置,第二個引數表示每個分頁查詢多少條資料)
  public List<Category> queryPageCategoryList(int startPosion, int currentCount) throws SQLException {

        ComboPooledDataSource dataSource= new ComboPooledDataSource();
        QueryRunner queryRunner= new QueryRunner(dataSource);
        String sql = "select * from category limit ?,?";
        //如果用到query的話 就需要把資料給封裝到BeanListHandler中,返回的是一個生鮮種類的集合
        //如果是SQL語句中需要引數的話,先用問號代替,然後在query方法中的新建的那個BeanListHandler後面加上引數
        List<Category> categoryList = queryRunner.query(sql, new BeanListHandler<Category>(Category.class),startPosion,currentCount);
        return categoryList;
    }