package cn.itcast.hibernate;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import cn.itcast.hibernate.domain.User;
public class TestCriteria {
/**
* @param args面向对象方式查询Criteria
*/
public static void main(String[] args) {
List<User> list=criteria("name");
for (User user2 : list) {
System.out.println(user2.getName());
}
}
static List<User> criteria(String name) {
Session session = null;
List<User> users=null;
try {
session = HibernateUtil.getSession();
// 创建criteria对象
Criteria criteria = session.createCriteria(User.class);
users =criteria.add(Restrictions.eq("name", name)).list();
return users;
} finally {
session.close();
}
}
}
criteria(面向对象的查询方式)不如query查询功能强大。