SSH 프로젝트02BaseDao는 다른 Dao에서 상속받을 수 있도록 페이지 나누기 기능 완료

13170 단어

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
            
        

        
        
    


    

    
    
        
    

    
    
        
            
            
            
            
            
            
            
            
        
    

    
    
        
        
    


    
    

    

    
    
        
    

    
        
    


    
    
        
    

    
        
    


    
    
        
    

    
        
    


좋은 웹페이지 즐겨찾기