PageUtil 分页

思路:首先用的是Mysql,主要分页是limit函数,limit第一个参数是从第几条开始(Mysql默认是从0开始),第二个是固定的,每页显示的条数。

PageUtil

属性:pageCount 每页显示的条数 、resultCount 一共有多少条、currentPage 当前页、totalPage 一共有多少页

方法:getCurrent(int currentPage,int pageCount)  用来计算limit的第一个参数,用(当前页数-1)*每页显示的条数

           getFooter(int resultCount,int pageCount,int currentPage)  用来显示分页的页脚,控制上一页下一页的显示

package com.liyi.test.util;


public class PageUtil {
    /**每页显示的条数**/
    private int pageCount;
    /**总共的条数**/
    private int resultCount;
    /**当前页**/
    private int currentPage;
    /**一共有多少页**/
    private int totalPage;
    public int getResultCount() {
        return resultCount;
    }
    public void setResultCount(int resultCount) {
        this.resultCount = resultCount;
    }
    public int getCurrentPage() {
        return currentPage;
    }
    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }
    
    
    public int getTotalPage() {
        return totalPage;
    }
    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }
    
    public int getPageCount() {
        return pageCount;
    }
    public void setPageCount(int pageCount) {
        this.pageCount = pageCount;
    }
    
    public PageUtil() {
        // TODO Auto-generated constructor stub
    }
    /**
     * 计算从第几条开始
     * @param currentPage
     * @param pageCount
     * @return
     */
    public int getCurrent(int currentPage,int pageCount){
        return (currentPage-1)*pageCount;
    }
    
    public String getFooter(int resultCount,int pageCount,int currentPage){
        StringBuffer sb = new StringBuffer();
        if(resultCount>0&&pageCount>resultCount){
            totalPage = 1;
        }else if(resultCount>0&&pageCount<resultCount){
            totalPage = resultCount%pageCount==0?resultCount/pageCount:resultCount/pageCount+1;
        }else if(resultCount==0){
            totalPage = 0;
        }
        sb.append("共"+totalPage+"页").append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
        if(currentPage<=totalPage&&currentPage!=1){
            sb.append("<a href='javascript:void(0);' id='pre'>上一页</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
        }else{
            sb.append("上一页&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
        }
        sb.append("当前第<b id='current'>"+currentPage+"</b>页&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
        if(currentPage<totalPage){
            sb.append("<a href='javascript:void(0);' id='next'>下一页</a>").append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
        }else{
            sb.append("下一页").append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
        }
        sb.append("共"+resultCount+"条");
        return sb.toString();
        
    }
}
@RequestMapping("/selectUserList")
    public ModelAndView selectUserList(HttpServletRequest request,@RequestParam 
                                        Map<String,Object> conds){
        Map<String,Object> map = new HashMap<String, Object>();
        int currentPage =1;
        //用于判断是前一页还是下一页
        String type = (String) conds.get("type");
        //用来接受页面的当前页数,如果初始化是1
        String current= (String) conds.get("currentPage");
        if(null!=type){
            if("1".equals(type)){
                if(null!=current){
                    currentPage =Integer.valueOf(current)+1;
                }
            }else if("2".equals(type)){
                if(null!=current){
                    currentPage =Integer.valueOf(current)-1;
                }
            }
            
        }
        //设置每页的条数
        int pageCount =3;
        map.put("pageCount",pageCount);
        //查询出来记录总条数用于分页计算
        int count = userService.getTotalPage();
        PageUtil page = new PageUtil();
        //用于计算Limit 第一个参数 根据当前页数
        map.put("currentPage",page.getCurrent(currentPage,pageCount));
        //下面页脚
        String footer = page.getFooter(count, pageCount, currentPage);
        List<UserPO> list = userService.findAll(map);
        ModelMap modelMap = new ModelMap();
        modelMap.addAttribute("list",list);
        modelMap.addAttribute("footer",footer);
        ModelAndView modelAndView = new ModelAndView("user/user_list",modelMap);
        return modelAndView;
    }
            $("#next").click(function(){
                var current=$("#current").html();
                window.location.href="${app}/userController/selectUserList.do?currentPage="+current+'&type=1';
            })
            
            $("#pre").click(function(){
                var current=$("#current").html();
                window.location.href="${app}/userController/selectUserList.do?currentPage="+current+'&type=2';
            })


转载于:https://my.oschina.net/u/1998885/blog/489011

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值