ssh 分頁查詢

Java_funds發表於2018-01-30

先寫公用類PageBean泛型類


public class PageBean<T> {

//當前頁
private Integer currentPage;
//總記錄
private Integer totalCount;
//每頁條數
private Integer pageSize;
//總頁數
private Integer totalPage;
//開始位置
private Integer begin;
//list集合
private List<T> list;

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 Integer getBegin() { return begin; } public void setBegin(Integer begin) { this.begin = begin; } public List<T> getList() { return list; } public void setList(List<T> list) { this.list = list; } }

action方法中查詢所有資料

private Integer currentPage;  get/set方法

 //分頁方法
public String listpage(){

  if(currentPage==null){
currentPage = 1;
}

  PageBean<User> pageBean = userService.listpage(currentPage);
//放到與物件裡面
request.setAttribute("pageBean",pageBean);
//放到與物件裡面
request.setAttribute("pageBean",pageBean);
return "listpage";
}



service中方法

//分頁

public PageBean<User> listpage(Integer currentPage) {
PageBean<User> pageBean = new PageBean<User>();

//當前頁
pageBean.setCurrentPage(currentPage);

//總記錄數  呼叫一個方法findCount()
int totalCount = userDao.findCount();
pageBean.setTotalCount(totalCount);

//meiye 條數
int pageSize= 3;

//總頁數
int totalPage = 0;
if(totalCount%pageSize==0){
totalPage = totalCount/pageSize;
}else{
totalPage = totalCount/pageSize+1;
} 
pageBean.setTotalPage(totalPage);
//開始位置
int begin = (currentPage-1)*pageSize;
//list集合
List<User> list = userDao.findAll(begin,pageSize);
pageBean.setList(list);
return pageBean;
}


dao層中

//總條數
@SuppressWarnings("all")
public int findCount() {
List<Object> list =(List<Object>) getSession().createQuery("select count(*) from User").list();
if(list!=null && list.size()>0){
Object obj = list.get(0);
Long log= (Long)obj;
int count = log.intValue();
return count;
}
return 0;
}


//反回list集合分頁查詢
@SuppressWarnings("all")
public List<User> findAll(int begin, int pageSize) {
/*//第一種方式
//得到工廠
SessionFactory sessionFactory = this.getHibernateTemplate().getSessionFactory();
Session session = sessionFactory.getCurrentSession();
Query query = session.createQuery("from User");
query.setFirstResult(begin);
query.setMaxResults(pageSize);
List<User> list = query.list();*/

//第二種、使用離線物件和hibernateTemplate實現
//建立離線物件 設定對哪個實體類操作
DetachedCriteria criteria = DetachedCriteria.forClass(User.class);
//呼叫模板方法

List<User> list = (List<User>)this.getHibernateTemplate().findByCriteria(criteria,begin,pageSize);

return list;
}

jsp頁面

 <div class="header-info">
                            共<span class="info-number">[${pageBean .totalCount}]</span>條結果,
                            分成<span class="info-number">[${pageBean.totalPage}]</span>頁顯示,
                            當前第<span class="info-number">[${pageBean.currentPage}]</span>頁.
           <c:if test="${pageBean.currentPage ne 1}">
           [<a href="${pageContext.request.contextPath }/user_listpage?currentPage=${pageBean.currentPage-1}">前一  頁</a>]   
        </c:if>
        <c:if test="${pageBean.currentPage ne pageBean.totalPage}">
           [<a href="${pageContext.request.contextPath }/user_listpage?currentPage=${pageBean.currentPage+1}">後一頁</a>]   
    </c:if>     

 </div>





相關文章