7、客户模块-客户模块之分页后台代码的编写(分页)

 

/crm/src/com/louis/domain/PageBean.java

package com.louis.domain;

import java.util.List;

/**
 * 分页的JavaBean
 * @author Administrator
 */
public class PageBean<T> {
    
    // 当前页
    private int pageCode;
    
    // 总页数
    // private int totalPage;
    
    // 总记录数
    private int totalCount;
    // 每页显示的记录条数
    private int pageSize;
    // 每页显示的数据
    private List<T> beanList;
    
    public int getPageCode() {
        return pageCode;
    }
    public void setPageCode(int pageCode) {
        this.pageCode = pageCode;
    }
    
    /**
     * 调用getTotalPage() 获取到总页数
     * JavaBean的属性规定:totalPage是JavaBean是属性 ${pageBean.totalPage}
     * @return
     */
    public int getTotalPage() {
        // 计算
        int totalPage = totalCount / pageSize;
        // 说明整除
        if(totalCount % pageSize == 0){
            return totalPage;
        }else{
            return totalPage + 1;
        }
    }
    
    /*public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }*/
    
    public int getTotalCount() {
        return totalCount;
    }
    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }
    public int getPageSize() {
        return pageSize;
    }
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
    public List<T> getBeanList() {
        return beanList;
    }
    public void setBeanList(List<T> beanList) {
        this.beanList = beanList;
    }
}

 /crm/WebContent/menu.jsp

/crm/src/com/louis/web/action/CustomerAction.java

// 属性驱动的方式
    // 当前页,默认值就是1
    private Integer pageCode = 1;

    public void setPageCode(Integer pageCode) {
        if (pageCode == null) {
            pageCode = 1;
        }
        this.pageCode = pageCode;
    }

    // 每页显示的数据的条数
    private Integer pageSize = 2;

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }
    /**
     * 分页的查询方法
     * @return
     */
    public String findByPage(){
        // 调用service业务层
        DetachedCriteria criteria = DetachedCriteria.forClass(Customer.class);
        // 查询
        PageBean<Customer> page = customerService.findByPage(pageCode,pageSize,criteria);
        // 压栈
        ValueStack vs = ActionContext.getContext().getValueStack();
        // 栈顶是map<"page",page对象>
        vs.set("page", page);
        return "page";
    }

/crm/src/com/louis/service/CustomerServiceImpl.java

/**
     * 分页查询
     */
    public PageBean<Customer> findByPage(Integer pageCode, Integer pageSize, DetachedCriteria criteria) {
        return customerDao.findByPage(pageCode,pageSize,criteria);
    }

/crm/src/com/louis/dao/CustomerDaoImpl.java

    /**
     * 分页的查询
     */
    public PageBean<Customer> findByPage(Integer pageCode, Integer pageSize, DetachedCriteria criteria) {
        PageBean<Customer> page = new PageBean<Customer>();
        page.setPageCode(pageCode);
        page.setPageSize(pageSize);
        
        // 先查询总记录数    select count(*)
        criteria.setProjection(Projections.rowCount());
        List<Number> list = (List<Number>) this.getHibernateTemplate().findByCriteria(criteria);
        if(list != null && list.size() > 0){
            int totalCount = list.get(0).intValue();
            // 总的记录数
            page.setTotalCount(totalCount);
        }
        
        // 强调:把select count(*) 先清空,变成  select * ...
        criteria.setProjection(null);
        
        // 提供分页的查询
        List<Customer> beanList = (List<Customer>) this.getHibernateTemplate().findByCriteria(criteria, (pageCode-1)*pageSize, pageSize);
        // 分页查询数据,每页显示的数据  使用limit
        page.setBeanList(beanList);
        
        return page;
    }
    

 

问题

看分页代码PageBean

属性驱动是什么意思

criteria

posted on 2017-10-22 12:05  Michael2397  阅读(281)  评论(0编辑  收藏  举报

导航