public class PageBean { private List<User> list; //要返回的某一页的记录列表 private int allRow; //总记录数 private int totalPage; //总页数 private int currentPage; //当前页 private int pageSize; //每页的记录数 private boolean isFirstPage; //是否为当前第一页 private boolean isLastPage; //是否为最后一页 private boolean hasPreviousPage; //是否有前一页 private boolean hasNextPage; //是否有下一页 /** * 初始化分页信息 */ public void init(){ this.isFirstPage = isFirstPage; this.isLastPage = isLastPage; this.hasPreviousPage = hasPreviousPage; this.hasNextPage = hasNextPage; } /** * 计算总页数 静态方法 * @param pageSize 每页的记录数 * @param allRow 总记录数 * @return 总页数 */ public static int countTatalPage(final int pageSize,final int allRow){ int toalPage = allRow % pageSize == 0 ? allRow/pageSize : allRow/pageSize + 1; return toalPage; } /** * 计算当前页开始的记录 * @param pageSize 每页记录数 * @param currentPage 当前第几页 * @return 当前页开始记录号 */ public static int countOffset(final int pageSize,final int currentPage){ final int offset = pageSize * (currentPage - 1); return offset; } /** * 计算当前页,若为0或者请求的URL中没有“?page = ”则用1代替 * @param page 传入的参数(可能为空,即0 则返回1) * @return */ public static int countCurrentPage(int page){ final int curpage = (page == 0 ? 1 : page); return curpage; } public List<User> getList() { return list; } public void setList(List<User> list) { this.list = list; } public int getAllRow() { return allRow; } public void setAllRow(int allRow) { this.allRow = allRow; } 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; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public boolean isFirstPage() { return isFirstPage; } public void setFirstPage(boolean isFirstPage) { this.isFirstPage = isFirstPage; } public boolean isLastPage() { return isLastPage; } public void setLastPage(boolean isLastPage) { this.isLastPage = isLastPage; } public boolean isHasPreviousPage() { return hasPreviousPage; } public void setHasPreviousPage(boolean hasPreviousPage) { this.hasPreviousPage = hasPreviousPage; } public boolean isHasNextPage() { return hasNextPage; } public void setHasNextPage(boolean hasNextPage) { this.hasNextPage = hasNextPage; }
Dao 及实现
public interface MemberDao { /** * 分页查询 * @param hql 查询条件 * @param offset 开始记录 * @param length 一次查询几条记录 * @return 查询的记录集合 */ public List<User> queryForPage(final String hql,final int offset,final int length); /** * 查询所有的记录数 * @param hql 查询条件 * @return 总记录数 */ public int getAllRowCount(String hql); } @Component("memberDao") public class MemberDaoImpl extends BaseDAO implements MemberDao{ /** * 查询所有的记录数 * @param hql 查询条件 * @return 总记录数 */ public int getAllRowCount(String hql) { return this.getHibernateTemplate().find(hql).size(); } /** * 分页查询 * @param hql 查询条件 * @param offset 开始记录 * @param length 一次查询几条记录 * @return 查询的记录集合 */ @SuppressWarnings("unchecked") public List<User> queryForPage(final String hql, final int offset, final int length) { Session session = this.hibernateTemplate.getSessionFactory().openSession(); Query q = session.createQuery(hql); q.setFirstResult(offset); q.setMaxResults(length); List<User> list = q.list(); System.out.println(list.size()); session.close(); return list; } }
service及实现层
public interface UserService { /** * 分页查询 * @param pageSize 每页显示多少记录 * @param currentPage 当前页 * @return 封装了分页信息的bean */ public PageBean queryForPage(int pageSize,int page); } @Service("userService") public class UserServiceImpl implements UserService{ private MemberDao memberDao; /** * 分页查询 * @param pageSize 每页显示多少记录 * @param currentPage 当前页 * @return 封装了分页信息的bean */ public PageBean queryForPage(int pageSize, int page) { final String hql = "from User user order by user.id"; //查询语句 int allRow = memberDao.getAllRowCount(hql); //总记录数 int totalPage = PageBean.countTatalPage(pageSize, allRow); //总页数 final int offset = PageBean.countOffset(pageSize, page); //当前页开始记录 final int length = pageSize; // 每页记录数 final int currentPage = PageBean.countCurrentPage(page); // 当前页 List list = memberDao.queryForPage(hql, offset, length); // //把分页信息保存到Bean当中 PageBean pageBean = new PageBean(); pageBean.setPageSize(pageSize); pageBean.setCurrentPage(currentPage); pageBean.setAllRow(allRow); pageBean.setTotalPage(totalPage); pageBean.setList(list); pageBean.init(); return pageBean; } public MemberDao getMemberDao() { return memberDao; } @Resource public void setMemberDao(MemberDao memberDao) { this.memberDao = memberDao; } }
前台页面
<table> <s:iterator value="pageBean"> <tr > <td colspan="3" align="center" bgcolor=""> 共<s:property value="allRow"/>条记录 共<s:property value="totalPage"/>页 当前第<s:property value="currentPage"/>页 <s:if test="%{currentPage == 1}"> 第一页 上一页 </s:if> <!-- currentPage为当前页 --> <s:else> <a href="listUser2.action?page=1&pageSize=<s:property value="%{pageSize}" />">第一页</a> <a href="listUser2.action?page=<s:property value="%{currentPage-1}"/>&pageSize=<s:property value="%{pageSize}" />">上一页</a> </s:else> <s:if test="%{currentPage != totalPage}"> <a href="listUser2.action?page=<s:property value="%{currentPage+1}" />&pageSize=<s:property value="%{pageSize}" />">下一页</a> <a href="listUser2.action?page=<s:property value="totalPage"/>&pageSize=<s:property value="%{pageSize}" />">最后一页</a> </s:if> <s:else> 下一页 最后一页 </s:else> 页面大小<select name="pageSize" id="pageSize" onchange="window.location.href=this.options[selectedIndex].value"> <option value="listUser2.action?pageSize=5" >5</option> <s:if test="pageSize==10"> <option value="listUser2.action?pageSize=10" selected="selected">10</option> <option value="listUser2.action?pageSize=15" >15</option> </s:if> <s:else> <option value="listUser2.action?pageSize=10" >10</option> <s:if test="pageSize==15"> <option value="listUser2.action?pageSize=15" selected="selected">15</option> </s:if> <s:else> <option value="listUser2.action?pageSize=15" >15</option> </s:else> </s:else> </select> </td> </tr> </s:iterator> </table>
时间: 2024-10-20 00:46:01