分页大概是所有WEB程序都需要去进行的一个工作,将多条数据分成多页,不会使用户感到冗长,那么如何实现一个通用的方法,使分页更加简单,这是我们所关注的问题,话不多说,上代码。
Java代码部分
PageInfo.java 记录Page相关信息
import java.util.List;
public class PageInfo {
// 传入的数据
private int currentPage;// 当前页面的Index
private int pageSize;// 每页所显示的数据大小
// 从数据库中取得的数据
private List currentPageList;// 当前页面所要显示的数据
private int recordCount;// 总记录的数
// 所要计算的数据
private int beginPage;// 开始的页面的Index
private int endPage;// 结束页面的Index
private int pageCount;// 总的页面数
public PageInfo() {
}
public PageInfo(int currentPage, int pageSize, List currentPageList,
int recordCount) {
super();
this.currentPage = currentPage;
this.pageSize = pageSize;
this.currentPageList = currentPageList;
this.recordCount = recordCount;
this.pageCount = (recordCount/pageSize)+1;
//如果总页面数小于10条,则显示前10条
if(pageCount<10){
this.beginPage=1;
this.endPage=pageCount;
}else{
//如果总记录数大于10条
if(currentPage-4<1){
this.beginPage=1;
this.endPage=currentPage+5;
}else if(currentPage+5>pageCount){
this.beginPage = currentPage - 4;
this.endPage = pageCount;
}else{
this.beginPage=currentPage-4;
this.endPage=currentPage+5;
}
//如果currentPage-4 = beginPage<1
//如果currentPage+5 = endPage >
}
}
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 getBeginPage() {
return beginPage;
}
public void setBeginPage(int beginPage) {
this.beginPage = beginPage;
}
public int getEndPage() {
return endPage;
}
public void setEndPage(int endPage) {
this.endPage = endPage;
}
public List getCurrentPageList() {
return currentPageList;
}
public void setCurrentPageList(List currentPageList) {
this.currentPageList = currentPageList;
}
}
辅助拼写SQL语句的QueryHelper类
import java.util.ArrayList;
import java.util.List;
import org.qust.oa.basedaoImple.BasedaoImple;
import org.qust.oa.domain.PageInfo;
import com.opensymphony.xwork2.ActionContext;
public class QueryHelper {
private String fromClause; // 拼接成的FROM
private String whereClause = ""; // 拼接成的Where
private String orderByClause = ""; // 拼接成的OrderBy
private List<Object> parameters = new ArrayList<Object>();
/**
*
*
* @param clazz
* @param alias
*
*/
public QueryHelper(Class clazz, String alias) {
fromClause = "FROM " + clazz.getSimpleName() + " " + alias;
}
public QueryHelper addCondition(String condition, Object... params) {
//
if (whereClause.length() == 0) {
whereClause = " WHERE " + condition;
} else {
whereClause += " AND " + condition;
}
if (params != null) {
for (Object p : params) {
parameters.add(p);
}
}
return this;
}
/**
* 筛选的条件
*
* @param append
* @param condition
* @param params
*/
public QueryHelper addCondition(boolean append, String condition, Object... params) {
if (append) {
addCondition(condition, params);
}
return this;
}
/**排序的条件
*/
public QueryHelper addOrderProperty(String propertyName, boolean asc) {
if (orderByClause.length() == 0) {
orderByClause = " ORDER BY " + propertyName + (asc ? " ASC" : " DESC");
} else {
orderByClause += ", " + propertyName + (asc ? " ASC" : " DESC");
}
return this;
}
public QueryHelper addOrderProperty(boolean append, String propertyName, boolean asc) {
if (append) {
addOrderProperty(propertyName, asc);
}
return this;
}
//得到当前页的记录
public String getListQueryHql() {
return fromClause + whereClause + orderByClause;
}
//得到数据的记录数
public String getCountQueryHql() {
return "SELECT COUNT(*) " + fromClause + whereClause;
}
//获取封装的参数
public List<Object> getParameters() {
return parameters;
}
}
使用QueryHelper进行查询的示例代码:
QueryHelper qh = new QueryHelper(类名.class, "别名").addCondition(
"别名.属性名=?", 属性值)
.addCondition((viewType == 1), "别名.属性名=?", Topic.TYPE_BEST) // 1 表示只看精华帖
.addOrderProperty((orderBy == 1), "别名.属性名", asc) // 1 表示只按最后更新时间排序
.addOrderProperty((orderBy == 2), "别名.属性名", asc) // 2 表示只按主题发表时间排序
.addOrderProperty((orderBy == 3), "别名.属性名“, asc) // 3 表示只按回复数量排序
.addOrderProperty((orderBy == 0), "(CASE "别名.属性名“ WHEN 2 THEN 2 ELSE 0 END)", false)//
.addOrderProperty((orderBy == 0), "别名.属性名", false);
PageInfo pageInfo = topicService
.findPageInfo(currentPage, pageSize, qh);
findPageInfo类代码
public PageInfo findPageInfo(Integer currentPage, Integer pageSize,
QueryHelper qh){
// 参数列表
List<Object> parameters = qh.getParameters();
// 查询本页的数据列表
Query listQuery = getSession().createQuery(qh.getListQueryHql()); // 创建查询对象
if (parameters != null) { // 设置参数
for (int i = 0; i < parameters.size(); i++) {
listQuery.setParameter(i, parameters.get(i));
}
}
listQuery.setFirstResult((currentPage - 1) * pageSize);
listQuery.setMaxResults(pageSize);
List list = listQuery.list(); // 执行查询
// 查询总记录数量
Query countQuery = getSession().createQuery(qh.getCountQueryHql());
if (parameters != null) { // 设置参数
for (int i = 0; i < parameters.size(); i++) {
countQuery.setParameter(i, parameters.get(i));
}
}
Long count = (Long) countQuery.uniqueResult(); // 执行查询
return new PageInfo(currentPage, pageSize, list,count.intValue());
}
通用的HTML代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!--分页信息-->
<div id=PageSelectorBar>
<div id=PageSelectorMemo>页次:${currentPage}/${pageCount}页
每页显示:${pageSize}条 总记录数:${recordCount}条</div>
<div id=PageSelectorSelectorArea>
<s:a href="javascript: goToPage(1)" title="首页" style="cursor: hand;">
<img
src="${pageContext.request.contextPath}/style/blue/images/pageSelector/firstPage.png" />
</s:a>
<!-- 显示页面按钮 -->
<s:iterator begin="beginPage" end="endPage" var="page">
<s:if test="#page == currentPage">
<!-- 当前页 -->
<span class="PageSelectorNum PageSelectorSelected">${page}</span>
</s:if>
<s:else>
<!-- 非当前页 -->
<span class="PageSelectorNum" style="cursor: hand;"
onClick="goToPage('${page}');">${page}</span>
</s:else>
</s:iterator>
<s:a href="javascript: goToPage(%{pageCount})" title="尾页"
style="cursor: hand;">
<img
src="${pageContext.request.contextPath}/style/blue/images/pageSelector/lastPage.png" />
</s:a>
转到: <select οnchange="goToPage(this.value);" id="_selected">
<s:iterator begin="1" end="%{pageCount}" var="number">
<option value="${number}">${number}</option>
</s:iterator>
</select>
<script type="text/javascript">
$("#_selected").val("${currentPage}");
</script>
</div>
</div>
效果图: