HQL通用分页
HQL分页的思路:
- HQL传过来,拼接countHql
- 给命名参数赋值(hibernate 5.0 以后占位符?基本不用了)
- pageBean setTotal(设置符合条件的总页数)
- 查询你需要的结果集
命名参数赋值
/**
* 给命名参数赋值
*
* @param query 预定义参数
* @param map 接受参数
*/
public void setParameters(Query query, Map<String, Object> map) {
if (map == null || map.size() == 0)return;
Object values;
for (Map.Entry<String, Object> entry : map.entrySet()) {
values = entry.getValue();
if (values instanceof Collection) {
values = query.setParameterList(entry.getKey(), (Collection) values);
} else if (values instanceof Object[]) {
values = query.setParameterList(entry.getKey(), (Object[]) values);
} else {
values = query.setParameter(entry.getKey(), values);
}
}
}
接受sql,拼接countHql语句
/**
* 拼接HQL countHql
* FROM bas_dict where 1=1 bac_dict like :bsc_dict
* select count(*) ForM bas_dict where 1=1 bac_dict like :bsc_dict
* select bid,bname FROM bas_dict hwere 1=1 bac_dict like :bsc_dict
*
* @return
*/
private String getCountHql(String hql) {
// 获取from的下标
int indexOf = hql.toUpperCase().indexOf("FROM");
return "select count(*)" + hql.substring(indexOf);
}
注意:获取form的下标的作用是避免拼接你传过来的HQL是第三种类型的拼接会出错误
通用分页查询
/**
* 通用分页查询
*
* @return
*/
protected List executeQuery(Session session, String hql, Map<String, Object> map, PageBean pageBean) {
if (pageBean != null && pageBean.isPagination()) {
// 获取数据库中总的记录数
String countHql = getCountHql(hql);
Query createQuery = session.createQuery(countHql);
this.setParameters(createQuery, map);
pageBean.setTotal(createQuery.getSingleResult().toString());
// 开始分页
Query query = session.createQuery(hql);
this.setParameters(query, map);
query.setFirstResult(pageBean.getStartIndex());
query.setMaxResults(pageBean.getRows());
return query.list();
} else {
Query query = session.createQuery(hql);
this.setParameters(query, map);
return query.list();
}
}
pageBean类
public class PageBean {
private int page = 1;// 页码
private int rows = 10;// 行数/页大小
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);
}
}
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 + "]";
}
}
StringsUtils工具类
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);
}
}