Hibernate(十五):QBC检索和本地SQL检索
Posted yy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hibernate(十五):QBC检索和本地SQL检索相关的知识,希望对你有一定的参考价值。
- QBC检索
QBC查询就是通过使用Hibernate提供的Query By Criteria API来查询对象,这种API封装了SQL语句的动态拼装,对查询提供了更加面向对象的功能接口。
1)通过Critera实现具有条件的查询
1 @Test 2 public void testCriteria00() { 3 // 1、创建criteria对象 4 Criteria criteria = session.createCriteria(Employee.class); 5 6 // 2、添加过滤条件可以用Criterion表,Criterion可以通过Restrictions的静态方法返回。 7 criteria.add(Restrictions.eq("email", "[email protected]")); 8 criteria.add(Restrictions.gt("salary", 1000F)); 9 10 System.out.println(criteria.uniqueResult()); 11 }
执行sql及结果:
Hibernate: select this_.ID as ID1_1_0_, this_.NAME as NAME2_1_0_, this_.SALARY as SALARY3_1_0_, this_.EMAIL as EMAIL4_1_0_, this_.DEPARTMENT_ID as DEPARTME5_1_0_ from DX_EMPLOYEE this_ where this_.EMAIL=? and this_.SALARY>? Employee [id=11, name=tommy10, salary=10000.0, [email protected]]
2)通过Critera实现具有AND OR条件的查询
1 @Test 2 public void testCriteraAndOr() { 3 Criteria criteria = session.createCriteria(Employee.class); 4 5 // AND :使用Conjunction表示,Conjunction本身就是一个Criterion对象,且其中还可以添加Criterion对象 6 Conjunction conjunction = Restrictions.conjunction(); 7 conjunction.add(Restrictions.like("name", "2", MatchMode.ANYWHERE)); 8 Department depart = new Department(); 9 depart.setId(5); 10 conjunction.add(Restrictions.eq("department", depart)); 11 12 // OR 13 Disjunction disjunction = Restrictions.disjunction(); 14 disjunction.add(Restrictions.gt("salary", 10000F)); 15 disjunction.add(Restrictions.isNull("email")); 16 17 criteria.add(disjunction); 18 criteria.add(conjunction); 19 20 List<Employee> items = (List<Employee>) criteria.list(); 21 System.out.println(items.size()); 22 }
执行sql及结果:
1 Hibernate: 2 select 3 this_.ID as ID1_1_0_, 4 this_.NAME as NAME2_1_0_, 5 this_.SALARY as SALARY3_1_0_, 6 this_.EMAIL as EMAIL4_1_0_, 7 this_.DEPARTMENT_ID as DEPARTME5_1_0_ 8 from 9 DX_EMPLOYEE this_ 10 where 11 ( 12 this_.SALARY>? 13 or this_.EMAIL is null 14 ) 15 and ( 16 this_.NAME like ? 17 and this_.DEPARTMENT_ID=? 18 ) 19 2
3)通过Critera实现统计查询:使用Projection 来表示
1 @Test 2 public void testStatistics(){ 3 Criteria criteria=session.createCriteria(Employee.class); 4 5 // 统计查询:使用Projection 来表示 6 criteria.setProjection(Projections.max("salary")); 7 8 System.out.println(criteria.uniqueResult()); 9 }
执行sql及结果:
1 Hibernate: 2 select 3 max(this_.SALARY) as y0_ 4 from 5 DX_EMPLOYEE this_ 6 79000.0
4)通过Critera实现排序、分页查询
1 @Test 2 public void testOrderByAndPager() { 3 Criteria criteria = session.createCriteria(Employee.class); 4 5 // 1) Order By 6 criteria.addOrder(Order.desc("salary")); 7 criteria.addOrder(Order.desc("name")); 8 9 // 2) Pager 10 int pageSize = 5; 11 int pageNum = 2; 12 List<Employee> employees = (List<Employee>) criteria.setFirstResult((pageNum - 1) * pageSize).setMaxResults(pageSize).list(); 13 14 System.out.println(employees.size()); 15 }
执行sql及结果:
1 Hibernate: 2 select 3 this_.ID as ID1_1_0_, 4 this_.NAME as NAME2_1_0_, 5 this_.SALARY as SALARY3_1_0_, 6 this_.EMAIL as EMAIL4_1_0_, 7 this_.DEPARTMENT_ID as DEPARTME5_1_0_ 8 from 9 DX_EMPLOYEE this_ 10 order by 11 this_.SALARY desc, 12 this_.NAME desc limit ?, 13 ? 14 5
更多关于QBC的使用方式需要参考Hibernate官网实例。
- 本地SQL查询
本地SQL查询来完善HQL不能涵盖所有的查询特性。
以上是关于Hibernate(十五):QBC检索和本地SQL检索的主要内容,如果未能解决你的问题,请参考以下文章
[原创]java WEB学习笔记92:Hibernate学习之路-- -QBC 检索和本地 SQL 检索:基本的QBC 查询,带 AND 和 OR 的QBC,统计查询,排序,分页