通用hql分页

basedao
获取查询符合条件的总记录数的hql语句
传进来hql语句的命名参数是没有赋值,给命名参数赋值
在通用的查询方法中给Query对象分页
query.setfirstResult
query.setMaxResult

	super.executeQuery(....);
	
在hibernate中使用原生sql语句,查的是原生sql,返回OBject[]

basedao

package com.zking.eight.dao;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.hibernate.Session;
import org.hibernate.query.Query;

/**
 * 处理hql通用查询问题
 * jdbc
 * sql,pageBean,Class,
 * 
 * @author Administrator
 *
 *hql:
 *1.hql传过来,拼接countHql
 *2.给命名参数赋值
 *3.pagebean.settotal
 *4.查询所需要的结果集
 */


/**
 * 给命名参数赋值
 * @author Administrator
 *
 */
public class BaseDao {
	private void setParemeters(Query query,Map<String, Object> map) {
		if (map==null || map.size()==0) return;
		Object value;
		for(Map.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传过来,拼接countHql
	 * 用来查询符合条件的所有记录数
	 * @param hql
	 * @return
	 */
	 private String getCountHql(String hql) {
		int i = hql.toUpperCase().indexOf("from");
		
		return "select count(*) "+hql.substring(i);
	}
	 /**
	  * 通用查询
	  * @param hql
	  * @param session
	  * @param map
	  * @param pageBean
	  * @return
	  */
	 protected List executeQuery(String hql,Session session,Map<String, Object> map,PageBean pageBean) {
		 if (pageBean!=null && pageBean.isPagination()) {
			 String countHql = getCountHql(hql);
			 Query createQuery = session.createQuery(countHql);
			 this.setParemeters(createQuery, map);
			 pageBean.setTotal(createQuery.getSingleResult().toString());
			 Query query = session.createQuery(hql);
			 this.setParemeters(query, map);
			 query.setFirstResult(pageBean.getStartIndex());
			 query.setMaxResults(pageBean.getRows());
			 return query.list();
		}else{
			Query query = session.createQuery(hql);
			this.setParemeters(query, map);
			return query.list();
		}
		
		 
	 }
	
}

PageBean :

package com.zking.eight.dao;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

/**
 * 分页工具类
 *
 */
public class PageBean {

	private int page = 1;// 页码

	private int rows = 10;// 页大小

	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 + "]";
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值