package com.kingstargroup.eqa.pageshow.action;
import java.util.List;
import org.apache.struts.action.Action;
import org.springframework.jdbc.core.JdbcTemplate;
import com.kingstargroup.framework.dao.Page;
public abstract class AbstractPageShowAction extends Action {
protected final int defaultPageSize = 50;
private JdbcTemplate jdbcTemplate;
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
protected Page queryForPage(String sql, int pageNo, int pageCount, Object[] args) {
String strSql = "";
int startNo = Page.getStartOfPage(pageNo, pageCount);
int endNo = startNo + pageCount;
int argsCount = args.length;
int totleCount = queryForCount(sql, args);
Object[] new_args;
if(startNo==0){
strSql = "select * from (" + sql + ") where rownum <= ?";
new_args = new Object[argsCount+1];
}
else{
strSql = "select * from (select row_.*,rownum rownum_ from (" + sql + ") row_ where rownum <= ?) where rownum_>?";
new_args = new Object[argsCount+2];
new_args[argsCount + 1] = new Integer(startNo);
}
for(int i=0; i<argsCount; i++){
new_args[i] =args[i];
}
new_args[argsCount] = new Integer(endNo);
List list = jdbcTemplate.queryForList(strSql, new_args);
return new Page(startNo, totleCount, pageCount, list);
}
private int queryForCount(String sql, Object[] args){
String strSql = "select count(rowid) from ( " + sql + ")";
return jdbcTemplate.queryForInt(strSql, args);
}
}
package com.kingstargroup.framework.dao;
import java.util.ArrayList;
import java.util.Collection;
import com.kingstargroup.framework.dao.hibernate.BaseHibernateDao;
/**
* 分页对象. 包含数据及分页信息.
*
* @author ajax
*/
public class Page implements java.io.Serializable {
/**
* 当前页第一条数据的位置,从0开始
*/
private int start;
/**
* 每页的记录数
*/
private int pageSize = BaseHibernateDao.DEFAULT_PAGE_SIZE;
/**
* 当前页中存放的记录
*/
private Object data;
/**
* 总记录数
*/
private int totalCount;
private int size;
/**
* 构造方法,只构造空页
*/
public Page() {
this(0, 0, BaseHibernateDao.DEFAULT_PAGE_SIZE, new ArrayList());
}
/**
* 默认构造方法
*
* @param start
* 本页数据在数据库中的起始位置
* @param totalSize
* 数据库中总记录条数
* @param pageSize
* 本页容量
* @param data
* 本页包含的数据
*/
public Page(int start, int totalSize, int pageSize, Object data) {
this.pageSize = pageSize;
this.start = start;
this.totalCount = totalSize;
this.data = data;
if (data instanceof Collection)
this.size = ((Collection) data).size();
else
this.size = ((Object[]) data).length;
}
/**
* 取数据库中包含的总记录数
*/
public int getTotalSize() {
return this.totalCount;
}
/**
* 取总页数
*/
public int getTotalPageCount() {
if (totalCount % pageSize == 0)
return totalCount / pageSize;
else
return totalCount / pageSize + 1;
}
/**
* 取每页数据容量
*/
public int getPageSize() {
return pageSize;
}
/**
* 当前页中的记录
*/
public Object getResult() {
return data;
}
/**
* 取当前页码,页码从1开始
*/
public int getCurrentPageNo() {
return (start / pageSize) + 1;
}
/**
* 是否有下一页
*/
public boolean hasNextPage() {
return (this.getCurrentPageNo() < this.getTotalPageCount() - 1);
}
/**
* 是否有上一页
*/
public boolean hasPreviousPage() {
return (this.getCurrentPageNo() > 1);
}
/**
* 获取任一页第一条数据的位置,每页条数使用默认值
*/
protected static int getStartOfPage(int pageNo) {
return getStartOfPage(pageNo, BaseHibernateDao.DEFAULT_PAGE_SIZE);
}
/**
* 获取任一页第一条数据的位置,startIndex从0开始
*/
public static int getStartOfPage(int pageNo, int pageSize) {
return (pageNo - 1) * pageSize;
}
public int getSize() {
return this.size;
}
}
posted on 2007-04-04 13:16
芨芨草 阅读(2174)
评论(1) 编辑 收藏 所属分类:
JAVA