例如,一个产品类,Product,创建好需要的entity,需要从前台传入的数据有当前的页数,封装一个Page类,如下:
/**
* 分页类的封装
*/
@Component
public class Page {
private int page; // 当前页数
private int totalCount; // 总记录数
private int totalPage; // 总页数
private int limit; // 每页显示的记录数
private List list; // 每页显示数据的集合.
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getLimit() {
return limit;
}
public void setLimit(int limit) {
this.limit = limit;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
}
然后再编写继承ActionSupport类并实现ModelDriven的action类:
@Component
public class ProductAction extends ActionSupport implements ModelDriven{
@Resource
private ProductInfo productInfo = new ProductInfo();
@Override
public Object getModel() {
return productInfo;
}
}
action中要实现分页查询的方法,所以编写dao层的分页查询方法:
dao层的分页查询实现是继承了HibernateDaoSupport中的方法:
this.getHibernateTemplate().execute(HibernateCallback action)
这里重新封装一下自己使用的HibernateCallback类:
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
public class PageHibernateCallback implements HibernateCallback>{
private String hql;
private Object[] params;
private int startIndex;
private int pageSize;
public PageHibernateCallback(String hql, Object[] params,
int startIndex, int pageSize) {
super();
this.hql = hql;
this.params = params;
this.startIndex = startIndex;
this.pageSize = pageSize;
}
public List doInHibernate(Session session) throws HibernateException,
SQLException {
//执行hql语句
Query query = session.createQuery(hql);
//设置实际参数
if(params != null){
for(int i = 0 ; i < params.length ; i ++){
query.setParameter(i, params[i]);
}
}
//实现分页
query.setFirstResult(startIndex);
query.setMaxResults(pageSize);
return query.list();
}
}
封装好了自己使用的分页查询回调方法,那么在dao层编写分页查询方法:
//Dao层的分页查询方法
public List findByPage(Integer id, int begin, int limit) {
String hql = "select p from Product p where p.id = ? ";
List list = this.getHibernateTemplate().execute(new PageHibernateCallback(hql, new Object[]{id}, begin, limit));
if(list != null && list.size() > 0){
return list;
}
return null;
}
接着在service层使用dao的方法即可:
public Page findByPage(Integer id, Integer pageNumber) {
int limit = 10;
int begin = (page -1) * limit;
page.setLimit(limit);
int totalCount = 0;
totalCount = productDao.findCountByCsid(csid);
page.setTotalCount(totalCount);
page.setPage(pageNumber);
int totalPage = (int) Math.ceil((double)totalCount / limit);
page.setTotalPage(totalPage);
List list = productDao.findByPage(id, begin, limit);
page.setList(list);
return page;
}
然后在action中编写需要的方法:
private Integer id;
private Integer page;
//设置好需要从前台获取的数据
public void setId(Integer id) {
this.id = id;
}
public void setPage(Integer page) {
this.page = page;
}
public void setPage(Integer page) {
this.page = page;
}
//action类中方法
public String findByPage() {
Page page = productService.findByPage(id, page);// 根据一级分类查询商品,带分页查询
// 将Page存入到值栈中:
ActionContext.getContext().getValueStack().set("page", page);
return "findByPage";
}
最后在jsp页面和struts.xml配置好对应的属性即可.