分页工具类PageInfo
package cn.hibernate.util; import java.util.List; import javax.persistence.criteria.CriteriaBuilder.In; /** * @Description:部门实体类 * @author:20155805邝家豪 * @date 2018年4月2日上午9:25:00 */ public class PageInfo<T> { public static final int PAGESIZE=3; private Integer count;//总记录数 private Integer pageIndex;//当前页号 public Integer getCount() { return count; } public void setCount(Integer count) { this.count = count; } public Integer getPageIndex() { return pageIndex; } public void setPageIndex(Integer pageIndex) { this.pageIndex = pageIndex; } public Integer getTotalPages() { if (this.count%this.PAGESIZE==0) { this.totalPages=this.count/this.PAGESIZE; } else { this.totalPages=this.count/this.PAGESIZE+1; } return this.totalPages; } public List<T> getPageList() { return pageList; } public void setPageList(List<T> pageList) { this.pageList = pageList; } public Integer getPrameterPageIndex() { this.prameterPageIndex = pageIndex-1; return prameterPageIndex; } public Integer getNextPageIndex() { this.nextPageIndex = pageIndex+1; return nextPageIndex; } private Integer totalPages;//总页数 private Integer prameterPageIndex;//上一页 private Integer nextPageIndex;//下一页 private List<T> pageList;//当前页的记录集合 }
1、Dao层用到的方法
//使用uniqueResult获取唯一结果,计算查询条件下的记录数 public Long obtainCount(String hql, Dept conditions) { // 定义HQL语句 String hql1 = "select count(deptNo) " + hql; // 构建Query对象 Query query = currentSession().createQuery(hql1); query.setProperties(conditions); // 执行查询 return (Long) query.uniqueResult(); } //条件查询+分页 public List<Dept> conditionFindByPage(String hql, Dept conditions,int pageNo,int pageSize) { // 定义HQL语句 //String hql = "from House order by id "; // 构建Query对象 Query query = currentSession().createQuery(hql); query.setProperties(conditions); query.setFirstResult((pageNo-1)*pageSize);//设置获取结果的起始下标 query.setMaxResults(pageSize);//设置最大返回结果数 // 执行查询 return query.list(); }
2、Service层用到的方法
//条件查询+分页【通用方法】 public void findHouseByConditionsByPage(PageInfo<Dept> pageInfo, Dept conditions) { Transaction tx = null; List<Dept> result = null; try { tx = HibernateUtil.currentSession().beginTransaction(); //HQL根据条件动态生成 StringBuilder hql=new StringBuilder("from Dept as dept where 1=1 "); //String类型属性 if (conditions.getDeptName() != null && conditions.getDeptName().length() > 0) { hql.append(" and dept.deptName like '%"+conditions.getDeptName()+"%' "); } if (conditions.getLocation() != null && conditions.getLocation().length() > 0) { hql.append(" and dept.location like '%"+conditions.getLocation()+"%' "); } //int类型属性 /*if (conditions.getFloorage() != 0 && conditions.getFloorage() > 0) { hql.append(" and house.floorage>=:floorage"); }*/ //float类型属性 /*if (conditions.getPrice() != 0) { hql.append(" and house.price<=:price "); }*/ //Date类型属性 /* if (conditions.getPubdate()!=null) { Calendar ca = Calendar.getInstance();// 得到一个Calendar的实例 //ca.setTime(new Date()); // 设置时间为当前时间 ca.setTime(conditions.getPubdate()); //ca.add(Calendar.YEAR, -1); // 年份减1 ca.add(Calendar.MONTH, -1);// 月份减1 //ca.add(Calendar.DATE, -1);// 日期减1 Date resultDate = ca.getTime(); // 结果 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); System.out.println("最早日期转换结果"+sdf.format(resultDate)); hql.append(" and house.pubdate >= '"+sdf.format(resultDate)+"'"); }*/ //获取总列数 Long count=deptdao.obtainCount(hql.toString(), conditions); pageInfo.setCount(count.intValue()); pageInfo.setPageList(deptdao.conditionFindByPage(hql.toString(),conditions,pageInfo.getPageIndex(),pageInfo.PAGESIZE)); for (Dept dept : pageInfo.getPageList()) { System.out.println("部门:"+dept.getDeptName()+"--位置:"+dept.getLocation()); } tx.commit(); } catch (HibernateException e) { // TODO: handle exception e.printStackTrace(); if (tx != null) { tx.rollback(); } } }
3、测试类
package test; import static org.junit.Assert.*; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.junit.Test; import cn.hibernate.entity.Dept; import cn.hibernate.entity.Emp; import cn.hibernate.service.DeptService; import cn.hibernate.util.PageInfo; public class DeptServiceTest { private DeptService deptservice = new DeptService(); private List<Dept> deptList = new ArrayList<Dept>(); private int index=1; PageInfo<Dept> pageInfo=new PageInfo<Dept>(); public List<Dept> getDeptList() { return deptList; } public void setDeptList(List<Dept> deptList) { this.deptList = deptList; } public DeptService getDeptservice() { return deptservice; } public void setDeptservice(DeptService deptservice) { this.deptservice = deptservice; } public int getIndex() { return index; } public void setIndex(int index) { this.index = index; } public PageInfo<Dept> getPageInfo() { return pageInfo; } @Test public void testFindHouseByConditionsByPage() { Dept conditions = new Dept(); conditions.setDeptName("技术部"); // conditions.setLocation(""); //分页查询 if(index>0&&index<=pageInfo.PAGESIZE){ pageInfo.setPageIndex(index); deptservice.findHouseByConditionsByPage(pageInfo, conditions); deptList = pageInfo.getPageList(); } } }
文章来源: Hibernate分页+条件查询