首页 > 代码库 > SSH分页查询操作实例

SSH分页查询操作实例

使用的框架是struts2+spring3+hibernate4,需要特别注意两点,其一是保证三层的松耦合,我看到有文章在Service层中向Dao传递HQL语句,其实是不对的;二是,新版hibernate在查询COUNT(*)返回记录数量时,类型改为了Long型,需要做特殊处理。

首先要做一个PageBean对象:

public class PageBean {
	//Result List
	@SuppressWarnings("rawtypes")
	private List list;
	
	//总行数
	private Long allRow;
	//总页数
	private Integer totalPage;
	//当前页
	private Integer currentPage;
	//每页条目数
	private Integer pageSize;
	
	//是否为第一页
	private Boolean firstPage;
	//是否为最后一页
	private Boolean lastPage;
	//是否有前一页
	private Boolean hasPreviousPage;
	//是否有后一页
	private Boolean hasNextPage;
	
	@SuppressWarnings("rawtypes")
	public List getList() {
		return list;
	}
	@SuppressWarnings("rawtypes")
	public void setList(List list) {
		this.list = list;
	}
	public Long getAllRow() {
		return allRow;
	}
	public void setAllRow(Long allRow) {
		this.allRow = allRow;
	}
	public Number getTotalPage() {
		return totalPage;
	}
	public Integer getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage;
	}
	public Integer getPageSize() {
		return pageSize;
	}
	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}
	public void setTotalPage(Integer totalPage) {
		this.totalPage = totalPage;
	}
	public Boolean getFirstPage() {
		return firstPage;
	}
	public void setFirstPage(Boolean firstPage) {
		this.firstPage = firstPage;
	}
	public Boolean getLastPage() {
		return lastPage;
	}
	public void setLastPage(Boolean lastPage) {
		this.lastPage = lastPage;
	}
	public Boolean getHasPreviousPage() {
		return hasPreviousPage;
	}
	public void setHasPreviousPage(Boolean hasPreviousPage) {
		this.hasPreviousPage = hasPreviousPage;
	}
	public Boolean getHasNextPage() {
		return hasNextPage;
	}
	public void setHasNextPage(Boolean hasNextPage) {
		this.hasNextPage = hasNextPage;
	}
	
	/**
	 * 初始化结果页面信息
	 */
	public void init(){
        this.firstPage = isFirstPage();
        this.lastPage = isLastPage();
        this.hasPreviousPage = isHasPreviousPage();
        this.totalPage = countTotalPage(pageSize, allRow);
        this.hasNextPage = isHasNextPage();
    }
    
    private boolean isFirstPage() {
        return currentPage == 1; 
    }
    private boolean isLastPage() {
        return currentPage == totalPage; 
    }
    private boolean isHasPreviousPage() {
        return currentPage != 1; 
    }
    private boolean isHasNextPage() {
        return currentPage != totalPage; 
    }
    
    
    /**
     * 计算总页数
     * @param pageSize 每页记录数
     * @param allRow 总记录数
     * @return 总页数
     */
    private Integer countTotalPage(Integer pageSize,Long allRow){
    	Integer row = allRow.intValue();
        Integer totalPage = row.intValue() % pageSize == 0 ? row/pageSize : row/pageSize+1;
        return totalPage;
    }
    
    /**
     * 计算当前页起始记录
     * @param pageSize 每页记录数
     * @param currentPage 当前第几页
     * @return
     */
    public Integer countOffset(Integer pageSize,Integer currentPage){
    	Integer offset = pageSize*(currentPage-1);
        return offset;
    }
    
}

 DAO层添加的两个接口实现如下:

	public List<User> getUsersByPage(Integer offSet, Integer length) {
		String hql = "FROM com.wang.rhys.model.User";
		@SuppressWarnings("unchecked")
		List<User> list = (ArrayList<User>)this.getObjects(hql, offSet, length);
		return list;
	}

	public Long getTotalUserNumber(Integer groupId) {
		String hql = "SELECT COUNT(*) FROM com.wang.rhys.model.User ";
		if(groupId!=0)
			hql+="WHERE group.group_id="+groupId;
		Long totalRows = (Long)this.getObject(hql);
		return totalRows;
	}

 Service层的接口实现:

	public PageBean getUsersByPage(Integer currentPage, Integer pageSize) {
		PageBean pageBean = new PageBean();
		
		//Set Parameters
		Long totalRows = userDao.getTotalUserNumber(0);
		pageBean.setAllRow(totalRows);
		pageBean.setCurrentPage(currentPage);
		pageBean.setPageSize(pageSize);
		
		//Initialize PageBean
		pageBean.init();
		
		//Get the splitted userList
		List<User> userList = userDao.getUsersByPage(pageBean.countOffset(pageSize, currentPage),pageSize);
		pageBean.setList(userList);
		return pageBean;
	}

 Struts Action实现:

	public String execute() throws Exception {
		
		//从session获取用户工号,如果不存在或处于非激活状态,返回登录页面
		String user_num = ActionContext.getContext().getSession().get("user_num").toString();
		if(!userService.isActive(user_num))
			return LOGIN;
		setPageBean(userService.getUsersByPage(currentPage, pageSize));
		return SUCCESS;
	}

 页面调用:

<table border="1" width="700px">
  			<!-- table head -->
  			<tr>
  				<td style="width:100px">工号</td>
  				<td style="width:100px">姓名</td>
  				<td style="width:150px">组件</td>
  				<td style="width:50px">性别</td>
  				<td style="width:100px">职位</td>
  				<td style="width:200px">备注</td>
  			</tr>
  		
  		
  		<s:iterator value="http://www.mamicode.com/#request.pageBean.list" status="st">
  			<s:if test="#st.odd==true">
  				<tr style="background-color: #cccccc">
  			</s:if>
  			<s:else>
  				<tr>
  			</s:else>
  					<td><s:property value="http://www.mamicode.com/user_number"/></td>
  					<td><s:property value="http://www.mamicode.com/user_chinese_name"/></td>
  					<td><s:property value="http://www.mamicode.com/group.group_chinese_name"/></td>
  					<td><s:property value="http://www.mamicode.com/sex"/></td>
  					<td></td>
  					<td>
  						<s:url id="modify_user" value="http://www.mamicode.com/ShowModifyUserForm.action">
  							<s:param name="user_id" value="http://www.mamicode.com/user_id">
  							</s:param>
  						</s:url>
  						<s:a href="http://www.mamicode.com/%{modify_user}">修改</s:a>
  					</td>
  				</tr><p/>
  		</s:iterator>
  		<tr>
  			<td style="width:100px">
  				<s:if test="#request.pageBean.hasPreviousPage">
  					<s:url id="getUsers_first" value="http://www.mamicode.com/GetUsers.action">
  						<s:param name="currentPage" value="http://www.mamicode.com/1"></s:param>
  					</s:url>
  					<s:a href="http://www.mamicode.com/%{getUsers_first}">首页</s:a>
  				</s:if>
  				<s:else>
  					首页
  				</s:else>
  				</td>
  			<td style="width:100px">
  				<s:if test="#request.pageBean.hasPreviousPage">
  					<s:url id="getUsers_previous" value="http://www.mamicode.com/GetUsers.action">
  						<s:param name="currentPage" value="http://www.mamicode.com/#request.pageBean.currentPage-1"></s:param>
  					</s:url>
  					<s:a href="http://www.mamicode.com/%{getUsers_previous}">上页</s:a>
  				</s:if>
  				<s:else>
  					上页
  				</s:else>
  			</td>
  			<td style="width:150px">
  				<s:if test="#request.pageBean.hasNextPage">
  					<s:url id="getUsers_next" value="http://www.mamicode.com/GetUsers.action">
  						<s:param name="currentPage" value="http://www.mamicode.com/#request.pageBean.currentPage+1"></s:param>
  					</s:url>
  					<s:a href="http://www.mamicode.com/%{getUsers_next}">下页</s:a>
  				</s:if>
  				<s:else>
  					下页
  				</s:else>
  			</td>
  			<td style="width:50px">
  				<s:if test="#request.pageBean.hasNextPage">
  					<s:url id="getUsers_last" value="http://www.mamicode.com/GetUsers.action">
  						<s:param name="currentPage" value="http://www.mamicode.com/#request.pageBean.totalPage"></s:param>
  					</s:url>
  					<s:a href="http://www.mamicode.com/%{getUsers_last}">末页</s:a>
  				</s:if>
  				<s:else>
  					末页
  				</s:else>
  			</td>
  			<td style="width:100px">共<s:property value="http://www.mamicode.com/pageBean.totalPage"/>页</td>
  			<td style="width:200px">共<s:property value="http://www.mamicode.com/pageBean.allRow"/>条记录</td>
  		</tr>
  	</table>

 

SSH分页查询操作实例