【SSH】分页查询

来总结一下SSH关于分页查询的代码。

一、PageBean.java:

package cn.itcast.shop.utils;

import java.util.List;

/**
 * 分页类的封装
 * @author 宋喆
 */
public class PageBean<T> {
	private int page;//当前页数
	private int totalCount;//总记录数
	private int totalPage;//总页数
	private int limit;//每页显示的记录数
	private List<T>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<T> getList() {
		return list;
	}
	public void setList(List<T> list) {
		this.list = list;
	}
	
}

二、AdminProductAction.java

package cn.itcast.shop.product.adminaction;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.shop.product.service.ProductService;
import cn.itcast.shop.product.vo.Product;
import cn.itcast.shop.utils.PageBean;
import javassist.expr.NewArray;

/**
 * 后台商品管理的Action
 * @author 宋喆
 *
 */
public class AdminProductAction extends ActionSupport implements ModelDriven<Product>{
	//模型驱动使用的对象
	private Product product = new Product();

	public Product getModel() {
		return product;
	}
	
	//注入商品的Service
	private ProductService productService;

	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	//接收page参数:
	private Integer page;

	public void setPage(Integer page) {
		this.page = page;
	}
	
	//带分页的查询商品的执行的方法:
	public String findAll(){
		//调用Service 完成查询操作
		PageBean<Product> pageBean = productService.findByPage(page);
		//将数据传递到页面上:
		ActionContext.getContext().getValueStack().set("pageBean", pageBean);
		//页面跳转:
		return "findAll()";
	}
}

三、ProductService.java:

	//业务层查询商品带分页的方法
	public PageBean<Product> findByPage(Integer page) {
		PageBean<Product> pageBean = new PageBean<Product>();
		//设置当前的页数:
		pageBean.setPage(page);
		//设置每页显示记录数:
		int limit = 10;
		pageBean.setLimit(limit);
		//设置总记录数:
		int totalCount = productDao.findCount();
		pageBean.setTotalCount(totalCount);
		//设置总页数:
		int totalPage = 0;
		if(totalCount % limit == 0){
			totalPage = totalCount / limit;
		}
		else{
			totalPage = totalCount/limit + 1;
		}
		pageBean.setTotalPage(totalPage);
		//设置显示到页面的数据集合:
		int begin=(page -1)*limit;
		List<Product> list = productDao.findByPage(begin,limit);
		pageBean.setList(list);
		return pageBean;
	}

四、ProductDao.java:

    //DAO层统计商品个数的方法
	public int findCount() {
		String hql="select count(*) from Product";
		List<Long> list = this.getHibernateTemplate().find(hql);
		if(list !=null && list.size()>0){
			return list.get(0).intValue(); 
		}
		return 0;
	}
	
	public List<Product> findByPage(int begin, int limit) {
		String hql= "from Product order by pdate desc";
		List<Product> list=this.getHibernateTemplate().execute(new PageHibernateCallback<Product>(hql, null, begin, limit));
		if(list !=null && list.size()>0){
			return list;  
		}
		return null;
	}

猜你喜欢

转载自blog.csdn.net/sz15732624895/article/details/81057262