SSH分页查询操作实例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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="#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="user_number"/></td>
  					<td><s:property value="user_chinese_name"/></td>
  					<td><s:property value="group.group_chinese_name"/></td>
  					<td><s:property value="sex"/></td>
  					<td></td>
  					<td>
  						<s:url id="modify_user" value="ShowModifyUserForm.action">
  							<s:param name="user_id" value="user_id">
  							</s:param>
  						</s:url>
  						<s:a href="%{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="GetUsers.action">
  						<s:param name="currentPage" value="1"></s:param>
  					</s:url>
  					<s:a href="%{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="GetUsers.action">
  						<s:param name="currentPage" value="#request.pageBean.currentPage-1"></s:param>
  					</s:url>
  					<s:a href="%{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="GetUsers.action">
  						<s:param name="currentPage" value="#request.pageBean.currentPage+1"></s:param>
  					</s:url>
  					<s:a href="%{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="GetUsers.action">
  						<s:param name="currentPage" value="#request.pageBean.totalPage"></s:param>
  					</s:url>
  					<s:a href="%{getUsers_last}">末页</s:a>
  				</s:if>
  				<s:else>
  					末页
  				</s:else>
  			</td>
  			<td style="width:100px">共<s:property value="pageBean.totalPage"/>页</td>
  			<td style="width:200px">共<s:property value="pageBean.allRow"/>条记录</td>
  		</tr>
  	</table>

 

以上是关于SSH分页查询操作实例的主要内容,如果未能解决你的问题,请参考以下文章

PHP连接数据库实现多条件查询与分页功能——关于租房页面的完整实例操作

求一个JAVA SSH框架的实例

关于多表联合查询的实例以及分页代码的书写

SSH初体验系列--Hibernate--3--单值与分页查询

SSH——基于datagrid实现分页查询

springmvc 分页查询的简单实现