package com.zking.util;
import org.hibernate.Session;
import org.hibernate.query.Query;
import java.util.Collection;
import java.util.List;
import java.util.Map;
/**
-
hql的通用查询
-
sql Pagebean Class clz
-
1.hql语句传过来拼接Countsql
-
2.给命名参数赋值
-
3.pageBean.settotal
-
4.查询你所需要的结果集
/
public class BaseDao {
/*- 给命名参数赋值
- @param query
- @param map
*/
private void setParameters(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
/
public String getCountSql(String hql) {
int index = hql.toUpperCase().indexOf(“FROM”);
return "select count() " + hql.substring(index);
}
/**
- 通用查询
- @param session
- @param hql
- @param map 含有命名参数的集合
- @param pageBean
- @return
*/
protected List executeQuery(Session session, String hql, Map<String, Object> map, PageBean pageBean) {
if (pageBean != null && pageBean.isPagination()) {
String countsql=getCountSql(hql);
Query countquery=session.createQuery(countsql);
this.setParameters(countquery,map);
pageBean.setTotal(countquery.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 一个分页的类 在这里也把代码贴出来
package com.zking.util;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
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;//所有参数集合
/**
*
* @Title: setPageBean
* @Description: (初始化对象的方法)
* @param request
* @return void
*/
public void setPageBean(HttpServletRequest request) {
this.page=request.getParameter("page")==null?1:Integer.parseInt(request.getParameter("page"));
this.rows=request.getParameter("rows")==null?5:Integer.parseInt(request.getParameter("rows"));
// 只有jsp页面上填写pagination=false才是不分页
this.setPagination(!"false".equals(request.getParameter("pagination")));
this.setParameterMap(request.getParameterMap());
this.setUrl(request.getRequestURL().toString());
}
/**
*
* @Title: getMaxPages
* @Description: (最大页码数)
* @return
* @return int
*/
public int getMaxPages() {
return this.total%this.rows==0?this.total/this.rows:this.total/this.rows+1;
}
/**
*
* @Title: getNextPages
* @Description: (下一页页码)
* @return
* @return int
*/
public int getNextPages() {
return this.page<getMaxPages()?this.page+1:getMaxPages();
}
/**
*
* @Title: getUpPages
* @Description: (上一页)
* @return
* @return int
*/
public int getUpPages() {
return this.page-1>0?this.page-1:1;
}
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 PageBean() {
super();
// TODO Auto-generated constructor stub
}
public PageBean(int page, int rows, int total, boolean pagination) {
super();
this.page = page;
this.rows = rows;
this.total = total;
this.pagination = pagination;
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public int getRows() {
return rows;
}
public void setRows(int rows) {
this.rows = 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;
}
/**
* 获得起始记录的下标
*
* @return
*/
public int getStartIndex() {
return (this.page - 1) * this.rows;
}
@Override
public String toString() {
return "PageBean [page=" + page + ", rows=" + rows + ", total=" + total + ", pagination=" + pagination + "]";
}
}
作者:yjt520557
来源:CSDN
原文:https://blog.csdn.net/yjt520557/article/details/84672086
版权声明:本文为博主原创文章,转载请附上博文链接!