天天看點

page分頁

首先封裝一個分頁類

public class Page<T> {
    /**
     * 目前頁号
     */
    private int pageNumber;
    /**
     * 總條數
     */
    private int totalCount;
    /**
     * 總頁數
     */
    private int totalPage;
    /**
     * 每頁顯示的數量
     */
    private int pageSize;
    /**
     * 目前頁的資料
     */
    private List<T> item;
    
    
    /**
     * 目前起始行号
     * @return
     */
    private int from;
    
    
    
    
    public Page(){}
    
    public Page(int pageSize,int totalCount,String pageNumber) {
        setPageSize(pageSize);
        setTotalCount(totalCount);
        setPageNumber(pageNumber);
    }
    
    
    
    
    
    
    
    
    
    public int getPageNumber() {
        return pageNumber;
    }
    public void setPageNumber(String pageNumber) {
        
        if(StringUtil.isNumber(pageNumber)){
            int tempNumber = Integer.parseInt(pageNumber);
            if(tempNumber < 1){
                tempNumber = 1;
            }else if(tempNumber > getTotalPage()){
                tempNumber = getTotalPage();
            }
            
            this.pageNumber = tempNumber;
            
        }else{
            this.pageNumber = 1;
        }
        
    }
    public int getTotalCount() {
        return totalCount;
    }
    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
        
        
        this.totalPage = totalCount / getPageSize();
        if(totalCount % getPageSize() != 0) {
            this.totalPage++;
        }
        
    }
    public int getTotalPage() {
        return totalPage;
    }
    /*public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }*/
    public int getPageSize() {
        return pageSize;
    }
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
    public List<T> getItem() {
        return item;
    }
    public void setItem(List<T> item) {
        this.item = item;
    }
    public int getFrom() {
        return (getPageNumber() - 1) * getPageSize();
    }
    public void setForm(int from) {
        this.from = from;
    }
    
    
    
    
    
    public List<Integer> getSlider(){
        
        int startPageNumber = getPageNumber() - 2;
        int endPageNumber = getPageNumber() + 2;
        
        if(startPageNumber <= 0){
            endPageNumber -= startPageNumber - 1;
            startPageNumber = 1;
        }
        
        if(endPageNumber > getTotalPage()){
            int temp = getTotalPage() - endPageNumber;
            endPageNumber = getTotalPage();
            startPageNumber = startPageNumber + temp;
            if(startPageNumber < 1){
                startPageNumber = 1;
            }
        }
        
        
        List<Integer> result = new ArrayList<Integer>();
        
        for(int i = startPageNumber;i <= endPageNumber;i++){
            result.add(i);
        }
        
        
        
        
        
        return result;
    }
    
    
    
    
    
    
    
    
}      

controller中

Page<Active> page = new ActiveService().findByProjectid(projectid,pageNo);      

service中

public Page<Active> findByProjectid(String projectid, String pageNo) {
        
        return activeDao.findAllByidAndPageNo(Long.valueOf(projectid),pageNo);
    }      

dao中

public Page<Active> findAllByidAndPageNo(Long projectid, String pageNo) {
        
        
        String sql = "SELECT * FROM active,account WHERE active.accountid = account.id AND projectid = ? order by active.id desc limit ?,?";
        
        Page<Active> page = new Page<Active>(5, findActiveCountByprojectid(projectid), pageNo);
        //因為傳回的page中的item裡是沒有account資訊的,但是在home.jsp中需要顯示account的頭像和姓名,是以要重寫這個rowmapper
        List<Active> list = db.queryForList(sql, new RowMapper<Active>(){

            @Override
            public Active mapRow(ResultSet rs) throws SQLException {
                Active active = new Active();
                active.setAccountid(rs.getLong("accountid"));
                active.setContext(rs.getString("context"));
                active.setCreatetime(rs.getTimestamp("createtime"));
                active.setId(rs.getLong("id"));
                active.setProjectid(rs.getLong("projectid"));
                active.setType(rs.getString("type"));
                
                
                Account account = new Account();
                account.setId(rs.getLong("accountId"));
                account.setUsername(rs.getString("username"));
                account.setPic(rs.getString("pic"));
                
                active.setAccount(account);
                return active;
            }
            
            
            
            
            
            
        }, projectid,page.getFrom(),page.getPageSize());
        
        
        
        page.setItem(list);
        
        
        
        
        
        return page;
    }
    private int findActiveCountByprojectid(Long projectid) {
        String sql = "select count(*) from active where projectid = ?";
        return db.queryForCount(sql, projectid).intValue();
    }      
page分頁