当前位置:   article > 正文

Hibernate分页+条件查询_hibernate findpage 加条件

hibernate findpage 加条件

分页工具类PageInfo

  1. package cn.hibernate.util;
  2. import java.util.List;
  3. import javax.persistence.criteria.CriteriaBuilder.In;
  4. /**
  5. * @Description:部门实体类
  6. * @author:20155805邝家豪
  7. * @date 2018年4月2日上午9:25:00
  8. */
  9. public class PageInfo<T> {
  10. public static final int PAGESIZE=3;
  11. private Integer count;//总记录数
  12. private Integer pageIndex;//当前页号
  13. public Integer getCount() {
  14. return count;
  15. }
  16. public void setCount(Integer count) {
  17. this.count = count;
  18. }
  19. public Integer getPageIndex() {
  20. return pageIndex;
  21. }
  22. public void setPageIndex(Integer pageIndex) {
  23. this.pageIndex = pageIndex;
  24. }
  25. public Integer getTotalPages() {
  26. if (this.count%this.PAGESIZE==0) {
  27. this.totalPages=this.count/this.PAGESIZE;
  28. } else {
  29. this.totalPages=this.count/this.PAGESIZE+1;
  30. }
  31. return this.totalPages;
  32. }
  33. public List<T> getPageList() {
  34. return pageList;
  35. }
  36. public void setPageList(List<T> pageList) {
  37. this.pageList = pageList;
  38. }
  39. public Integer getPrameterPageIndex() {
  40. this.prameterPageIndex = pageIndex-1;
  41. return prameterPageIndex;
  42. }
  43. public Integer getNextPageIndex() {
  44. this.nextPageIndex = pageIndex+1;
  45. return nextPageIndex;
  46. }
  47. private Integer totalPages;//总页数
  48. private Integer prameterPageIndex;//上一页
  49. private Integer nextPageIndex;//下一页
  50. private List<T> pageList;//当前页的记录集合
  51. }

1、Dao层用到的方法

  1. //使用uniqueResult获取唯一结果,计算查询条件下的记录数
  2. public Long obtainCount(String hql, Dept conditions) {
  3. // 定义HQL语句
  4. String hql1 = "select count(deptNo) " + hql;
  5. // 构建Query对象
  6. Query query = currentSession().createQuery(hql1);
  7. query.setProperties(conditions);
  8. // 执行查询
  9. return (Long) query.uniqueResult();
  10. }
  11. //条件查询+分页
  12. public List<Dept> conditionFindByPage(String hql, Dept conditions,int pageNo,int pageSize) {
  13. // 定义HQL语句
  14. //String hql = "from House order by id ";
  15. // 构建Query对象
  16. Query query = currentSession().createQuery(hql);
  17. query.setProperties(conditions);
  18. query.setFirstResult((pageNo-1)*pageSize);//设置获取结果的起始下标
  19. query.setMaxResults(pageSize);//设置最大返回结果数
  20. // 执行查询
  21. return query.list();
  22. }

2、Service层用到的方法

  1. //条件查询+分页【通用方法】
  2. public void findHouseByConditionsByPage(PageInfo<Dept> pageInfo, Dept conditions) {
  3. Transaction tx = null;
  4. List<Dept> result = null;
  5. try {
  6. tx = HibernateUtil.currentSession().beginTransaction();
  7. //HQL根据条件动态生成
  8. StringBuilder hql=new StringBuilder("from Dept as dept where 1=1 ");
  9. //String类型属性
  10. if (conditions.getDeptName() != null && conditions.getDeptName().length() > 0) {
  11. hql.append(" and dept.deptName like '%"+conditions.getDeptName()+"%' ");
  12. }
  13. if (conditions.getLocation() != null && conditions.getLocation().length() > 0) {
  14. hql.append(" and dept.location like '%"+conditions.getLocation()+"%' ");
  15. }
  16. //int类型属性
  17. /*if (conditions.getFloorage() != 0 && conditions.getFloorage() > 0) {
  18. hql.append(" and house.floorage>=:floorage");
  19. }*/
  20. //float类型属性
  21. /*if (conditions.getPrice() != 0) {
  22. hql.append(" and house.price<=:price ");
  23. }*/
  24. //Date类型属性
  25. /* if (conditions.getPubdate()!=null) {
  26. Calendar ca = Calendar.getInstance();// 得到一个Calendar的实例
  27. //ca.setTime(new Date()); // 设置时间为当前时间
  28. ca.setTime(conditions.getPubdate());
  29. //ca.add(Calendar.YEAR, -1); // 年份减1
  30. ca.add(Calendar.MONTH, -1);// 月份减1
  31. //ca.add(Calendar.DATE, -1);// 日期减1
  32. Date resultDate = ca.getTime(); // 结果
  33. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  34. System.out.println("最早日期转换结果"+sdf.format(resultDate));
  35. hql.append(" and house.pubdate >= '"+sdf.format(resultDate)+"'");
  36. }*/
  37. //获取总列数
  38. Long count=deptdao.obtainCount(hql.toString(), conditions);
  39. pageInfo.setCount(count.intValue());
  40. pageInfo.setPageList(deptdao.conditionFindByPage(hql.toString(),conditions,pageInfo.getPageIndex(),pageInfo.PAGESIZE));
  41. for (Dept dept : pageInfo.getPageList()) {
  42. System.out.println("部门:"+dept.getDeptName()+"--位置:"+dept.getLocation());
  43. }
  44. tx.commit();
  45. } catch (HibernateException e) {
  46. // TODO: handle exception
  47. e.printStackTrace();
  48. if (tx != null) {
  49. tx.rollback();
  50. }
  51. }
  52. }

3、测试类

  1. package test;
  2. import static org.junit.Assert.*;
  3. import java.util.ArrayList;
  4. import java.util.Date;
  5. import java.util.List;
  6. import org.junit.Test;
  7. import cn.hibernate.entity.Dept;
  8. import cn.hibernate.entity.Emp;
  9. import cn.hibernate.service.DeptService;
  10. import cn.hibernate.util.PageInfo;
  11. public class DeptServiceTest {
  12. private DeptService deptservice = new DeptService();
  13. private List<Dept> deptList = new ArrayList<Dept>();
  14. private int index=1;
  15. PageInfo<Dept> pageInfo=new PageInfo<Dept>();
  16. public List<Dept> getDeptList() {
  17. return deptList;
  18. }
  19. public void setDeptList(List<Dept> deptList) {
  20. this.deptList = deptList;
  21. }
  22. public DeptService getDeptservice() {
  23. return deptservice;
  24. }
  25. public void setDeptservice(DeptService deptservice) {
  26. this.deptservice = deptservice;
  27. }
  28. public int getIndex() {
  29. return index;
  30. }
  31. public void setIndex(int index) {
  32. this.index = index;
  33. }
  34. public PageInfo<Dept> getPageInfo() {
  35. return pageInfo;
  36. }
  37. @Test
  38. public void testFindHouseByConditionsByPage() {
  39. Dept conditions = new Dept();
  40. conditions.setDeptName("技术部");
  41. // conditions.setLocation("");
  42. //分页查询
  43. if(index>0&&index<=pageInfo.PAGESIZE){
  44. pageInfo.setPageIndex(index);
  45. deptservice.findHouseByConditionsByPage(pageInfo, conditions);
  46. deptList = pageInfo.getPageList();
  47. }
  48. }
  49. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/529044
推荐阅读
  

闽ICP备14008679号