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(); } } }
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 + "]"; } }
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); } }
原文链接:https://blog.csdn.net/zimuliusu/article/details/83547842