日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

SSH2+Daoz项目中的分页查询

發布時間:2024/9/5 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SSH2+Daoz项目中的分页查询 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Page.java

Java代碼 ?
  • import?java.util.List;??
  • /**?
  • ?*?分頁Page類?
  • ?*?@author?zhxing?
  • ?*?
  • ?*?@param?<T>?
  • ?*/??
  • public?class?Page<T>{??
  • ????public?final?int?DEFAULT_PAGESIZE=?10;???//?每頁記錄數???
  • ????private?List<T>?result?=?null;//頁面數據??
  • ??????
  • ????private?int?totalRows;//總記錄數??
  • ????private?int?pageSize=DEFAULT_PAGESIZE;//每頁顯示行數??
  • ????private?int?currentPage;//當前頁數??
  • ????private?int?totalPages;//總頁數??
  • ????private?int?startRow;//查詢開始的記錄數??
  • ??????
  • ??????
  • ????public?Page(){??
  • ??????????
  • ????}??
  • ????public?Page(int?totalRows){??
  • ????????//初始化總記錄數??
  • ????????this.totalRows=totalRows;??
  • ????????//初始化總頁數??
  • ????????this.totalPages=totalRows/pageSize;??
  • ????????if(totalRows%pageSize!=0){??
  • ????????????totalPages++;??
  • ????????}??
  • ????????//初始化當前頁數??
  • ????????this.currentPage=1;??
  • ????????//初始化查詢開始的記錄數??
  • ????????this.startRow=0;??????????
  • ????}??
  • ??
  • ??
  • ????//?頁內的數據列表.??
  • ??
  • ????public?List<T>?getResult()?{??
  • ????????return?result;??
  • ????}??
  • ??
  • ????public?void?setResult(List<T>?result)?{??
  • ????????this.result?=?result;??
  • ????}??
  • ??
  • ?????//?獲得上一頁??
  • ??
  • ????public?void?previous(){??
  • ????????if(currentPage==1)??
  • ????????????return;??
  • ????????currentPage--;??
  • ????????startRow=(currentPage-1)*pageSize;??
  • ????}??
  • ????//獲得下一頁??
  • ????public?void?next(){??
  • ????????if(currentPage==totalPages){??
  • ????????????return;??
  • ????????}??
  • ????????currentPage++;??
  • ????????startRow=(currentPage+1)*pageSize;??
  • ????}??
  • ????public?int?getCurrentPage()?{??
  • ????????return?currentPage;??
  • ????}??
  • ????//設置當前頁??
  • ????public?void?setCurrentPage(int?currentPage)?{??
  • ??
  • ????????if(currentPage>=totalPages){??
  • ????????????if(totalPages==0)??
  • ????????????????this.currentPage=1;??
  • ????????????else??
  • ????????????????this.currentPage=totalPages;??
  • ????????}??
  • ????????if(1<currentPage&&currentPage<totalPages){??
  • ????????????this.currentPage=currentPage;??
  • ????????}??
  • ????????if(currentPage<1){??
  • ????????????this.currentPage=1;??
  • ????????}??
  • ??????????????
  • ????}?????
  • ????public?int?getStartRow()?{??
  • ????????startRow=(currentPage-1)*pageSize;??
  • ????????return?startRow;??
  • ????}??
  • ????public?void?setStartRow(int?startRow)?{??
  • ????????this.startRow?=?startRow;??
  • ????}??
  • ????public?int?getTotalRows()?{??
  • ????????return?totalRows;??
  • ????}??
  • ????public?void?setTotalRows(int?totalRows)?{??
  • ????????this.totalRows?=?totalRows;??
  • ????}??
  • ????public?int?getTotalPages()?{??
  • ????????return?totalPages;??
  • ????}??
  • ????public?int?getPageSize()?{??
  • ????????return?pageSize;??
  • ????}??
  • ????public?boolean?isStart()?{??
  • ????????return?currentPage==1;??
  • ????}??
  • ????public?boolean?isEnd()?{??
  • ????????return?currentPage==totalPages||totalPages==0;??
  • ????}??
  • ??
  • }??
  • import java.util.List; /*** 分頁Page類* @author zhxing** @param <T>*/ public class Page<T>{public final int DEFAULT_PAGESIZE= 10; // 每頁記錄數 private List<T> result = null;//頁面數據private int totalRows;//總記錄數private int pageSize=DEFAULT_PAGESIZE;//每頁顯示行數private int currentPage;//當前頁數private int totalPages;//總頁數private int startRow;//查詢開始的記錄數public Page(){}public Page(int totalRows){//初始化總記錄數this.totalRows=totalRows;//初始化總頁數this.totalPages=totalRows/pageSize;if(totalRows%pageSize!=0){totalPages++;}//初始化當前頁數this.currentPage=1;//初始化查詢開始的記錄數this.startRow=0; }// 頁內的數據列表.public List<T> getResult() {return result;}public void setResult(List<T> result) {this.result = result;}// 獲得上一頁public void previous(){if(currentPage==1)return;currentPage--;startRow=(currentPage-1)*pageSize;}//獲得下一頁public void next(){if(currentPage==totalPages){return;}currentPage++;startRow=(currentPage+1)*pageSize;}public int getCurrentPage() {return currentPage;}//設置當前頁public void setCurrentPage(int currentPage) {if(currentPage>=totalPages){if(totalPages==0)this.currentPage=1;elsethis.currentPage=totalPages;}if(1<currentPage&&currentPage<totalPages){this.currentPage=currentPage;}if(currentPage<1){this.currentPage=1;}} public int getStartRow() {startRow=(currentPage-1)*pageSize;return startRow;}public void setStartRow(int startRow) {this.startRow = startRow;}public int getTotalRows() {return totalRows;}public void setTotalRows(int totalRows) {this.totalRows = totalRows;}public int getTotalPages() {return totalPages;}public int getPageSize() {return pageSize;}public boolean isStart() {return currentPage==1;}public boolean isEnd() {return currentPage==totalPages||totalPages==0;}}

    ?BaseDao.java

    ?

    Java代碼 ?
  • import?java.io.Serializable;??
  • import?java.util.List;??
  • ??
  • public?interface?BaseDao<T,ID?extends?Serializable>?{??
  • ????/**?
  • ?????*?保存實體?
  • ?????*?@param?entity?實體類?
  • ?????*/??
  • ????public?void?save(T?entity);??
  • ????/**?
  • ?????*?刪除實體?
  • ?????*?@param?entity?實體類?
  • ?????*/??
  • ????public?void?delete(T?entity);??
  • ????/**?
  • ?????*?根據實體id?刪除實體?
  • ?????*?@param?entityClass??實體類?
  • ?????*?@param?id??實體id?
  • ?????*/??
  • ????public?void?deleteById(Class<T>?entityClass,ID?id);??
  • ????/**?
  • ?????*?更新實體?
  • ?????*?@param?entity??實體類?
  • ?????*/??
  • ????public?void?update(T?entity);??
  • ????/**?
  • ?????*?根據實體id?查詢單個實體?
  • ?????*?@param?entityClass?實體類?
  • ?????*?@param?id?實體id?
  • ?????*?@return?
  • ?????*/??
  • ????public?T?findById(Class<T>?entityClass,ID?id);??
  • ????/**?
  • ?????*?列出所有實體集合?
  • ?????*?@param?entityClass?實體類?
  • ?????*?@return?實體類List?
  • ?????*/??
  • ????public?List<T>?findAll(Class<T>?entityClass);??
  • ????/**?
  • ?????*?根據實體參數,查詢符合條件的實體類集合?
  • ?????*?@param?hql??
  • ?????*?@param?values?參數?
  • ?????*?@return?
  • ?????*/??
  • ????public?List<Object>?find(String?hql,?Object...?values);??
  • ??
  • ????/**?
  • ?????*?根據hql?語句,返回Page?類?
  • ?????*?@param?page?Page類?
  • ?????*?@param?hql?@param?hql?
  • ?????????????????*?@param?currentPage?當前頁碼?
  • ?????*?@return?
  • */??
  • ????public?Page<T>?findByPage(final?String?hql,final?String?countHql,final?int?currentPage);??
  • ??????
  • }??
  • import java.io.Serializable; import java.util.List;public interface BaseDao<T,ID extends Serializable> {/*** 保存實體* @param entity 實體類*/public void save(T entity);/*** 刪除實體* @param entity 實體類*/public void delete(T entity);/*** 根據實體id 刪除實體* @param entityClass 實體類* @param id 實體id*/public void deleteById(Class<T> entityClass,ID id);/*** 更新實體* @param entity 實體類*/public void update(T entity);/*** 根據實體id 查詢單個實體* @param entityClass 實體類* @param id 實體id* @return*/public T findById(Class<T> entityClass,ID id);/*** 列出所有實體集合* @param entityClass 實體類* @return 實體類List*/public List<T> findAll(Class<T> entityClass);/*** 根據實體參數,查詢符合條件的實體類集合* @param hql * @param values 參數* @return*/public List<Object> find(String hql, Object... values);/*** 根據hql 語句,返回Page 類* @param page Page類* @param hql @param hql* @param currentPage 當前頁碼* @return */public Page<T> findByPage(final String hql,final String countHql,final int currentPage);}

    ?

    BaseHibernateDao.java

    ?

    Java代碼 ?
  • import?java.io.Serializable;??
  • import?java.sql.SQLException;??
  • import?java.util.List;??
  • ??
  • import?org.hibernate.HibernateException;??
  • import?org.hibernate.Query;??
  • import?org.hibernate.Session;??
  • import?org.springframework.orm.hibernate3.HibernateCallback;??
  • import?org.springframework.orm.hibernate3.support.HibernateDaoSupport;??
  • ??
  • public?class?BaseHibernateDao<T,ID?extends?Serializable>?extends?HibernateDaoSupport?implements?BaseDao<T,ID>?{??
  • ??
  • ????@Override??
  • ????public?void?delete(T?entity)?{??
  • ????????this.getHibernateTemplate().delete(entity);??
  • ??????????
  • ????}??
  • ??
  • ????@Override??
  • ????public?void?deleteById(Class<T>?entityClass,?ID?id)?{??
  • ????????delete(this.findById(entityClass,?id));??
  • ??????????
  • ????}??
  • ??
  • ????@Override??
  • ????public?T?findById(Class<T>?entityClass,?ID?id)?{??
  • ????????return?(T)this.getHibernateTemplate().get(entityClass,?id);??
  • ????}??
  • ??
  • ????@Override??
  • ????public?List<T>?findAll(Class<T>?entityClass)?{??
  • ????????String?name=entityClass.getName();??
  • ????????return?this.getHibernateTemplate().find("from"+name);??
  • ????}??
  • ??
  • ????@Override??
  • ????public?void?save(T?entity)?{??
  • ????????this.getHibernateTemplate().save(entity);??
  • ????}??
  • ??
  • ????@Override??
  • ????public?void?update(T?entity)?{??
  • ????????this.getHibernateTemplate().update(entity);??
  • ????}??
  • ??
  • ????@Override??
  • ????public?List<Object>?find(String?hql,?Object...?values)?{??
  • ????????return?this.getHibernateTemplate().find(hql,values);??
  • ????}??
  • ??
  • ????@Override??
  • ????public?Page<T>?findByPage(final?String?hql,final?String?countHql,final?int?currentPage)?{??
  • ????????//?TODO?Auto-generated?method?stub??
  • ????????return?(Page<T>)this.getHibernateTemplate().execute(new?HibernateCallback(){??
  • ????????????@Override??
  • ????????????public?Object?doInHibernate(Session?session)??
  • ????????????????????throws?HibernateException,?SQLException?{??
  • ????????????????//初始化Page??
  • ????????????????Page<T>?page=new?Page<T>(getCount(session,?countHql));??
  • ????????????????page.setCurrentPage(currentPage);??
  • ???????????????????????????????????????????//分頁查詢開始??
  • ????????????????Query?query=session.createQuery(hql);??
  • ????????????????query.setFirstResult(page.getStartRow());??
  • ????????????????query.setMaxResults(page.getPageSize());??
  • ????????????????//把取得的實體list設置到Page?類中??
  • ????????????????page.setResult(query.list());??
  • ????????????????return?page;??
  • ????????????}??
  • ????????????//獲取總記錄數??
  • ????????????private?int?getCount(Session?session,String?countHql){??
  • ????????????????Long?count=0L;??
  • ????????????????List?list=session.createQuery(countHql).list();??
  • ????????????????if(list!=null&&list.size()==1)??
  • ????????????????????count=(Long)list.get(0);??
  • ????????????????return?count.intValue();??
  • ????????????}??
  • ????????});??
  • ????}??
  • ??
  • ??
  • }?
  • 轉載于:https://www.cnblogs.com/sailormoon/archive/2012/12/20/2827024.html

    總結

    以上是生活随笔為你收集整理的SSH2+Daoz项目中的分页查询的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。