使用Criteria进行查询时,不仅仅能组合出SQL中where子句的功能,还可以组合出如排序、统计、分组等的查询功能。
您可以使用Criteria进行查询,
并使用org.hibernate.criterion.Order对结果进行排序,例如使用Oder.asc(),指定根据”age”由小到大排序(反
之则使用desc()):
Criteria criteria =
session.createCriteria(User.class);
criteria.addOrder(Order.asc("age"));
List
users = criteria.list();
注意在加入Order条件时,使用的是
addOrder()方法,而不是add()方法,在产生SQL语句时,会使用order
by与asc(desc)来进行排序指定:
Hibernate: select
this_.id as
id0_0_, this_.name as name0_0_, this_.age as age0_0_ from T_USER this_ order by this_.age asc
Criteria的
setMaxResults()方法可以限定查询回来的笔数,如果配合setFirstResult()设定传回查询结果第一笔资料的位置,就可以实现简
单的分页,例如传回第51笔之后的50笔资料(如果有的话):
Criteria criteria =
session.createCriteria(User.class);
criteria.setFirstResult(51);
criteria.setMaxResults(50);
List
users = criteria.list();
根据您所指定得资料
库,Hibernate将自动产生与资料库相依的限定笔数查询子句,例如在MySQL中,将使用limit产生以下的SQL语句:
Hibernate: select
this_.id as
id0_0_, this_.name as name0_0_, this_.age as age0_0_ from T_USER this_ limit
?, ?
您可以对查询结果进行统计动作,使用
org.hibernate.criterion.Projections的avg()、rowCount()、count()、max()、min
()、
countDistinct()等方法,再搭配Criteria的setProjection()方法加入条件设定,例如对查询结果的"age"作平均:
Criteria criteria =
session.createCriteria(User.class);
criteria.setProjection(Projections.avg("age"));
List
users = criteria.list();
上面的程式将由Hibernate自动
产生SQL的avg函数进行平均计算:
Hibernate: select avg(this_.age)
as y0_ from T_USER
this_
还可以配合Projections的
groupProperty()来对结果进行分组,例如以"age"进行分组,也就是如果资料中"age"如果有
20、20、25、30,则以下会显示20、25、30:
Criteria criteria =
session.createCriteria(User.class);
criteria.setProjection(Projections.groupProperty("age"));
List
users = criteria.list();
上面的程式将由Hibernate自动
产生SQL的group
by子句进行分组计算:
Hibernate: select
this_.age
as y0_ from T_USER this_ group by
this_.age
如果想同时结合统计与分组功能,则可以
使用org.hibernate.criterion.ProjectionList,例如下面的程式会计算每个年龄各有多少个人:
ProjectionList projectionList =
Projections.projectionList();
projectionList.add(Projections.groupProperty("age"));
projectionList.add(Projections.rowCount());
Criteria
criteria = session.createCriteria(User.class);
criteria.setProjection(projectionList);
List
users = criteria.list();
观察所产生的SQL语句,将使用
group
by先进行分组,再针对每个分组进行count函数的计数,
Hibernate: select
this_.age
as y0_, count(*)
as y1_ from
T_USER this_
group by
this_.age
设定查询条件并非一定要使用
Restrictions,如果属性条件很多,使用Restrictions也不方便,如果有一个已知的物件,则可以根据这个物件作为查询的依据,看看是
否有属性与之类似的物件,例如:
User user = new
User();
user.setAge(new
Integer(30));
Criteria criteria =
session.createCriteria(User.class);
criteria.add(Example.create(user)
);
List
users = criteria.list();
您可以透过
org.hibernate.criterion.Example的create()方法来建立Example实例,Example实作了
Criteria介面,因此可以使用add()方法加入至Criteria条件设定之中,Hibernate将自动过滤掉空属性,根据已知物件上已设定的
属性,判定是否产生于where子句之中:
Hibernate: select
this_.id as
id0_0_, this_.name as name0_0_, this_.age as age0_0_ from T_USER this_ where
(this_.age=?)
如果您了解如何撰写SQL语句,想要设
定一些Hibernate产生SQL时的范本,您也可以使用Restrictions的sqlRestriction()方法,提供SQL语法范本作限定
查询,例如查询name以cater开头的资料:
Criteria criteria =
session.createCriteria(User.class);
criteria.add(Restrictions.sqlRestriction(
"{alias}.name
LIKE
(?)", "cater%", Hibernate.STRING));
List users =
criteria.list();
其中alias将被替换为与User类
别相关的名称,而?
将被替换为cater%,也就是第二个参数所提供的值,sqlRestriction()方法第一个参数所设定的是where子句的部份,所以在SQL撰
写时,不必再写where,观察所产生的SQL语句,将使用您所设定的SQL范本作为基础,来完成SQL的条件查询:
Hibernate: select
this_.id as
id0_0_, this_.name as name0_0_, this_.age as age0_0_ from T_USER this_
where
this_.name LIKE
(?)
如果有多个查询条件,例如
between子句的查询,则可以如下:
Criteria criteria =
session.createCriteria(User.class);
Integer[] ages =
{new
Integer(20), new Integer(40)};
Type[] types =
{Hibernate.INTEGER, Hibernate.INTEGER};
criteria.add(Restrictions.sqlRestriction(
"{alias}.age
BETWEEN
(?) AND (?)", ages, types));
List users =
criteria.list();
观察所产生的SQL语句如下:
Hibernate: select
this_.id as
id0_0_, this_.name as name0_0_, this_.age as age0_0_ from T_USER this_
where
this_.age BETWEEN (?) AND
(?)
分享到:
相关推荐
09 Criteria查询方式
Hibernate中的Criteria连表查询,包括单表属性查询,多表内连,以及左外连接查询
1、掌握Hibernate框架应用项目 2、学习HQL参数查询和Criteria查询 3、比较HQL和Criteria的不同 建立web项目,通过页面查询构造动态查询语句,struts2和hibernate框架整合。
Hibernat 使用Criteria查询多对多关系(SET集合)条件
T21.9_Criteria查询 java 经典教程 经典教材
Criteria查询将数据查询条件封装成为一个对象,可以堪称是传统SQL的对象化表示。
Hibernate中关于Criteria查询条件的选择.doc
NULL 博文链接:https://beisicao.iteye.com/blog/955674
绍Hibernate的Criteria 查询方法
主要介绍了Java的Hibernate框架中Criteria查询使用的实例讲解,Hibernate是Java的SSH三大web开发框架之一,需要的朋友可以参考下
Hibernate-Criteria 模糊查询
Hibernate-Criteria_模糊查询
第一章:Spring Data JPA入门 包括:是什么、能干什么、有什么、HelloWorld等 第二章:JpaRepository基本功能 ... 包括:Specifications基础、 Criteria查询基本概念、 Criteria查询实现、多表联接等内容
这个案例是根据分组查询,并且得到每组的条数,不懂得可以q我:1710086675,欢迎认识更多新朋友
我尝试着模仿hibernate的criteria构建这么一个轻量级查询框架,提供一下特点: 1 可以根据前台传过来的查询数据自动构建查询条件 2 可以生成纯SQl 3 也可以直接使用preparestatement进行查询,返回我们想要的数据。 ...
Criteria标准化对象查询小例子,内有配套的SQL表语句和使用说明
Hibernate常用操作示例,包含详细注释说明,数据库初始化文件。使用和学习请参考:http://blog.csdn.net/daijin888888/article/details/51692942