码迷,mamicode.com
首页 > 编程语言 > 详细

springMVC手写分页查询

时间:2016-11-08 19:43:52      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:action   map   selected   getattr   http   this   ted   rownum   att   

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>

 

springMVC手写分页查询

标签:action   map   selected   getattr   http   this   ted   rownum   att   

原文地址:http://www.cnblogs.com/bingo584235807/p/6044166.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!