Hibernate 框架的查詢方式

Kunaly發表於2018-02-03

Query 查詢介面

1. 具體的查詢程式碼如下
// 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. 具體的查詢程式碼如下
// 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);*/

相關文章