ssh中的HibernateDaoSupport 实现分页
数据库为mysql5.0 表名为Author
struts2+hibernate3.0+spring3.0
首先是在自动生成的DAO里面加上如下代码
public List findAll(){
String sql="FROM Author order by author_id";
return getHibernateTemplate().find(sql);
}
public List findAllByPage( final int pageNow, final int pageSize){
List list = (List) getHibernateTemplate().execute(
new HibernateCallback() {
int size=pageNow*pageSize-pageSize;
public Object doInHibernate(Session session)
throws HibernateException {
Query q =session.createQuery("from Author order by author_id");
q.setFirstResult(size);
q.setMaxResults(pageSize);
List cats = q.list();
return cats;
}
});//内部类,返回一个List
return list;
}
然后就是在action里面定义
public int pageNow = 1 ; //初始化为1,默认从第一页开始显示
public int pageSize =3 ; //每页显示5条记录 并生成get,set方法
public int getPageNow() {
return pageNow;
}
public void setPageNow(int pageNow) {
this.pageNow = pageNow;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
} 最后就是前台jsp的调用
<s:url id="url_pre" value="List.action">
<s:param name="pageNow" value="pageNow-1"></s:param>
</s:url>
<s:url id="url_next" value="List.action">
<s:param name="pageNow" value="pageNow+1"></s:param>
</s:url>
<s:form action="List" theme="simple">
转到第<s:textfield name="pageNow"></s:textfield>页
<span><s:submit value=''GO'' name=''cndok''/></span>
</s:form>
<s:a href="%{url_pre}">上一页</s:a>
<s:a href="%{url_next}">下一页</s:a>
struts2+hibernate3.0+spring3.0
首先是在自动生成的DAO里面加上如下代码
public List findAll(){
String sql="FROM Author order by author_id";
return getHibernateTemplate().find(sql);
}
public List findAllByPage( final int pageNow, final int pageSize){
List list = (List) getHibernateTemplate().execute(
new HibernateCallback() {
int size=pageNow*pageSize-pageSize;
public Object doInHibernate(Session session)
throws HibernateException {
Query q =session.createQuery("from Author order by author_id");
q.setFirstResult(size);
q.setMaxResults(pageSize);
List cats = q.list();
return cats;
}
});//内部类,返回一个List
return list;
}
然后就是在action里面定义
public int pageNow = 1 ; //初始化为1,默认从第一页开始显示
public int pageSize =3 ; //每页显示5条记录 并生成get,set方法
public int getPageNow() {
return pageNow;
}
public void setPageNow(int pageNow) {
this.pageNow = pageNow;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
} 最后就是前台jsp的调用
<s:url id="url_pre" value="List.action">
<s:param name="pageNow" value="pageNow-1"></s:param>
</s:url>
<s:url id="url_next" value="List.action">
<s:param name="pageNow" value="pageNow+1"></s:param>
</s:url>
<s:form action="List" theme="simple">
转到第<s:textfield name="pageNow"></s:textfield>页
<span><s:submit value=''GO'' name=''cndok''/></span>
</s:form>
<s:a href="%{url_pre}">上一页</s:a>
<s:a href="%{url_next}">下一页</s:a>