分页查询

java代码

public class PageBean {
	private int curPage = 1;// 当前页
	private int pageSize = 5;// 每页记录
	private int totalPage;// 总页数
	private int totalCount;// 总记录
	private boolean pre;// 上一页
	private boolean next;// 下一页
	@SuppressWarnings("rawtypes")
	private List list;// 分页查询的集合
	private String hql;

	public String getHql() {
		return hql;
	}
	public void setHql(String hql) {
		this.hql = hql;
	}
	public int getCurPage() {
		//如果总页数为0,则当前页也为0
		if(this.getTotalPage()==0)
			curPage=0;
		//当前页大于总页数时
		if(curPage>this.getTotalPage()){
			curPage=this.getTotalPage();
		}
		if(curPage<1)
			curPage=1;
		return curPage;
	}
	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}
	public int getTotalPage() {
		totalPage = (totalCount % pageSize == 0) ? (totalCount / pageSize)
				: (totalCount / pageSize + 1);
		return totalPage;
	}
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		if(pageSize>0) 
			this.pageSize = pageSize;
	}

	public boolean isPre() {
		pre=(curPage>1)?true:false;
		return pre;
	}

	public void setPre(boolean pre) {
		this.pre = pre;
	}

	public boolean isNext() {
		next=(curPage<this.totalPage)?true:false;
		return next;
	}

	public void setNext(boolean next) {
		this.next = next;
	}

	@SuppressWarnings("rawtypes")
	public List getList() {
		return list;
	}

	@SuppressWarnings("rawtypes")
	public void setList(List list) {
		this.list = list;
	}
}


/**
 * 设置pageBean的值 总记录数 以及查询的集合
 */
public class CommonDao {
	private Session session = null;
	private Query query = null;
	@SuppressWarnings("rawtypes")
	private List list = new ArrayList();

	/**
	 * 通过HQL语句查询一个对象集合
	 * 
	 * @param hql
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public List find(String hql) {
		try {
			session = HibernateSessionFactory.getSession();
			query = session.createQuery(hql);
			list = query.list();
		} catch (HibernateException e) {
			e.printStackTrace();
			return null;
		} finally {
			HibernateSessionFactory.closeSession();
		}
		return list;
	}

	/**
	 * 对PageBean的属性进行设置 ( 总记录数 以及分页查询的集合)
	 * 
	 * @param bean
	 */
	public void setPageBean(PageBean bean) {
		String hql = bean.getHql();
		// 对hql进行判断 截取
		int index = hql.indexOf("from");
		if (index != -1) {
			hql=hql.substring(index);
		}
		index = hql.indexOf("order by");
		if (index != -1) {
			hql=hql.substring(0, index);
		}
		// 进行组装
		hql = "select count(*)" + hql;
		list = this.find(hql);
		// 不同版本获得的数据类型不一致
		
		Long count = (Long) list.get(0);
		bean.setTotalCount(count.intValue());// 设置总记录数

		// 分页查询的集合
		int start = (bean.getCurPage() - 1) * bean.getPageSize();// 开始记录数
		try {
			session = HibernateSessionFactory.getSession();
			query = session.createQuery(bean.getHql());
			query.setFirstResult(start);
			query.setMaxResults(bean.getPageSize());
			bean.setList(query.list());
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			HibernateSessionFactory.closeSession();
		}
	}
}

public class UserDaoImpl implements UserDao {
	public void getUserPage(PageBean pageBean) {
		String hql="from User order by id";
		if(pageBean.getHql()==null)
			pageBean.setHql(hql);
			dao.setPageBean(pageBean);	
	}

servlet中userServlet中的调用

public void doPage(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String curPage=request.getParameter("curPage");
		if(curPage!=null){
			pageBean.setCurPage(Integer.parseInt(curPage));
		}
		/*String hql="from User order by age";
		pageBean.setHql(hql);*/
		userDao.getUserPage(pageBean);//对pageBean进行初始化设置
		request.setAttribute("userPage", pageBean);
		request.getRequestDispatcher("/JSP/showPage.jsp").forward(request, response);
	}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值