Java codeimport java.util.List;
import org.apache.struts2.ServletActionContext;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class Zmxpage extends HibernateDaoSupport {
private int first;//从哪开始
private int pageSize;//一页存在多少数据
private String hql;//查询语句
private String hql1;
public String getHql1() {
return hql1;
}
public void setHql1(String hql1) {
this.hql1 = hql1;
}
/*
* ���磺Zmxpage z = new Zmxpage();
* z.getPages(1,10"from BeanName");
* List list = z.findByPage();
*/
// 分页方法
public void getPages(int currentPage,int pageSize,String hql)
{
this.first = (currentPage-1)*pageSize;
this.pageSize = pageSize;
this.hql = hql;
}
public List findByPage()
{
try {
List result = (List)getHibernateTemplate().execute(
new HibernateCallback()
{
public Object doInHibernate(Session sess)throws HibernateException
{
List tmp = sess.createQuery(hql)
.setFirstResult(first)
.setMaxResults(pageSize)
.list();
return tmp;
}
});
return result;
} catch (RuntimeException re) {
throw re;
}
}
public int gettotalPages()
{//获取总页数
try {
List list = getHibernateTemplate().find("select count(*) "+hql);
int j=0;
j = ((Long)list.get(0)).intValue();
int i = 0;
if(j%pageSize==0) i = j/pageSize;
else i = j/pageSize+1;
return i;
} catch (RuntimeException re) {
throw re;
}
}
}
------解决方案--------------------
jsp页面
当前${currentpage}页| 共 ${pages}页
------解决方案--------------------
http://wenku.baidu.com/view/e32c9c946bec0975f465e275.html
数据库中有TopN这个东西,这里是oracle实现它的方式,自己看吧