在拜读了各位大牛的博客后,加以修改和添加总算是借鉴出了一个可行的分页实现。(●'◡'●)
话不多说,先贴两张效果图吧
接下来是实现代码:
pagingDAOImpl.java
public class pagingDAOImpl implements pagingDAO{ public List<PagingTest> getPerPageList(String hql, int firstResult, int pageSize) { Session session = HibernateSessionFactory.getSession(); Transaction tx = null; List<PagingTest> list = null; try { tx = session.beginTransaction(); Query query = session.createQuery(hql).setFirstResult(firstResult).setMaxResults(pageSize); list = query.list(); tx.commit(); } catch (Exception e) { if(tx != null) { tx.rollback(); } e.printStackTrace(); } finally { session.close(); } return list; } @Override public int getTotalRows(String hql) { Session session = HibernateSessionFactory.getSession(); Transaction tx = null; int totalRows = 0; try { tx = session.beginTransaction(); Query query = session.createQuery(hql); totalRows = query.list().size(); tx.commit(); } catch (Exception e) { if(tx != null) { tx.rollback(); } e.printStackTrace(); } finally { session.close(); } return totalRows; } }
pagingServiceImpl.java
public class pagingServiceImpl implements pagingService { private pagingDAO pagingDAO = new pagingDAOImpl(); /** * pageSize为每页显示的记录数 * page为当前显示的网页 */ public PageBean getPageBean(int pageSize, int page) { PageBean pageBean = new PageBean(); String hql = "from PagingTest"; int totalRows = pagingDAO.getTotalRows(hql); int totalPage = pageBean.getTotalPages(pageSize, totalRows); int currentPage = pageBean.getCurPage(page); int firstResult = pageBean.getCurrentPageFirstResult(pageSize, currentPage); List<PagingTest> list = pagingDAO.getPerPageList(hql, firstResult, pageSize); pageBean.setList(list); pageBean.setTotalRows(totalRows); pageBean.setCurrentPage(currentPage); pageBean.setTotalPage(totalPage); return pageBean; } }
PageBean.java
public class PageBean { private List<PagingTest> list; //通过hql从数据库分页查询出来的list集合 private int totalRows; //总记录数 private int totalPage; //总页数 private int currentPage; //当前页 public List<PagingTest> getList() { return list; } public void setList(List<PagingTest> list) { this.list = list; } public int getTotalRows() { return totalRows; } public void setTotalRows(int totalRows) { this.totalRows = totalRows; } public int getTotalPage() { return totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } public int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } /** * 得到总页数 * @param pageSize 每页记录数 * @param totalRows 总记录数 * @return 总页数 */ public int getTotalPages(int pageSize, int totalRows) { int totalPage = (totalRows % pageSize == 0)? (totalRows / pageSize): (totalRows / pageSize) + 1; return totalPage; } /** * 得到当前开始记录号 * @param pageSize 每页记录数 * @param currentPage 当前页 * @return */ public int getCurrentPageFirstResult(int pageSize, int currentPage) { int firstResult = pageSize * (currentPage - 1); return firstResult; } /** * 得到当前页, 如果为0 则开始第一页,否则为当前页 * @param page * @return */ public int getCurPage(int page) { int currentPage = (page == 0)? 1: page; return currentPage; } }
PagingAction.java
public class PagingAction extends ActionSupport { private pagingService pagingService = new pagingServiceImpl(); private int page; public int getPage() { return page; } public void setPage(int page) { this.page = page; } @Override public String execute() throws Exception { //表示每页显示5条记录,page表示当前网页 PageBean pageBean = pagingService.getPageBean(5, page); ActionContext context=ActionContext.getContext(); Map session=context.getSession(); session.put("pageBean", pageBean); return SUCCESS; } public static void main(String[] args) { } }
以上就是功能实现的相关代码,至于dao,service,HibernateSessionFactory之类的文件就不贴上来了,上面的代码才是精华!