Hibernate框架的查詢方式
Query查詢接口
-
具體的查詢代碼如下
1.查詢所有記錄
/Query query = session.createQuery("from Customer");
List<Customer> list = query.list();
System.out.println(list);/2.條件查詢:
/Query query = session.createQuery("from Customer where name = ?");
query.setString(0, "李健");
List<Customer> list = query.list();
System.out.println(list);/3.條件查詢:
/Query query = session.createQuery("from Customer where name = :aaa and age = :bbb");
query.setString("aaa", "李健");
query.setInteger("bbb", 38);
List<Customer> list = query.list();
System.out.println(list);/
Criteria查詢接口(做條件查詢非常合適)
-
具體的查詢代碼如下
1.查詢所有記錄
/Criteria criteria = session.createCriteria(Customer.class);
List<Customer> list = criteria.list();
System.out.println(list);/2.條件查詢
/Criteria criteria = session.createCriteria(Customer.class);
criteria.add(Restrictions.eq("name", "李健"));
List<Customer> list = criteria.list();
System.out.println(list);/3.條件查詢
/Criteria criteria = session.createCriteria(Customer.class);
criteria.add(Restrictions.eq("name", "李健"));
criteria.add(Restrictions.eq("age", 38));
List<Customer> list = criteria.list();
System.out.println(list);/