1. 程式人生 > >hibernate之通用分頁

hibernate之通用分頁

分頁原理(思路)
executeQuery(String sql,PageBean pageBean,Clazz clz);
sql: 1.將原生態的sql拼接成計算頁數的countSql,計算有多少頁資料
2.將原生態的sql拼接成分頁語句pageSql,得到查詢結果
3.處理結果集

hql: hql在傳過來的時候是沒有給引數賦值的
1.將原生態的hql拼接成計算頁數的countHql,計算有多少頁資料
2.將原生態的hql拼接成分頁語句pageHql,得到查詢結果(hibernate可以呼叫內建的分頁語句)
3.hibernate不用處理結果集

/**
	 * 給query中的變數賦值
	 * @param query 需要賦值的query物件
	 * @param map   key:需要賦值的變數名
	 * 				value: 對應的值
	 */
	private void setParamter(Query query,Map<String, Object> map) {
		if(map == null || map.size() == 0) {
			return ;//結束程式
		}else {
			Object value = null;
			//給query賦值
			for (Entry<String, Object> entry : map.entrySet()) {
				value = entry.getValue();
				//如果獲得引數為集合
				if(value instanceof Collection) {
					query.setParameterList(entry.getKey(), (Collection) value);
				}else if (value instanceof Object[]){
					query.setParameterList(entry.getKey(), (Object[]) value);
				}else {
					query.setParameter(entry.getKey(), value);
				}
			}
		}
		
	}
	
/**
	 * 獲得查詢條數的hql
	 * @param hql
	 * @return
	 */
	private String getCountSql(String hql) {
		int index = hql.toUpperCase().indexOf("FROM");
		return "select count(*) " + hql.substring(index);
	}
/**
	 * 通用查詢
	 * @param hql 查詢語句
	 * @param pageBean 分頁資料
	 * @param map 查詢條件
	 * @param session 
	 * @return
	 */
	public List executeQuery(String hql,PageBean pageBean,Map<String, Object> map,Session session) {
		if(pageBean != null && pageBean.isPagination()) {
			//分頁
			String countHql = this.getCountSql(hql);
			Query countQuery = session.createQuery(countHql);//查詢total總條數
			this.setParamter(countQuery, map);
			System.out.println("countHql"+countHql);
			List<Long> t = countQuery.list();
//			countQuery.uniqueResult();獲取單個
			
			Long total = t.get(0);
			pageBean.setTotal(total+"");
			
			
			Query pageQuery = session.createQuery(hql);
			//給引數設值
			this.setParamter(pageQuery, map);
			pageQuery.setFirstResult(pageBean.getStartIndex());
			pageQuery.setMaxResults(pageBean.getRows());
			
			return pageQuery.list();
		}else {
			//不分頁
			Query query = session.createQuery(hql);
			//給引數設值
			this.setParamter(query, map);
			return query.list();
		}
	}

幫助類
PageBean

public class PageBean {

	private int page = 1;// 頁碼

	private int rows = 3;// 頁大小

	private int total = 0;// 總記錄數

	private boolean pagination = true;// 是否分頁
	// 獲取前臺向後臺提交的所有引數
	private Map<String, String[]> parameterMap;
	// 獲取上一次訪問後臺的url
	private String url;

	/**
	 * 初始化pagebean
	 * 
	 * @param req
	 */
	public void setRequest(HttpServletRequest req) {
		this.setPage(req.getParameter("page"));
		this.setRows(req.getParameter("rows"));
		// 只有jsp頁面上填寫pagination=false才是不分頁
		this.setPagination(!"fasle".equals(req.getParameter("pagination")));
		this.setParameterMap(req.getParameterMap());
		this.setUrl(req.getRequestURL().toString());
	}

	public int getMaxPage() {
		return this.total % this.rows == 0 ? this.total / this.rows : this.total / this.rows + 1;
	}

	public int nextPage() {
		return this.page < this.getMaxPage() ? this.page + 1 : this.getMaxPage();
	}

	public int previousPage() {
		return this.page > 1 ? this.page - 1 : 1;
	}

	public PageBean() {
		super();
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public void setPage(String page) {
		this.page = StringUtils.isBlank(page) ? this.page : Integer.valueOf(page);
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public void setRows(String rows) {
		this.rows = StringUtils.isBlank(rows) ? this.rows : Integer.valueOf(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 Map<String, String[]> getParameterMap() {
		return parameterMap;
	}

	public void setParameterMap(Map<String, String[]> parameterMap) {
		this.parameterMap = parameterMap;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	/**
	 * 獲得起始記錄的下標
	 * 
	 * @return
	 */
	public int getStartIndex() {
		return (this.page - 1) * this.rows;
	}

	@Override
	public String toString() {
		return "PageBean [page=" + page + ", rows=" + rows + ", total=" + total + ", pagination=" + pagination
				+ ", parameterMap=" + parameterMap + ", url=" + url + "]";
	}

StringUtils

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);
	}