Hibernate检索方式主要有5种,分别是导航对象图检索方式、OID检索方式、HQL检索方式、QBC检索方式和SQL检索方式
1、对象图导航检索方式
对象图导航检索方式是根据已经加载的对象,导航到他的关联对象。他利用类与类之间的关系来检索对象。
Linkman linkMan = (LinkMan) session.get(LinkMan.class,1l);
Customer customer=linkMan.getCustomer();
2、OID检索方式
OID检索方式主要是指用Session的get()和load()方法架子啊某条记录对应的对象。
Customer customer = (Customer) session.get(Customer.class,1);
Customer customer = (Customer) session.load(Customer.class, 1);
3、HQL检索
HQL使用的是类、对象和属性的概念,没有表和字段的概念。HQL检索具备如下的功能:
1)在查询语句中设定各种查询条件
2)支持投影查询,仅检索出对象的部分属性
3)支持分页查询
4)支持分组查询,允许使用group by 和having 关键字
5)提供内置聚集函数,如sum()、min()和max()
6)能够调用用户定义的SQL函数
7)支持子查询,即嵌套查询
8)支持动态绑定参数
完整的HQL语句:
select ....from ...where ...group by .....having ....order by ....asc/desc
3.1HQL基本检索
public void demo(){
Session session=HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
//基本查询
Query query = session.createQuery("from Customer");
List<Customer> list = query.list();
//启别名
Query query = session.createQuery("from Customer c");
Query query = session.createQuery("select c from Customer c");
List<Customer> list = query.list();
for(Customer customer : list){
System.Out.println(customer);
}
tx.commit();
}
3.2排序检索
Query query = session.createQuery(“from Customer order by cust_id desc”);
List<Customer> list = query.list();
3.3条件检索
//按位置绑定参数
Query query = session.createQuery("from Custoemr where cust_name = ?");
query.setString(0,"小君君");
query.setParameter(0,"小君君");
List<Customer> list = query.list();
//按名称绑定参数
Query query = session.createQuery("from Customer where cust_name = :name");
query.setParameter("name","小君君");
List<Customer> list = query.list();
3.4f分页检索
//投影查询一列
List<String> list = session.createQuery("select cust_name from Customer").list();
//投影查询多列
List<object[]> list = session.createQuery("select cust_id, cust_name from Customer ").list();
//投影的构造的方式查询
List<Customer> list = session.createQuery("select new Customer(cust_id, cust_name ) from Customer").list();
4QBC检索
QBC检索有criteria接口、Criterion接口和Expression类组成。
//创建criteria对象
Criteria criteria = session.createCriteria(Customer.class);
//设定查询条件
Criterion criterion = Restrictions.eq("id" , 1);
//添加查询条件
criteria.add(criterion);
//执行查询,返回结果
List<Customer> cs = criteria.list()
.41基本检索
//简单检索
Criteria criteria = session.createCriteria(Customer.class);
List<Customer> list = criteria.list();
4.2条件检索
//条件查询
Criteria criteria = session.createCriteria(Customer.class);
//设置条件
criteria.add(Resrictions.eq("cust_name", “小君君”));
criteria.add(Restriction.like("cust_name","%君君%"));
criteria.add(Restriction.gt("cust_id",1l));
4.3分页查询
//条件查询
Criteria criteria = session.createCriteria(linkMan.class);
//设置分页查询
criteria.setFirstResult(5);
criteria.setMaxResult(5);
4.4排序查询
//排序查询
Criteria criteria = session.createCriteria(LinkMan.class);
criteria.addOrder(Order.desc("lkm_id"));
List<LinkMan> list = criteria.list();
4.5统计检索
//统计查询
Criteria criteria = HibernateUtils.getCurrentSession(LinkMan.class);
criteria.setProjection(Projections.rowCount());
Long count = (Long) criteria.uniqueResult();
4.6离线条件检索
//离线条件查询:DetachedCriteria(SSH整合经常用到)
DetachedCriteria detachedCriteria = DetacheCriteria.forClass(Customer.class);
detachedCriteria.add(Restrictions.eq("cust_name","小君君"));
Session session = HibernateUtils.getCurrent();
Transction tx = session.beginTransaction();
List<Customer> list = detachedCriteria.getExecutableCriteria(session).list();
5本地SQL检索方式
SQLQuery sqlQuery = session.createSQLQuery(select id,name ,age, city from customer);