分页查询主要就是对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;
}
}