ssh 分頁查詢
先寫公用類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>
相關文章
- 【記錄】SSH分頁查詢功能
- SSH框架下的分頁查詢框架
- ssh框架中的分頁查詢框架
- SSH整合實現分頁查詢(兩種方式)
- 在SSH框架下按條件分頁查詢框架
- Elasticsearch 分頁查詢Elasticsearch
- oracle分頁查詢Oracle
- 使用SSH完成條件及分頁查詢的主要程式碼
- MySQL的分頁查詢MySql
- ThinkPhp框架:分頁查詢PHP框架
- 分頁查詢優化優化
- NET 集合分頁查詢
- MySQL 多表查詢分頁MySql
- 分頁查詢重構
- Oracle的分頁查詢Oracle
- Oracle分頁查詢格式Oracle
- Lucene的分頁查詢
- SSH:查詢
- java-用SSH框架實現將查詢的資訊分頁顯示Java框架
- javaee導測(ssh框架)分頁 mysql資料庫(帶條件查詢)Java框架MySql資料庫
- elasticsearch查詢之大資料集分頁查詢Elasticsearch大資料
- 分庫分表後的分頁查詢
- indexdb實現分頁查詢Index
- AntDesignBlazor示例——分頁查詢Blazor
- MySQL分頁查詢優化MySql優化
- Oracle分頁查詢格式(十三)Oracle
- Oracle分頁查詢格式(十二)Oracle
- Oracle分頁查詢格式(十一)Oracle
- Oracle分頁查詢格式(八)Oracle
- Oracle分頁查詢格式(九)Oracle
- Oracle分頁查詢格式(十)Oracle
- oracle分頁查詢語句Oracle
- Oracle分頁查詢之order byOracle
- oracle sql分頁查詢(一)OracleSQL
- 分頁查詢初接觸
- 關於批次分頁查詢
- 批次分頁查詢問題?
- Oracle總結【SQL細節、多表查詢、分組查詢、分頁】OracleSQL