分页查询

本文详细介绍了一种基于PageBean的分页查询实现方法,包括Action、Service和DAO层的具体操作流程,以及如何通过PageBean对象封装分页信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

分页查询主要就是对PageBean的封装,PageBean的封装主要在service层,
Action中要封装页面传来的数据,把封装好的数据传到service层,要封装的数据两个来自页面,两个要从数据库中查询还有一个在有参变量中已经计算好了,要访问dao层查询总数量,以及该页的产品List集合

implements ModelDriven<要压栈的对象>

重写方法返回压栈对象,valueStack的栈顶的对象会在执行拦截器时为属性赋值,会先寻找栈顶有没有与name同名的属性,如果没有向下一个对象找,
PageBean
有5个属性,当前页面,总共多少页面,当前页面数量,总数,
当前页面显示内容的详细信息
有参构造可直接为成员变量赋值但是List要查到后赋值,如果页面没有这些信息,
那么也要规定,当前页面和页面条数并且体跳转页面时输入不存在的页面也会跳转到合理的页面,提供getStart方法获取每页的第一个的索引,当前页面确定,每页产品数确定就可以得到当前页面第一个索引

public class PageBean <T>{
	//当前页面,页面数
	private Integer currentPage;
	//一共有几页
	private Integer totalPage;
	//此页面显示的列表
	private List<T> list;
	//查询出的总数
	private Integer totalCount;
	//当前页面显示数量
	private Integer currentCount;
	
	public PageBean(Integer currentPage, Integer totalCount, Integer currentCount) {
		super();
		this.currentPage = currentPage;
		this.totalCount = totalCount;
		this.currentCount = currentCount;
		if(this.currentCount==null){
			this.currentCount=3;
		}
		this.totalPage=(this.totalCount+this.currentCount-1)/this.currentCount;
		if(this.currentPage==null){
			this.currentPage=1;
		}
		if(this.totalPage<1){
			this.currentPage=1;
		}
		if(this.currentPage>this.totalPage){
			this.currentPage=this.totalPage;
		}
	}
	public Integer getStart(){
		return (this.currentPage-1)*this.currentCount;
	}
	/**
	 * @return the currentPage
	 */
	public Integer getCurrentPage() {
		return currentPage;
	}
	/**
	 * @param currentPage the currentPage to set
	 */
	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage;
	}
	/**
	 * @return the totalPage
	 */
	public Integer getTotalPage() {
		return totalPage;
	}
	/**
	 * @param totalPage the totalPage to set
	 */
	public void setTotalPage(Integer totalPage) {
		this.totalPage = totalPage;
	}
	/**
	 * @return the list
	 */
	public List<T> getList() {
		return list;
	}
	/**
	 * @param list the list to set
	 */
	public void setList(List<T> list) {
		this.list = list;
	}
	/**
	 * @return the totalCount
	 */
	public Integer getTotalCount() {
		return totalCount;
	}
	/**
	 * @param totalCount the totalCount to set
	 */
	public void setTotalCount(Integer totalCount) {
		this.totalCount = totalCount;
	}
	/**
	 * @return the currentCount
	 */
	public Integer getCurrentCount() {
		return currentCount;
	}
	/**
	 * @param currentCount the currentCount to set
	 */
	public void setCurrentCount(Integer currentCount) {
		this.currentCount = currentCount;
	}
	
}

public class CustomerAction extends ActionSupport implements ModelDriven<Customer>{
	private Customer customer=new Customer();//把对象压入栈顶
	//Action对象的属性
	private CustomerService customerService;
	private Integer currentCount;
	private Integer currentPage;
	//(PageBean需要页面传给它currentPage,currentCount,还需要查询条件)
	public String getCustomerList(){
		DetachedCriteria dc=DetachedCriteria.forClass(Customer.class);
		if(StringUtils.isNotBlank(customer.getCust_name())){
			dc.add(Restrictions.like("cust_name", "%"+customer.getCust_name()+"%"));
		}
		
		PageBean pageBean=customerService.getList(dc,currentPage,currentCount);
		//调用service层的方法,把数据传给service层
		//为什么使用dc因为可增强代码的可重复性,用离线对象,把他一层一层传给Dao层

		ActionContext.getContext().put("pageBean",pageBean);
		//把PageBean放入context中与放入Request域相同的作用
		return "List";//转发到list页面
	}
	/**
	 * @return the currentCount
	 */
	public Integer getCurrentCount() {
		return currentCount;
	}
	/**
	 * @return the currentPage
	 */
	public Integer getCurrentPage() {
		return currentPage;
	}
	@Override
	public Customer getModel() {
		
		return customer;
	}
	
	/**
	 * @return the customerService
	 */
	public CustomerService getCustomerService() {
		return customerService;
	}
	/**
	 * @param customerService the customerService to set
	 */
	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}
	/**
	 * @param currentCount the currentCount to set
	 */
	public void setCurrentCount(Integer currentCount) {
		this.currentCount = currentCount;
	}
	/**
	 * @param currentPage the currentPage to set
	 */
	public void setCurrentPage(Integer currentPage) {
		this.currentPage = currentPage;
	}
	
	
}

Srevice
把从上一层获得的和数据库查到的封装到PageBean对象中

public class CustomerServiceImpl implements CustomerService{
	private CustomerDao customerDao;
	@Override
	public PageBean getList(DetachedCriteria dc,Integer currentPage,Integer currentCount) {
		Integer totalCount=customerDao.getTotalCount(dc);
		PageBean pb=new PageBean(currentPage, totalCount, currentCount);
		List<Customer> list=customerDao.getCustomerList(dc,pb.getStart(),pb.getCurrentCount());
		pb.setList(list);
		return pb;
	}

}

Dao
查询总数与当前页面的内容List

public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {
//继承了这个类后可直接获得HibernateTemplate配置文件Spring中就不用配置//HibernateTemplate,直接为Dao注入SessionFactory对象即可(维护者session对象)
	@Override
	public Integer getTotalCount(DetachedCriteria dc) {
		dc.setProjection(Projections.rowCount());
		List<Long> list=getHibernateTemplate().findByCriteria(dc);
		dc.setProjection(null);
		if(list!=null&&list.size()>0){
			return list.get(0).intValue();
		}else{
		return null;
		}
	}

	@Override
	public List<Customer> getCustomerList(DetachedCriteria dc,Integer start,Integer currentCount) {
		
		List<Customer> list=getHibernateTemplate().findByCriteria(dc,start,currentCount); 
		return list;
	}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值