SSH实现分页功能

public class QueryHelper {
	
	/**
	 * fromClause: FROM子句
	 */
	private StringBuffer	fromClause		= null;
	
	/**
	 * whereClause:WHERE子句
	 */
	private StringBuffer	whereClause		= null;
	
	/**
	 * orderByClause: ORDER BY 子句
	 */
	private StringBuffer	orderByClause	= null;
	
	private List	parameters		= new ArrayList();
	
	/**
	 * 生成FROM子句
	 * 
	 * @param :查询对象
	 * @param :别名
	 */
	public QueryHelper(Class
     
      clazz, String alias) {
		fromClause = new StringBuffer(" FROM " + clazz.getSimpleName() + " " + alias);
	}
	
	/**
	 * 拼接WHERE子句
	 * 
	 * @param :condition WHERE条件
	 * @param :param 参数
	 */
	public QueryHelper addWhereCondition(String condition, Object... param) {
		// 子句
		if (whereClause == null) {
			whereClause = new StringBuffer(" WHERE " + condition);
		} else {
			whereClause.append(" AND " + condition);
		}
		// 参数
		if (param != null) {
			for (Object object : param) {
				parameters.add(object);
			}
		}
		return this;
	}
	
	public QueryHelper addWhereCondition(boolean append, String condition, Object... param) {
		if (append) {
			addWhereCondition(condition, param);
		}
		return this;
	}
	
	/**
	 * 拼接order by子句
	 * 
	 * @param :propertyName 参与排序的属性名
	 * @param :asc true升序,false降序
	 */
	public QueryHelper addOrderByProperty(String propertyName, boolean asc) {
		if (orderByClause == null) {
			orderByClause = new StringBuffer(" ORDER BY  " + propertyName + (asc ? " ASC " : " DESC "));
		} else {
			orderByClause.append(" , " + propertyName + (asc ? " ASC " : " DESC "));
		}
		return this;
	}
	
	public QueryHelper addOrderByProperty(boolean append, String propertyName, boolean asc) {
		if (append) {
			addOrderByProperty(propertyName, asc);
		}
		return this;
	}
	
	/**
	 * 获取生成的用于查询数据列表的HQL语句
	 * 
	 * @return
	 */
	public String getListQueryHQL() {
		return "" + fromClause + (whereClause == null ? "" : whereClause) + (orderByClause == null ? "" : orderByClause);
	}
	
	/**
	 * 获取记录条数
	 * 
	 * @return
	 */
	public String getCountQueryHQL() {
		return "SELECT COUNT(*) " + fromClause + (whereClause == null ? "" : whereClause);
	}
	
	public StringBuffer getFromClause() {
		return fromClause;
	}
	
	public void setFromClause(StringBuffer fromClause) {
		this.fromClause = fromClause;
	}
	
	public StringBuffer getWhereClause() {
		return whereClause;
	}
	
	public void setWhereClause(StringBuffer whereClause) {
		this.whereClause = whereClause;
	}
	
	public StringBuffer getOrderByClause() {
		return orderByClause;
	}
	
	public void setOrderByClause(StringBuffer orderByClause) {
		this.orderByClause = orderByClause;
	}
	
	public List getParameters() {
		return parameters;
	}
	
	public void setParameters(List parameters) {
		this.parameters = parameters;
	}
}
public class PageBean {

	// 此块变量是通过查询数据库获取的
	private int recordCount; // 总记录数
	private List recordList; // 本页的数据列表

	// 此块变量是通过指定的或页面参数获取的
	private int currentPage; // 当前页
	private int pageSize; // 每页显示多少条数据

	// 此块变量是通过计算得到的
	private int pageCount; // 总页数
	private int beginPageIndex; // 页码列表的开始索引
	private int endPageIndex; // 页码列表的结束索引

	/**
	 * 只接收前四个必要属性的值,会自动计算出其他三个属性的值
	 * 
	 */
	public PageBean(int currentPage, int pageSize, int recordCount,
			List recordList) {
		super();
		this.recordCount = recordCount;
		this.recordList = recordList;
		this.currentPage = currentPage;
		this.pageSize = pageSize;

		// 计算总页码
		pageCount = (recordCount + pageSize - 1) / pageSize;

		// 计算页码列表的beginPageIndex endPageIndex
		// 总页数不多于10页,则全部显示
		if (pageCount <= 10) {
			beginPageIndex = 1;
			endPageIndex = pageCount; 
		} else {
			// 总页数多于10页,则显示当前页附近的10个页码
			// 当前页附近的共10个页码(前四个,当前页,后五个)
			beginPageIndex = currentPage - 4;
			endPageIndex = currentPage + 5;
			if (beginPageIndex < 1) {
				// 当前面页码不足4个时,则显示前10个页码
				beginPageIndex = 1;
				endPageIndex = 10;
			} 
			if (endPageIndex > pageCount) {
				// 当后面页码不足5个时,则显示后10个页码
				endPageIndex = pageCount;
				beginPageIndex = pageCount - 10 + 1;
			}
		}
	}

	public List getRecordList() {
		return recordList;
	}

	public void setRecordList(List recordList) {
		this.recordList = recordList;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getPageCount() {
		return pageCount;
	}

	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getRecordCount() {
		return recordCount;
	}

	public void setRecordCount(int recordCount) {
		this.recordCount = recordCount;
	}

	public int getBeginPageIndex() {
		return beginPageIndex;
	}

	public void setBeginPageIndex(int beginPageIndex) {
		this.beginPageIndex = beginPageIndex;
	}

	public int getEndPageIndex() {
		return endPageIndex;
	}

	public void setEndPageIndex(int endPageIndex) {
		this.endPageIndex = endPageIndex;
	}
}

list()方法中加入如下语句即可将查询的内容放到栈顶pageBean中
QueryHelper queryHelper = new QueryHelper(Account.class, "a");
		queryHelper//
				.addWhereCondition("a.loginName!=?", "admin")//
				.addWhereCondition((model.getLoginName() != null && !"".equals(model.getLoginName())), "a.loginName=?", model.getLoginName())//
				.addWhereCondition((model.getName() != null && !"".equals(model.getName())), "a.name=?", model.getName());//
				//.addWhereCondition(departmentId != null, "a.department.id=?", departmentId);//
				//.addWhereCondition(permissionGroupId != null, "a.permissionGroup.id=?", permissionGroupId);
		
		PageBean pageBean = accountService.getPageBean(pageNum, pageSize, queryHelper);
		ActionContext.getContext().getValueStack().push(pageBean);
		
		
		
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>

 ${currentPage}/${pageCount}页 共${recordCount}条   每页显示${pageSize}条  << 首页 1 
         
           ${currentPage-1} 
         )" class="page-next form-element"><上一页 
         
          
          ${num} 
          
          
          ${num} 
          
          pageCount'>${pageCount} 
         
           ${currentPage+1} 
         )" class="page-next form-element">下一页> 尾页>> 
<script type="text/javascript">
function gotoPage(pageNum) {
	//window.location.href = "account_list.action?pageNum="+pageNum;
	$("#page").append("");
	$("#page").submit();
}
</script>

最后只需在要分页的jsp页面添加如下代码即可,

       
        
         
       

       
       
	<%@include file="/WEB-INF/jsp/public/pageView.jspf"%>
										

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值