1.Criteria Query检索方式
Criteria criteria = session.createCriteria(User.class);
criteria.add(Expression.like("account" , "T%"));
criteria.add("sex" , "男");
2. Hibernate Query Language(HQL)
涵盖Criteria前提下提供更强大的查询功能
Query query = session.createQuery("from Users as C where c.userName =:
userName and c.age =:" userAge);
query.setString("userName" , "Tom");
query.setInteger("userAge" , 20);
List list = query.list();
3.DetachedCriteria 检索方式 (Hibernate3)
DetachedCriteria deCriteria = DetachedCriteria.forClass(Users.class);
deCriteria.add(Expression.eq("UserName","Eleven"));
deCriteria.add(Expression.eq("Age" , 25));
Criteria criteria = deCriteria.getExecutableCriteria(session);
Iterator it = criteria.list().iterator();
while(it.hasNext()){
Users user = (Users)it.next();
System.out.println(user.getUserName());
}
DetachedCriteria生命周期与Session无关,将查询逻辑与Criteria实例分离,最大化的代码重用