SSH项目_02BaseDao完成分页功能供其他Dao继承

1. BaseDao的抽取和通用功能的实现

1.1 BaseDao

public interface BaseDao {

    //增
    void save(T t);

    //删
    void delete(T t);

    //删
    void delete(Serializable id);

    //改
    void update(T t);

    //查 根据id查询
    T getById(Serializable id);

    //查 符合条件的总记录数
    Integer getTotalCount(DetachedCriteria dc);

    //查 查询分页列表数据
    List getPageList(DetachedCriteria dc, Integer start, Integer pageSize);
}

1.1 BaseDaoImpl

public class BaseDaoImpl extends HibernateDaoSupport implements BaseDao {

    private Class clazz;

    public BaseDaoImpl() {

        //获得当前类型的带有泛型类型的父类
        ParameterizedType ptClass = (ParameterizedType) this.getClass().getGenericSuperclass();

        //获得运行期的泛型类型
        clazz = (Class) ptClass.getActualTypeArguments()[0];
    }

    @Override
    public void save(T t) {

        getHibernateTemplate().save(t);
    }

    @Override
    public void delete(T t) {

        getHibernateTemplate().delete(t);
    }

    @Override
    public void delete(Serializable id) {

        getHibernateTemplate().delete(getById(id));
    }

    @Override
    public void update(T t) {

        getHibernateTemplate().update(t);
    }

    @Override
    public T getById(Serializable id) {

        return (T) getHibernateTemplate().get(clazz, id);
    }

    @Override
    public Integer getTotalCount(DetachedCriteria dc) {

        //设置查询的聚合函数,总记录数
        dc.setProjection(Projections.rowCount());

        List list = (List) getHibernateTemplate().findByCriteria(dc);

        // 清空之前设置的聚合函数
        dc.setProjection(null);

        if (list != null && list.size() > 0) {

            Long count = list.get(0);

            return count.intValue();

        } else {

            return null;
        }
    }


    @Override
    public List getPageList(DetachedCriteria dc, Integer start, Integer pageSize) {

        List list = (List) getHibernateTemplate().findByCriteria(dc, start, pageSize);

        return list;
    }
}

2. 封装PageBean

作用:将页面传入的参数进行统一的判断处理,并且将查询到的分页数据list也存储进PageBean的对象中,方便进行重定向或者转发

public class PageBean {

    //当前页数
    private Integer currentPage;
    //总记录数
    private Integer totalCount;
    //每页显示条数
    private Integer pageSize;
    //总页数
    private Integer totalPage;
    //分页列表数据
    private List list;


    public PageBean(Integer currentPage, Integer totalCount, Integer pageSize) {

        this.currentPage = currentPage;
        this.totalCount = totalCount;
        this.pageSize = pageSize;


        //如页面没有指定显示那一页.显示第一页.
        if(this.currentPage == null){
            this.currentPage = 1;
        }


        //如果每页显示条数没有指定,默认每页显示3条
        if(this.pageSize == null){
            this.pageSize = 10;
        }

        //计算总页数
        this.totalPage = (this.totalCount+this.pageSize-1)/this.pageSize;

        //判断当前页数是否超出范围
        //不能小于1
        if(this.currentPage < 1){
            this.currentPage = 1;
        }

        //不能大于总页数
        if(this.currentPage > this.totalPage){
            this.currentPage = this.totalPage;
        }

    }


    //计算起始索引
    public int getStart(){
        return (this.currentPage-1)*this.pageSize;
    }



    public Integer getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(Integer currentPage) {
        this.currentPage = currentPage;
    }

    public Integer getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(Integer totalCount) {
        this.totalCount = totalCount;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(Integer totalPage) {
        this.totalPage = totalPage;
    }

    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }
}

3. 通过继承BaseDao和封装的PageBean完成客户列表的分页查询

3.1 CustomerAction处理请求

public class CustomerAction extends ActionSupport implements ModelDriven{

    private Customer customer = new Customer();

    private CustomerService customerService;
    private Integer currentPage;
    private Integer pageSize;


    public String list() throws Exception {

        //封装离线查询对象
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Customer.class);

        //判断并封装参数
        if (StringUtils.isNotBlank(customer.getCust_name())) {

            detachedCriteria.add(Restrictions.like("cust_name", "%"+customer.getCust_name()+"%"));
        }

        //1. 调用Service查询分页数据(PageBean)
        PageBean pageBean = customerService.getPageBean(detachedCriteria, currentPage, pageSize);

        //2. 将PageBean放入request域,转发到列表页面显示
        ActionContext.getContext().put("pageBean", pageBean);

        return "list";
    }



    public void setCustomerService(CustomerService customerService) {
        this.customerService = customerService;
    }

    public void setCurrentPage(Integer currentPage) {
        this.currentPage = currentPage;
    }

    public Integer getCurrentPage() {
        return currentPage;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    @Override
    public Customer getModel() {
        return customer;
    }
}

3.2 CustomerService和CustomerServiceImpl


public interface CustomerService {

    // 分页方法
    PageBean getPageBean(DetachedCriteria dc, Integer currentPage, Integer pageSize);

}

@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED, readOnly = true)
public class CustomerServiceImpl implements CustomerService {

    CustomerDao customerDao;


    @Override
    public PageBean getPageBean(DetachedCriteria dc, Integer currentPage, Integer pageSize) {

        //1 调用Dao查询总记录数
        Integer totalCount = customerDao.getTotalCount(dc);

        //2 创建PageBean对象
        PageBean pageBean = new PageBean(currentPage, totalCount, pageSize);

        //3 调用Dao查询分页列表数据
        List list = customerDao.getPageList(dc, pageBean.getStart(), pageBean.getPageSize());

        pageBean.setList(list);

        return pageBean;
    }



    public void setCustomerDao(CustomerDao customerDao) {
        this.customerDao = customerDao;
    }
}

3.3 通过继承BaseDao来实现CustomerDao

public interface CustomerDao extends BaseDao {
}
public class CustomerDaoImpl extends BaseDaoImpl implements CustomerDao {
}

4. 配置文件最终形态

4.1 struts.xml



    
    

    
    

    
    

        
            
        
        
        
            /index.htm
            /login.jsp
        

        
            /jsp/customer/list.jsp
        
    


4.2 applicationContext.xml



    
    

    
    
        
        
        
        
    



    

    
    
    

        
        
        
        
        
            
                
                org.hibernate.dialect.MySQLDialect

                
                true
                true
                update
            
        

        
        
    


    

    
    
        
    

    
    
        
            
            
            
            
            
            
            
            
        
    

    
    
        
        
    


    
    

    

    
    
        
    

    
        
    


    
    
        
    

    
        
    


    
    
        
    

    
        
    


你可能感兴趣的:(SSH项目_02BaseDao完成分页功能供其他Dao继承)