当前位置: 代码迷 >> Web前端 >> 施用PageObject做分页
  详细解决方案

施用PageObject做分页

热度:73   发布时间:2012-11-23 00:03:29.0
使用PageObject做分页

/**
* 获取指定页的记录
*
* @param sql
*            要执行的sql
* @param args
*            要执行sql的参数
* @param req
*            页面传递的参数
* @return PageObject
*/
public PageObject queryForMPageList(String sql, Object[] args, Map req) {
int totalCount = 1;

int start = NumberUtils.toInt(ObjectUtils.toString(req.get("start"),
"1"));
int limit = NumberUtils.toInt(ObjectUtils.toString(req.get("limit"),
"25"));
String sort = ObjectUtils.toString(req.get("sort"));
String dir = ObjectUtils.toString(req.get("dir"));
System.out.println("sort是"+sort+",dir是"+dir);
StringBuffer buildsql = new StringBuffer();
buildsql.append("select * from (");
buildsql.append(" select rownum temp_num,temp.* from (");
if (sort != null && !"".equals(sort)) {
String pa = "order by (,|\\.|\\w|\\d| )+( asc| desc)?( Nulls last)?( Nulls first)?\\s?$";
String re = " order by " + sort + " " + dir;
System.out.println("re是"+re);
Matcher matcher = Pattern.compile(pa, Pattern.CASE_INSENSITIVE)
.matcher(sql);
if (matcher.find()) {
buildsql.append(matcher.replaceFirst(re));
System.out.println("matcher.replaceFirst(re)是"+matcher.replaceFirst(re));
}
} else {
buildsql.append(sql);
}
buildsql.append(" ) temp where rownum<="
+ String.valueOf(start + limit));
buildsql.append(") where temp_num>" + String.valueOf(start));

List list = super.getJdbcTemplate().queryForList(buildsql.toString(),
args);

String countsql = "select count(*) from (" + sql + ")";
totalCount = super.getJdbcTemplate().queryForInt(countsql, args);

PageObject page = new PageObject();
page.setDatasource(list);
page.setTotalCount(totalCount);

int _absolutePage = (int)Math.ceil(totalCount * 1.0/limit);
page.setAbsolutePage(_absolutePage);

int _currentPage = start/limit + 1;
page.setCurrentPage(_currentPage);

return page;
}
  相关解决方案