使用criteria查询not like某个字段时使用sqlRestriction
public List<CmsProgram> queryAllNews() {
DetachedCriteria criteria=DetachedCriteria.forClass(CmsProgram.class);
criteria.add(Restrictions.eq("delFlag", 0));
criteria.add(Restrictions.eq("isMain", 1));
criteria.add(Restrictions.sqlRestriction("PROGRAM_CODE not like '03%'"));
criteria.add(Restrictions.sqlRestriction("PROGRAM_CODE not like '05%'"));
criteria.addOrder(Order.asc("sortNo"));
return(List<CmsProgram>) this.hibernateTemplate.findByCriteria(criteria);
}
使用criteria查询{where(1 and 2)or (3 and 4)}
public List<Message> recycleBox(String id){
DetachedCriteria criteria = DetachedCriteria.forClass(Message.class);
criteria.add(Restrictions.or(Restrictions.and(Restrictions.eq("messageTo",id),Restrictions.eq("messageToFlag","1")),Restrictions.and(Restrictions.eq("messageFrom",id),Restrictions.eq("messageFromFlag","1"))));
criteria.addOrder(Order.desc("delDate"));
return (List<Message>) this.hibernateTemplate.findByCriteria(criteria);
}
排序
criteria.addOrder(Order.desc("createDate"));
criteria.addOrder(Order.asc("createDate"));
模糊查询
criteria.add(Restrictions.like("bookBorrowerName", "%" + bookBorrowerName + "%"));
两者之间
criteria.add(Restrictions.between("bookBorrowingDate", startTime_date, endTime_date));
小于等于
criteria.add(Restrictions.le("bookBorrowingDate", end));
大于等于
criteria.add(Restrictions.ge("bookBorrowingDate", start));
小于
criteria.add(Restrictions.lt("bookRebackDate", DateUtil.StringToDate(simpleDateFormat.format(new Date()), "yyyy-MM-dd")));
大于
criteria.add(Restrictions.gt("bookBorrowingDate", start));
在xx之间
criteria.add(Restrictions.in("id", arr));
创建方式
Criteria criteria = hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(this.entityClass);
小生刚学,有什么不足,请大神指导,如果有人指导怎么用criteria多表查询,请指导,谢谢!