手動實現SpringMVC Mybatis 數據真分頁

一、思路

  • 返回Map 包含返回的數據 、 分頁的HTML代碼
  • 真分頁使用SQL語句的LIMIT 來規定取數據的範圍
  • 傳入 當前頁碼 和 頁面數據大小(如10條分一次頁)
  • 取數據的開始值爲 (當前頁面-1)*頁面大小

service

public Map<String, Object> getPageList(int currentPage, int pageSize) {
        //數據列表
        Map<String,Object> map = new HashMap<>();
        int page = (currentPage-1)*pageSize; //獲取數據開始值
        List<Product> pdList = productMapper.findPageList( page, pageSize);//從寫好的Mapper獲取數據
        map.put("list",pdList);//添加進Map

        //分頁信息
        StringBuffer html = new StringBuffer();
        int listCount = productMapper.pdCount();//獲取數據數量
        int pageCount = listCount/pageSize;//計算頁面數量
        if(listCount%pageSize != 0){
            pageCount += 1;
        }
        //拼寫HTML(layUi)
        html.append("<li class=\"disabled\"><span>"+currentPage+" / "+pageCount+"</span></li>");
        if(currentPage > 1){
            html.append("<li><a href=\"?page=1\">«</a></li>");
            html.append("<li><a href=\"?page="+(currentPage-1)+"\">‹</a></li>");
        }
        if(pageCount <= 10){
            for(int i = 1;i <= pageCount;i++){
                if(i == currentPage){
                    html.append("<li class=\"active\"><span>"+i+"</span></li>");
                }else{
                    html.append("<li><a href=\"?page="+i+"\">"+i+"</a></li>");
                }
            }
        }else{
            if(currentPage >= 10){
                if(currentPage+5 <= pageCount){
                    for(int i = currentPage-4;i <= currentPage+5;i++){
                        if(i == currentPage){
                            html.append("<li class=\"active\"><span>"+i+"</span></li>");
                        }else{
                            html.append("<li><a href=\"?page="+i+"\">"+i+"</a></li>");
                        }
                    }
                }else{
                    for(int i = currentPage-4;i <= pageCount;i++){
                        if(i == currentPage){
                            html.append("<li class=\"active\"><span>"+i+"</span></li>");
                        }else{
                            html.append("<li><a href=\"?page="+i+"\">"+i+"</a></li>");
                        }
                    }
                }
            }else{
                for(int i = 1;i <= 10;i++){
                    if(i == currentPage){
                        html.append("<li class=\"active\"><span>"+i+"</span></li>");
                    }else{
                        html.append("<li><a href=\"?page="+i+"\">"+i+"</a></li>");
                    }
                }
            }
        }
        if(currentPage < pageCount){
            html.append("<li><a href=\"?page="+(currentPage+1)+"\">›</a></li>");
            html.append("<li><a href=\"?page="+pageCount+"\" >»</a></li>");
        }

        map.put("page",html.toString());//放入Map


        return map;
    }

Mybatis SQL (productMapper.findPageList())

		select *
        from s_product
        ORDER BY pd_id DESC
        LIMIT #{page},#{pageSize}
        //起始位置   頁面大小
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章