Hibernate常用查询
1、通过Criteria查询
Criteria criteria = this.sessionFactory.getCurrentSession().createCriteria(xxx.class); criteria.add(Restrictions.eq("isDeleted", 0)); criteria.add(Restrictions.in("fullName",depList)); List<xxx> resultList = new ArrayList<>(); resultList.addAll(criteria.list());
2、通过HQL查询
@Override public Integer getDailyProjectNumByDepNameAndTime(String depName,String startDate,String endDate){ String sql = "select count(*) from xxx where creator_department like:depName and project_type_enum='日常项目' and gmt_create>=:ds and gmt_create<=:de"; Query query = this.sessionFactory.getCurrentSession().createSQLQuery(sql); query.setParameter("ds",startDate); query.setParameter("de",endDate); query.setParameter("depName",depName); BigInteger bigInteger = (BigInteger) query.uniqueResult(); return bigInteger.intValue(); }
3、分页查询
@Override public List<xxx> listByDepartmentAndDateAndPage(ProjectEfficiencyQuery query){ Criteria criteria = this.sessionFactory.getCurrentSession().createCriteria(xxx.class); Date startDate = DateUtil.getDateTimeFromString(query.getStartDate()); Date endDate = DateUtil.getDateTimeFromString(query.getEndDate()); criteria.add(Restrictions.eq("stamp", "research")); criteria.add(Restrictions.eq("type", "project")); criteria.add(Restrictions.like("department", query.getDepName(), MatchMode.START)); criteria.add(Restrictions.ge("gmtCreate", startDate)); criteria.add(Restrictions.le("gmtCreate", endDate)); int total = ((Long) criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue(); query.setTotal(total); criteria.setProjection(null); criteria.setFirstResult((query.getPageNum()-1)*query.getPageSize()); criteria.setMaxResults(query.getPageSize()); List<xxx> list = (List<xxx>)criteria.list(); return list; }