分页查询

来源:互联网 发布:ie11不支持ie8的js 编辑:程序博客网 时间:2024/05/22 02:09

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,则当前页也为0if(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);}




原创粉丝点击