1、Restrictions:判断属性值是大于、小于、等于
2、DetachedCriteria中的add方法将条件封装起来,hibernate拼写sql语句
3、用getHibernateTemplate().findByCriteria();方法执行DetachedCriteria
@SuppressWarnings("unchecked")
public Set<Prodphoto> prodphotos(String productId, Integer viewType)
throws Exception {
DetachedCriteria dcr = DetachedCriteria.forClass(Prodphoto.class);
if ((productId != null) && (!"".equals(productId.trim()))) {
dcr.add(Restrictions.eq("productId", productId));
}
if (viewType != null){
dcr.add(Restrictions.eq("viewType", viewType));
}
// return (TreeSet<Prodphoto>) getHibernateTemplate().findByCriteria(dcr);//最好不要这样用
return new TreeSet<Prodphoto>(getHibernateTemplate().findByCriteria(dcr));
}