[转] Spring Hibernate findByCriteria(...)方法 使用

Java代码

publicList < DispatchTask > findByCriteria
   (DetachedCriteria criteria)
{
   log.debug("findingDispatchTaskinstancebycriteria");
   try
   {
      List < DispatchTask > results = getHibernateTemplate().findByCriteria
         (criteria);
      log.debug("findbycriteriasuccessful,resultsize:" + results.size());
      returnresults;
   }
   catch (RuntimeExceptionre)
   {
      log.error("findbycriteriafailed", re);
      throwre;
   }
}

这是Service的片段
Java代码

publicList < DispatchTask > getByQuery(DispatchTaskinstance, DatequeryTime1,
   DatequeryTime2)
{
   StringtaskId = instance.getTaskId();
   StringdeptName = instance.getDeptName();
   Stringlinkman = instance.getLinkman();
   StringplateNum = instance.getPlateNum();
   ShortstatusId = instance.getStatusId();
   DatequeryTime3 = null;
   if (queryTime2 != null)
   {
      queryTime3 = newDate();
      queryTime3.setTime(queryTime2.getTime() + (newLong("86400000")));
   }
   DetachedCriteriacriteria = DetachedCriteria .forClass(DispatchTask.class);
   if (taskId != null && taskId != "")
   {
      criteria.add(Expression.like("taskId", taskId, MatchMode.ANYWHERE));
   }
   if (deptName != null && deptName != "")
   {
      criteria.add(Expression.like("deptName", deptName, MatchMode.ANYWHERE));
   }
   if (linkman != null && linkman != "")
   {
      criteria.add(Expression .like("linkman", linkman, MatchMode.ANYWHERE));
   }
   if (plateNum != null && plateNum != "")
   {
      criteria.add(Expression.like("plateNum", plateNum, MatchMode.ANYWHERE));
   }
   if (statusId != null && statusId != 999)
   {
      criteria.add(Expression.eq("statusId", statusId));
   }
   if (queryTime1 != null)
   {
      if (queryTime2 != null)
      {
         if (queryTime1.getTime() <= queryTime2.getTime())
         {
            criteria.add(Expression.between("dispatchDate", queryTime1,
               queryTime3));
         }
      }
      else
      {
         criteria.add(Expression.ge("dispatchDate", queryTime1));
      }
   }
}
else
{
   if (queryTime2 != null)
   {
      criteria.add(Expression.le("dispatchDate", queryTime3));
   }
}
returndispatchTaskDAO.findByCriteria(criteria);
}

猜你喜欢

转载自107x.iteye.com/blog/2112206