springMVC手寫分頁查詢


package com.neuedu.java7.baen;

public class Page {

    private int currentPage = 1;    //當前頁數
    private int totalPages;        //總頁數
    private int total;            //記錄總行數
    private int pageSize = 5;    //每頁記錄行數
    private int nextPage;        //下一頁
    private int prefPage;        //前一頁
    
    public Page(){
    }
    
    public Page(int currentPage, int pageSize) {
        this.currentPage = currentPage;
        this.pageSize = pageSize;
    }
    
    public int getCurrentPage() {
        return currentPage;
    }
    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }
    public int getTotalPages() {
        totalPages = total%pageSize == 0?total/pageSize:total/pageSize+1;
        return totalPages;
    }
    public int getTotal() {
        return total;
    }
    public void setTotal(int total) {
        this.total = total;
    }
    public int getPageSize() {
        return pageSize;
    }
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
    public int getNextPage() {
        if(currentPage<totalPages){
            nextPage = currentPage+1;
        }else{
            nextPage = currentPage;
        }
        return nextPage;
    }
    public int getPrefPage() {
        if(currentPage>1){
            prefPage = currentPage-1;
        }else{
            prefPage = currentPage;
        }
        return prefPage;
    }    
    
}

//寫方法
    /**
     * 分頁查詢
     * 
     * @param pagger
     * @return
     */
    public List<Category> findList(Page page);

    /**
     * 查詢記錄總數
     * 
     * @return
     */
    public int findTotal();

//寫Mapper方法
    <select id="findTotal" resultType="int">
        select count(t.id) t from
        category t
    </select>

    <select id="findList" parameterType="com.neuedu.java7.baen.Page"
        resultMap="CategoryMap">
        select * from (select rownum rn, id,cname from category
        where rownum &lt;= #{currentPage}*#{pageSize}) where rn &gt;(#{currentPage}-1)*#{pageSize}
    </select>



//寫Action控制
@RequestMapping("/doAll")
    public String findAlls(HttpServletRequest request) {    
        
        Page page  = new Page();        
        int count = categoryService.findTotal();
         page.setTotal(count);
        
         request.getSession().setAttribute("page", page);
        List<Category> category = categoryService.findList(page);        
        request.getSession().setAttribute("category", category);
        return "list";
    }

    @RequestMapping("/page")
    public String page(Integer pageSize,Integer currentPage,HttpServletRequest request){
        Page page = null;
        if (request.getSession().getAttribute("page") !=null) {
            page = (Page) request.getSession().getAttribute("page");
        }else {
            page = new Page();
        }
        if (pageSize!=null) {
            page.setPageSize(pageSize);
        }
        if (currentPage!=null) {
            page.setCurrentPage(currentPage);
        }        
        List<Category> category = categoryService.findList(page);
        request.setAttribute("category", category);
        
        return "list";
    }
    
//寫頁面編碼
<tr>
    <td align="left" colspan="2">每頁顯示<select name="pageSize" id="pageSize" style="background-color: #FFFF33" onchange="toPage()">
                <option value="5" ${page.pageSize==5?"selected='selected'":"" }>5</option>
                <option value="10" ${page.pageSize==10?"selected='selected'":"" }>10</option>
                <option value="20" ${page.pageSize==20?"selected='selected'":"" }>20</option>
            </select>條</td>
    
    
    <td align="right" colspan="3"><a href="../category/page.do?currentPage=1">[首頁]</a>
    <a href="../category/page.do?currentPage=${page.prefPage }">前一頁</a>
    <a href="../category/page.do?currentPage=${page.nextPage }">后一頁</a>
    <a href="../category/page.do?currentPage=${page.totalPages }">[尾頁]</a></td>
    </td>
    </tr>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM