本文章适合一些初学者
一、使用Criteria查询数据
1.条件查询
1.1:使用Criteria查询的步骤
1.使用Session接口的creatCriteria()方法创建Criteria对象
2.使用Restrictions类提供的静态方法设置条件,这些静态方法返回Criterion对象,一个
Criterion对象代表一个查询条件,Criteria接口的add()方法用来添加查询方法
3.使用Criteria接口的list()方法执行查询语句,list()方法返回java.util.List类型的
结果,List集合中每个元素都是持久化对象
eg:(1) 使用Criteria查询所有的部门
Criteria criteria = session.createCriteria(Dept.class).list();
(2) 查询部门在“西一区”的部门
Criteria criteria = session.createCriteria(Dept.class).add(Restrictions.eq("location","西一区")).list();
(3) add(Restrictions.eq("location","西一区")).ignoreCase();
ignoreCase():表示 不区分大小写
1.2:HQL和Criteria支持的比较运算
----------------------------------------------------------------------------------------
HQL运算符 Criteria运算方法 说明
----------------------------------------------------------------------------------------
= SimpleExpression Restrictions.eq(String propertyName,Object value) 等于
----------------------------------------------------------------------------------------
<> SimpleExpression Restrictions.ne(String propertyName,Object value) 不等于
----------------------------------------------------------------------------------------
> SimpleExpression Restrictions.gt(String propertyName,Object value) 大于
----------------------------------------------------------------------------------------
>= SimpleExpression Restrictions.ge(String propertyName,Object value) 大于等于
----------------------------------------------------------------------------------------
< SimpleExpression Restrictions.lt(String propertyName,Object value) 小于
----------------------------------------------------------------------------------------
<= SimpleExpression Restrictions.le(String propertyName,Object value) 小于等于
----------------------------------------------------------------------------------------
is null Criterion Restrictions.isNull(String propertyName) 等于空值
----------------------------------------------------------------------------------------
is not null Criterion Restrictions.isNotNull(String propertyName) 非空值
----------------------------------------------------------------------------------------
1.3:HQL和Criteria支持的范围运算
----------------------------------------------------------------------------------------
HQL运算符 Criteria运算方法 说明
----------------------------------------------------------------------------------------
in(列表) Criterion Restrictions.in(String propertyName,Collection values)
或Criterion Restrictions.in(String propertyName,Object[] values) 等于列表中的某一个值
----------------------------------------------------------------------------------------
not in(列表) Criterion Restrictions.not(Restrictions.in()) 不等于列表中的任意一个值
----------------------------------------------------------------------------------------
between 值1 and 值2 Criterion Restrictions.between(String propertyName,Object lo,Object hi) 大于等于值1并且小于值2
----------------------------------------------------------------------------------------
not between 值1 and 值2 Criterion Restrictions.not(Restrictions.between()) 小于值1并且大于值2
1.4:HQL和Criteria支持的字符串模式匹配
----------------------------------------------------------------------------------------
HQL运算符 Criteria运算方法 说明
----------------------------------------------------------------------------------------
|SimpleExpression Restrictions.like(String propertyName,Object value)
|或 字符串模式匹配
|SimpleExpression Restrictions.like(String propertyName,Object value,MatchMode
| matchMode)
|-------------------------------------------------------------------------------
like |Criterion Restrictions.ilike(String propertyName,Collection values)
|或 字符串模式匹配,同时忽略大小写
|Criterion Restrictions.in(String propertyName,String value,MatchMode matchMode)
|
----------------------------------------------------------------------------------------
1.5:MatchMode类的静态常量
----------------------------------------------------------------------------------------
模式 举例 说明
----------------------------------------------------------------------------------------
MatchMode.Start Restrictions.like("empName","s",MatchMode.start) 员工姓名以“s”开头
----------------------------------------------------------------------------------------
MatchMode.end Restrictions.end("empName","s",MatchMode.end) 员工姓名以“s”结尾
----------------------------------------------------------------------------------------
MatchMode.anywhere Restrictions.anywhere("empName","s",MatchMode.anywhere) 员工姓名中包含“s”
----------------------------------------------------------------------------------------
MatchMode.exact Restrictions.anywhere("empName","s",MatchMode.exact) 员工姓名中等于“s”,精确匹配
----------------------------------------------------------------------------------------
1.6:HQL和Criteria支持的逻辑方法
----------------------------------------------------------------------------------------
HQL运算符 Criteria运算方法 说明
----------------------------------------------------------------------------------------
and LogicalExpression Restrictions.and(Criterion lhs,Criterion rhs) 逻辑与
----------------------------------------------------------------------------------------
or LogicalExpression Restrictions.or(Criterion lhs,Criterion rhs)
或Disjunction Restritions.disjunction() 逻辑或
----------------------------------------------------------------------------------------
not Criterion Restrictions.not(Criterion expression) 逻辑非
1.7:HQL和Criteria支持的集合运算
----------------------------------------------------------------------------------------
模式 举例 说明
----------------------------------------------------------------------------------------
is empty Criterion Restritions.isEmpty(String properName) 集合为空,不包含任何元素
----------------------------------------------------------------------------------------
is not empty Criterion Restritions.isNotEmpty(String properName) 集合不为空
2.动态查询(封装条件类)
二、排序、分页及关联 .addOrder(Order.asc("salary")).list();
排序: eg: List<Emp> list = session.createCriteria(Emp.class).add(Restrictions.gt("salary",4000D))
.addOrder(Order.asc("salary")).list();
分页: eg: List<Emp> list = session.createCriteria(Emp.class).add(Restrictions.gt("salary",4000D))
.addOrder(Order.asc("salary")).setFirstResult(0).setMaxResult(2).list();
三、查询唯一对象
Query和Criteria接口执行查询语句的方法
------------------------------------------------------------------------------------------------
方法 说明 Query接口 Criteria接口
------------------------------------------------------------------------------------------------
List list() 返回List集合 支持 支持
------------------------------------------------------------------------------------------------
Iterator iterate() 返回Iterator迭代器,只查询出ID值 支持 不支持
------------------------------------------------------------------------------------------------
Object uniqueResult 返回唯一对象 支持 支持
------------------------------------------------------------------------------------------------
eg: Emp emp = session.createCriteria(Emp.class).add(Restrictions.gt("salary",4000D))
.addOrder(Order.asc("salary")).uniqueResult();
四、连接查询
1.Criteria查询和HQL查询都支持连接查询,Criteria只支持内连接和迫切左外连接
2.Criteria接口提供了createCriteria()和createAlias()方法建立内连接
createCriteria(): 输出姓名包括“a”的员工
eg:List<Emp> list = session.createCriteria(Emp.class).add(Restrictions.ilike("empName","a",MatchMode.anywhere))
.createCriteria("dept").list();
createAlias():输出姓名包括“a”的员工
eg:eg:List<Emp> list = session.createCriteria(Emp.class).add(Restrictions.ilike("empName","a",MatchMode.anywhere))
.createAlias("dept").list();
3.Criteria接口提供了setFetchMode()方法来设置迫切左外连接
查询位置在“西一区”的部门及其员工人数
eg:List<Dept> list = session.createCriteria(Dept.class,"d").setFetchMode("emps",FetchMode.Join)
.add(Restrictions.eq("d.location","西一区").ignoreCase()).list();
五、投影分组及DetachedCriteria
1.使用投影和分组
1.1:查询1个的属性 使用Criteria接口的setProjection()方法来设置投影属性
eg:List<String> list = session.createCriteria(Dept.class).setProjection(
Property.forName("deptName")).list();
1.2: 查询2个以上的属性。需要使用Projections的projectionList()方法
eg:List<Object[]> list = session.createCriteria(Emp.class).setProjection(
Propertions.projectionList().add(Property.forName("empName"))
.add(Property.forName("hiredate"))
).list();
1.3:Projections常用聚合查询方法
------------------------------------------------------------------------------
方法 说明
------------------------------------------------------------------------------
PropertyProjection Projections.groupProperty(String propertyName) 分组
------------------------------------------------------------------------------
Projection Projections.rowCount() 统计记录数
------------------------------------------------------------------------------
AggregateProjection Projections.avg(String propertyName) 统计平均值
------------------------------------------------------------------------------
AggregateProjection Projections.max(String propertyName) 统计最大值
------------------------------------------------------------------------------
AggregateProjection Projections.min(String propertyName) 统计最小值
------------------------------------------------------------------------------
CountProjection Projections.count(String propertyName) 统计某一段的非空记录数
------------------------------------------------------------------------------
AggregateProjection Projections.sum(String propertyName) 针对某一字段求和
------------------------------------------------------------------------------
eg:List<Object[]> list = session.createCriteria(Emp.class,"e").createAlias("e.dept","d").
setProjection(
Projections.projectionList()
.add(Projections.groupProperty("d.deptName"))
.add(Projections.avg("e.salary"))
.add(Projections.max("e.salary"))
.add(Projections.min("e.salary"))
).list();
2.使用DetachedCriteria(DetachedCriteria创建时不需要Session对象而Criteria是由Session对象创建的)
2.1:什么时候使用DetachedCriteria查询:在常规的Web编程中,有大量的动态查询
使用DetachedCriteria查询财务部的姓名包括“a”的员工
eg:DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Emp.class,"e")
.createAlias("d.dept","d")
.add(Restrictions.eq("d.deptName","财务部"))
.add(Restrictions.ilike("e.empName","a",
MatchMode.anywhere));
List<Emp> list = detachedCriteria.getExecutableCriteria(session).list();