Hibernate通用分頁
阿新 • • 發佈:2018-11-03
- BaseDao
package com.hibernateSql.dao; import java.util.Collection; import java.util.List; import java.util.Map; import org.hibernate.Session; import org.hibernate.query.Query; import com.hibernateSql.util.PageBean; /** * jdbc: * executeQuery(pagebean,sql,clz) * sql: select * from book where book_name '%?%' * select * from book where book_name '%xx%' * * 分頁:1.sql-->countSql-->total-->pagebaen * 2.sql-->pagesql-->result * 3.處理結果集 * * hibername * 分頁: * 1.hql-->countHql-->total-->pagebaen * 2.Hql-->pageHql-->result * * @author Administrator * */ public class BaseDao { /** * 如果有帶引數, * 命名引數 :賦值 * * @param query * @param hql */ public void setParameters(Query<?> query,Map<String, Object> map) { if(map == null || map.size()==0) { return ; } else { //建立map的檢視 Object values=null; for (Map.Entry<String, Object> entry : map.entrySet()) { values=entry.getValue(); //判斷它的資料型別 if(values instanceof Collection) { query.setParameterList(entry.getKey(), (Collection) values); } else if(values instanceof Object[]) { query.setParameterList(entry.getKey(), (Object[]) values); } else { query.setParameter(entry.getKey(), values); } } } } /** * 拼接Hqlcount語句 */ public String getcountHql(String hql) { int index = hql.toUpperCase().indexOf("FROM"); return "select count(*) "+ hql.substring(index); } /** * */ public List<?> executeQuery(String hql,Map<String, Object> map,PageBean pageBean,Session session){ //判斷它是否分頁 if(pageBean != null && pageBean.isPagination()) { //如果分頁 String countHql = getcountHql(hql); //查詢出總頁數 Query<?> query = session.createQuery(countHql); //給這個countHql中的命名引數賦值 setParameters(query, map); String total = query.getSingleResult().toString(); //放入pageBeau pageBean.setTotal(total); //開始查詢資料 Query<?> pageQuery = session.createQuery(hql); //給命名引數賦值 this.setParameters(pageQuery, map); //設定分頁 pageQuery.setFirstResult(pageBean.getStartIndex()); pageQuery.setMaxResults(pageBean.getRows()); return pageQuery.list(); } else { //不分頁的時候 Query<?> query = session.createQuery(hql); //給引數賦值 setParameters(query, map); return query.list(); } } }
- PageBean
package com.hibernateSql.util; import java.util.Map; import javax.servlet.http.HttpServletRequest; public class PageBean { private int page = 1;// 頁碼 private int rows = 1;// 行數/頁大小 private int total = 0;// 總記錄數 private boolean pagination = true;// 預設分頁 private String url;// 上一次請求的地址 private Map<String, String[]> parameterMap;// 上一次請求的所有引數 public PageBean() { super(); } /** * 對分頁bean進行初始化 * * @param request */ public void setRequest(HttpServletRequest request) { // 公共引數 this.setPage(request.getParameter("page")); this.setRows(request.getParameter("rows")); this.setPagination(request.getParameter("pagination")); // 請求地址和請求引數 this.setUrl(request.getContextPath() + request.getServletPath()); this.setParameterMap(request.getParameterMap()); } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public Map<String, String[]> getParameterMap() { return parameterMap; } public void setParameterMap(Map<String, String[]> parameterMap) { this.parameterMap = parameterMap; } public int getPage() { return page; } public void setPage(int page) { this.page = page; } public void setPage(String page) { if (null != page && !"".equals(page.trim())) { this.page = Integer.parseInt(page); } } /** * 行數/頁大小 * @return */ public int getRows() { return rows; } public void setRows(int rows) { this.rows = rows; } public void setRows(String rows) { if (null != rows && !"".equals(rows.trim())) { this.rows = Integer.parseInt(rows); } } public int getTotal() { return total; } public void setTotal(int total) { this.total = total; } public void setTotal(String total) { this.total = Integer.parseInt(total); } public boolean isPagination() { return pagination; } public void setPagination(boolean pagination) { this.pagination = pagination; } public void setPagination(String pagination) { if ("false".equals(pagination)) { this.pagination = false; } } /** * 下一頁 * * @return */ public int getNextPage() { int nextPage = page + 1; if (nextPage > this.getMaxPage()) { nextPage = this.getMaxPage(); } return nextPage; } /** * 上一頁 * * @return */ public int getPreviousPage() { int previousPage = page - 1; if (previousPage < 1) { previousPage = 1; } return previousPage; } /** * 最大頁碼 * * @return */ public int getMaxPage() { return total % rows == 0 ? total / rows : total / rows + 1; } /** * 起始記錄的下標 * * @return */ public int getStartIndex() { return (page - 1) * rows; } @Override public String toString() { return "PageBean [page=" + page + ", rows=" + rows + ", total=" + total + ", pagination=" + pagination + "]"; } }
- StringUtils
package com.hibernateSql.util; public class StringUtils { // 私有的構造方法,保護此類不能在外部例項化 private StringUtils() { } /** * 如果字串等於null或去空格後等於"",則返回true,否則返回false * * @param s * @return */ public static boolean isBlank(String s) { boolean b = false; if (null == s || s.trim().equals("")) { b = true; } return b; } /** * 如果字串不等於null或去空格後不等於"",則返回true,否則返回false * * @param s * @return */ public static boolean isNotBlank(String s) { return !isBlank(s); } }