标签:
首先封装一个分页类
public class Page<T> { /** * 当前页号 */ private int pageNumber; /** * 总条数 */ private int totalCount; /** * 总页数 */ private int totalPage; /** * 每页显示的数量 */ private int pageSize; /** * 当前页的数据 */ private List<T> item; /** * 当前起始行号 * @return */ private int from; public Page(){} public Page(int pageSize,int totalCount,String pageNumber) { setPageSize(pageSize); setTotalCount(totalCount); setPageNumber(pageNumber); } public int getPageNumber() { return pageNumber; } public void setPageNumber(String pageNumber) { if(StringUtil.isNumber(pageNumber)){ int tempNumber = Integer.parseInt(pageNumber); if(tempNumber < 1){ tempNumber = 1; }else if(tempNumber > getTotalPage()){ tempNumber = getTotalPage(); } this.pageNumber = tempNumber; }else{ this.pageNumber = 1; } } public int getTotalCount() { return totalCount; } public void setTotalCount(int totalCount) { this.totalCount = totalCount; this.totalPage = totalCount / getPageSize(); if(totalCount % getPageSize() != 0) { this.totalPage++; } } public int getTotalPage() { return totalPage; } /*public void setTotalPage(int totalPage) { this.totalPage = totalPage; }*/ public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public List<T> getItem() { return item; } public void setItem(List<T> item) { this.item = item; } public int getFrom() { return (getPageNumber() - 1) * getPageSize(); } public void setForm(int from) { this.from = from; } public List<Integer> getSlider(){ int startPageNumber = getPageNumber() - 2; int endPageNumber = getPageNumber() + 2; if(startPageNumber <= 0){ endPageNumber -= startPageNumber - 1; startPageNumber = 1; } if(endPageNumber > getTotalPage()){ int temp = getTotalPage() - endPageNumber; endPageNumber = getTotalPage(); startPageNumber = startPageNumber + temp; if(startPageNumber < 1){ startPageNumber = 1; } } List<Integer> result = new ArrayList<Integer>(); for(int i = startPageNumber;i <= endPageNumber;i++){ result.add(i); } return result; } }
controller中
Page<Active> page = new ActiveService().findByProjectid(projectid,pageNo);
service中
public Page<Active> findByProjectid(String projectid, String pageNo) { return activeDao.findAllByidAndPageNo(Long.valueOf(projectid),pageNo); }
dao中
public Page<Active> findAllByidAndPageNo(Long projectid, String pageNo) { String sql = "SELECT * FROM active,account WHERE active.accountid = account.id AND projectid = ? order by active.id desc limit ?,?"; Page<Active> page = new Page<Active>(5, findActiveCountByprojectid(projectid), pageNo); //因为返回的page中的item里是没有account信息的,但是在home.jsp中需要显示account的头像和姓名,所以要重写这个rowmapper List<Active> list = db.queryForList(sql, new RowMapper<Active>(){ @Override public Active mapRow(ResultSet rs) throws SQLException { Active active = new Active(); active.setAccountid(rs.getLong("accountid")); active.setContext(rs.getString("context")); active.setCreatetime(rs.getTimestamp("createtime")); active.setId(rs.getLong("id")); active.setProjectid(rs.getLong("projectid")); active.setType(rs.getString("type")); Account account = new Account(); account.setId(rs.getLong("accountId")); account.setUsername(rs.getString("username")); account.setPic(rs.getString("pic")); active.setAccount(account); return active; } }, projectid,page.getFrom(),page.getPageSize()); page.setItem(list); return page; } private int findActiveCountByprojectid(Long projectid) { String sql = "select count(*) from active where projectid = ?"; return db.queryForCount(sql, projectid).intValue(); }
标签:
原文地址:http://www.cnblogs.com/itliucheng/p/4581449.html