分页原理以及解决方式

最近又重温了一次分页的任务,不知觉间觉得自己忘记了不少东西。本次将分页的一些自己的心得体会总结如下:

 

概述:

       分页总体原理为在每次点击页数的时候将点击的页数作为参数当前页传递到后台服务器端。服务器端经过nowpage和pagesize的一定算法将其他参数(例如pagecount,startrow,

endrow)等分页需要的属性计算出来。然后将startrow和endrow应用到查询的限制条数上。以查出来本页所需要的数据。在此同时更新了当前页等参数之后在服务器将page对象传递到前台更新分页情况展现即可


前台发送请求的2种写法:

1、 js提交form

//按条件查询分页
	function PageQuery(nowpage){	document.getElementById("searchForm").action+='?pageNow='+nowpage
			document.getElementById("searchForm").submit();
	}

2、 a标签href指向分页的action

<a href="ListInnerDialog.action?nowpage=1” id="indexpage">下一页</a>

3、 后台传递写法:request.setAttribute(page,page) 尽量不要用session


4、 分页算法:(仿照百度谷歌分页) 

参照: http://redarmychen.iteye.com/blog/946725
public class Pagination<T> {
	 // 分页信息  
    private int nowpage;// 当前页  
    private int countrecord;// 总记录  
    private int countpage;// 总页数  
  
    
    private int countpageleast; //页面实际显示页数,数据少的时候用到
    
    
    public static final int PAGESIZE = 10;// 每页显示的记录数  
  
    private int startpage;// 页面中的起始页  
    private int endpage;// 页面中的结束页  
  
    private final int SHOWPAGE = 9;// 页面中显示的总页数 baidu,google显示的总页数是20  
    // 在测试我们才用6来测试  
  
    private List<T> allentities;  
  
    private String url;  
  
    /** 根据当前页及总记录数来构造分页对象 */  
    public Pagination(int nowpage, int countrecord) {  
        this.nowpage = nowpage;  
        this.countrecord = countrecord;  
        
        /** 计算总页数 */  
        this.countpage = this.countrecord % this.PAGESIZE == 0 ? this.countrecord  
                / this.PAGESIZE  
                : this.countrecord / this.PAGESIZE + 1;  
  
        /** 计算startpage与endpage的值 */  
  
        /** 总页数数是否小于4 */  
        if (this.countpage < (this.SHOWPAGE / 2 + 1)) {  
            this.startpage = 1; // 页面中起始页就是1  
            this.endpage = this.countpage;// 页面中的最终页就是总页数  
        } else {  
            /** else中是总页数大于4的情况 */  
  
            /** 首先当前页的值是否小于等于4 */  
            if (this.nowpage <= (this.SHOWPAGE / 2 + 1)) {  
                this.startpage = 1;  
                this.endpage = this.nowpage + 4;  
                /** 判断页面的最终页是否大于总页数 */  
                if (this.endpage >= this.countpage) {  
                    this.endpage = this.countpage;  
                }  
            } else {  
                this.startpage = this.nowpage - 3;  
                this.endpage = this.nowpage + 4;  
  
                if (this.endpage >= this.countpage) {  
                    this.endpage = this.countpage;  
                    if (this.countpage < this.SHOWPAGE) {  
                        this.startpage = 1;  
                    } else {  
                        this.startpage = this.endpage - 5;  
                    }  
  
                }  
            }  
  
        }  
        this.countpageleast = endpage-startpage+1;
    }  
  
    public int getNowpage() {  
        return nowpage;  
    }  
  
    public void setNowpage(int nowpage) {  
        this.nowpage = nowpage;  
    }  
  
    public int getCountrecord() {  
        return countrecord;  
    }  
  
    public void setCountrecord(int countrecord) {  
        this.countrecord = countrecord;  
    }  
  
    public int getCountpage() {  
        return countpage;  
    }  
  
    public void setCountpage(int countpage) {  
        this.countpage = countpage;  
    }  
  
    public int getStartpage() {  
        return startpage;  
    }  
  
    public void setStartpage(int startpage) {  
        this.startpage = startpage;  
    }  
  
    public int getEndpage() {  
        return endpage;  
    }  
  
    public void setEndpage(int endpage) {  
        this.endpage = endpage;  
    }  
  
    public List<T> getAllentities() {  
        return allentities;  
    }  
  
    public void setAllentities(List<T> allentities) {  
        this.allentities = allentities;  
    }  
  
    public String getUrl() {  
        return url;  
    }  
  
    public void setUrl(String url) {  
        this.url = url;  
    }
    

	public int getCountpageleast() {
		return countpageleast;
	}

	public void setCountpageleast(int countpageleast) {
		this.countpageleast = countpageleast;
	}

	public Pagination() {
		
	}  
}

5、hibernate和Oracle RowNum的分页写法:

Query q = this.getHibernateTemplate().getSessionFactory().getCurrentSession().createQuery(hql);
		q.setFirstResult(startrow);	//开始条数
		q.setMaxResults(pagesize);	//查询出来几条
		List<WeiboPrivateBean> list = q.list();
	
	Oracle ROWNUM JDBC的写法:
	int pagesize = Pagination.PAGESIZE;
		int nowp = Integer.valueOf(nowpage);
		int startrow = (nowp-1)*pagesize;;	//rownum分页开始
		int endrow = pagesize*nowp;		//rownum分页结束
		int CountRec = 0;
		Map<String, Object> map = new HashMap<String,Object>();
		HibernateTemplate hibernateTemplate = this.getHibernateTemplate();
		Session session = hibernateTemplate.getSessionFactory().openSession();	//AEBF8D1FD75781B718C4A6DE91175544
		StringBuffer SqlWithPage = new StringBuffer("select * from (select A.*,ROWNUM rn from (");
		String sql = "select distinct userid,touserid from weibo_private where userid = '"+userid+"' or touserid = '"+userid+"'";
		SqlWithPage.append(sql);
		SqlWithPage.append(") A)where rn <="+endrow+" and rn >"+startrow);
		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		Integer contacts = 0;
		try {
			con = session.connection();
			stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
			rs = stmt.executeQuery(sql);
			rs.last();
			CountRec = rs.getRow();
			rs = stmt.executeQuery(SqlWithPage.toString());
			while(rs.next()){
				List<String> ids = new ArrayList<String>();
				String uid = "";
				String touserid = "";
				uid = rs.getString(1);
				touserid = rs.getString(2);
				ids.add(uid);
				ids.add(touserid);
				String key = UUID.randomUUID().toString();
				map.put(key, ids);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(null != rs){
				rs.close();
			}
			if(null != stmt){
				stmt.close();
			}
			if(null != con){
				con.close();
			}
		}
		
		
		Pagination page = new Pagination(Integer.valueOf(nowpage),CountRec);
		map.put("page", page);




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值