一个完整的Criteria查询例子
public List<ResourceContent> findResourceBySearch(ResourceContent resourceContent, int off, int max, Object... date) {Session session = this.getSession(true);Criteria ca = session.createCriteria(ResourceContent.class);String resourceName = null;Criterion resource = Expression.like("name", "%"+ resourceContent.getName() + "%");Criterion start = Expression.ge("createdTime", date[0]);Criterion end = Expression.le("createdTime", date[1]);Criterion typeID = Expression.eq("resourceTypeID", resourceContent.getResourceTypeID());if (resourceContent.getName() != null) {ca.add(resource);}if (date[0] != null) {ca.add(start);}if (date[1] != null) {ca.add(end);}if (resourceContent.getResourceTypeID() != 0) {ca.add(typeID);}ca.setFirstResult(off);ca.setMaxResults(max);ca.addOrder(Order.desc("createdTime"));List<ResourceContent> resourceList = ca.list();this.releaseSession(session);return resourceList;}
?
Expression.eq相当于String的equals方法Expression.le比较两个属性的值,对应SQL中的“field < field”Expression.ge比较两个属性的值,对应SQL中的“field > field”ca.addOrder 排序ca.setMaxResults:每次查询的最大记录数ca.setFirstResult:偏移量1 楼 taupo 2008-08-27 "%" + resourceContent.getName() + "%"