? HQL在语法结构上和SQL语句十分的相同,所以可以很快的上手进行使用。使用HQL需要用到Hibernate中的Query对象,该对象专门执行HQL方式的操作。
查询所有示例
1
2
3
4
5
6
7
8
|
session.beginTransaction();
String hql = "from User"; // from 后跟的是要查询的对象,而不是表
Query query = session.createQuery(hql);
List<User> userList = query.list();
for(User user:userList){
System.out.println(user.getUserName());
}
session.getTransaction().commit();
|
带where的查询示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
session.beginTransaction();
String hql = "from User where userName = ‘James‘";
Query query = session.createQuery(hql);
List<User> userList = query.list();
for(User user:userList){
System.out.println(user.getUserName());
}
session.getTransaction().commit();
/*
在HQL中where语句中使用的是持久化对象的属性名,如上面示例中的userName。当然在HQL中也可以使用别名
*/
String hql = "from User as u where u.userName = ‘James‘";
/*
过滤条件
在where语句中还可以使用各种过滤条件,如:=、<>、<、>、>=、<=、between、not between、
in、not in、is、like、and、or等
*/
|
获取一个不完整的对象
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
session.beginTransaction();
String hql = "select userName from User";
Query query = session.createQuery(hql);
List<Object> nameList = query.list();
for(Object obj:nameList){ String name=(String)obj; System.out.println(name);
}
session.getTransaction().commit();
// 多个属性的话,需要用object[]接收
session.beginTransaction();
String hql = "select userName,userPwd from User";
Query query = session.createQuery(hql);
List nameList = query.list();
for(Object obj:nameList){
Object[] array = (Object[]) obj; // 转成object[]
System.out.println("name:" + array[0]);
System.out.println("pwd:" + array[1]);
}
session.getTransaction().commit();
|
统计和分组查询
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
session.beginTransaction();
String hql = "select count(*),max(id) from User";
Query query = session.createQuery(hql);
List nameList = query.list();
for(Object obj:nameList){
Object[] array = (Object[]) obj;
System.out.println("count:" + array[0]);
System.out.println("max:" + array[1]);
}
session.getTransaction().commit();
/*
该条sql语句返回的是单条数据,所以还可以这样写
单列数据用Object,多列数据用Object[]
*/
Object[] object = (Object[]) query.uniqueResult();
System.out.println("count:" + object[0]);
System.out.println("max:" + object[1]);
|
更多写法
1
2
3
4
5
6
7
|
select distinct name from Student;
select max(age) from Student;
select count(age),age from Student group by age;
from Student order by age;
|
HQL占位符
1
2
3
4
5
6
7
8
9
10
11
12
|
session.beginTransaction();
String hql = "from User where userName = ?";
Query query = session.createQuery(hql);
// 索引从0开始
query.setString(0, "James");
List<User> userList = query.list();
for(User user:userList){
System.out.println(user.getUserName());
}
session.getTransaction().commit();
|
HQL引用占位符
1
2
3
4
5
6
7
8
9
10
11
12
13
|
session.beginTransaction();
String hql = "from User where userName = :name";
Query query = session.createQuery(hql);
query.setParameter("name", "James");
List<User> userList = query.list();
for(User user:userList){
System.out.println(user.getUserName());
}
session.getTransaction().commit();
|
HQL分页
1
2
3
4
5
6
7
8
9
10
11
12
|
session.beginTransaction();
String hql = "from User";
Query query = session.createQuery(hql);
query.setFirstResult(0);
query.setMaxResults(2);
List<User> userList = query.list();
for(User user:userList){
System.out.println(user.getUserName());
}
session.getTransaction().commit();
|
1.1HQL语句不只支持查询功能,还支持修改以及删除功能
- public void doRemove(Integer id) throws Exception {
-
-
- String hql = "DELETE FROM News AS n WHERE n.id = ?" ;
- Query query = HibernateSessionFactory.getSession().createQuery(hql);
- query.setInteger(0, id);
- query.executeUpdate();
- }
注:使用HQL的删除可以不需要先查询,直接删除,支持一次删除多条数据
开发中的选择:1)当删除一条数据时,直接使用session.delete()就可以,因为简单,hibernate不在乎那点查询的性能;
2)批量删除时,使用Hql形式,这是可以提高性能的方法,因为它中间省去了查询的步骤。
1.2HQL语句修改功能
- public void doUpdate(News vo) throws Exception {
-
- String hql = "UPDATE News AS n SET n.title = ?,n.content = ? WHERE n.id = ?" ;
- Query query = HibernateSessionFactory.getSession().createQuery(hql);
- query.setString(0, vo.getTitle());
-
- query.executeUpdate();
- }
开发中的选择:1)如果是直接的修改功能,选择session.update()方法;
2)如果是只修改某一字段,使用HQL方式。
注:HQL语句不支持添加,但是Query支持添加。
1.3针对HQL的查询功能,也支持写SELECT,可以通过编写SELECT,来只查询对象中某一个或某几个属性。
但是对于多种字段不同类型的查询返回的,Hibernate中只能是数组。
例如:
- public List testHQL() throws Exception {
- String hql = "SELECT n.id,n.title FROM News AS n";
- Query query = HibernateSessionFactory.getSession().createQuery(hql);
- return query.list();
- }
经过测试,发现当只查询一个字段时,直接返回该类型的List集合。
但查询两个以上的字段时,返回的是List<Object[]>,每一条查询出的数据,使用Object[]来表示,这就很不方便。
- public void testHQL() throws Exception {
- List all = ServiceFactory.getINewsServiceInstance().testHQL();
- Object[] value1 = (Object[]) all.get(0);
- System.out.println(value1[1]);
- }
这样使用起来很麻烦,因此在Hibernate3.2以上的版本中,提供了一个自动转换类,可以将查询出的Object[],自动转换为pojo 对象。
- public List testHQL() throws Exception {
- String hql = "SELECT n.id AS id,n.title AS title FROM News AS n";
- Query query = HibernateSessionFactory.getSession().createQuery(hql);
- <span style="color:#cc0000;">query
- .setResultTransformer(new AliasToBeanResultTransformer(
- News.class));</span>
- return query.list();
- }
注:一般开发中不会使用这种方法,只有当表中的字段过多,但查询只需要其中的几个字段时,才会用到这种方法。
1.4Hibernate还可以将语句写到配置文件中
- <query name="findAll">
- FROM News AS n WHERE n.title LIKE ?
- </query>
通过程序读取配置文件,取得这段HQL,并生成Query对象,完成查询。
- Query query = HibernateSessionFactory.getSession().getNamedQuery(
- "findAll");
- query.setString(0, "%测试%");
- return query.list();
这种方式在Mybatis中普遍使用,但是在Hibernate中一般很少这样做。
二.QBC(Query By Criteria)查询
? Criteria对象提供了一种面向对象的方式查询数据库。Criteria对象需要使用Session对象来获得。
? 一个Criteria对象表示对一个持久化类的查询。
查询所有
1
2
3
4
5
6
7
8
9
10
11
|
session.beginTransaction();
Criteria c = session.createCriteria(User.class);
List<User> userList = c.list();
for(User user:userList){
System.out.println(user.getUserName());
}
session.getTransaction().commit();
|
或
- public List<News> testCriteria() throws Exception {
-
- Criteria c = HibernateSessionFactory.getSession().createCriteria(
- News.class);
- return c.list();
- }
如果想加入查询条件,需要使用Restrictions的各种方法来完成条件的拼写。
1
2
3
4
5
6
7
8
9
10
11
|
session.beginTransaction();
Criteria c = session.createCriteria(User.class);
c.add(Restrictions.eq("userName", "James"));
List<User> userList = c.list();
for(User user:userList){
System.out.println(user.getUserName());
}
session.getTransaction().commit();
|
或
- public List<News> testCriteria() throws Exception {
-
- Criteria c = HibernateSessionFactory.getSession().createCriteria(
- News.class);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- c
- .add(Restrictions.or(Restrictions.eq("id", 23), Restrictions
- .and(Restrictions.ne("id", 26), Restrictions.like(
- "title", "%测试%"))));
- return c.list();
- }
如果想加入ORDER BY排序条件,需要使用Order对象。
- c.addOrder(Order.desc("id"));
如果想加入统计函数和分组函数,则需要用到Projection这个类
- <span style="white-space:pre"> </span>ProjectionList pro = Projections.projectionList();
-
- pro.add(Projections.rowCount());
-
- pro.add(Projections.groupProperty("title"));
- c.setProjection(pro);
Restrictions对象
1
2
3
4
5
6
7
8
9
10
11
12
|
方法名称 对应SQL中的表达式
----------------------------------------------------------
Restrictions.eq field = value
Restrictions.gt field > value
Restrictions.lt field < value
Restrictions.ge field >= value
Restrictions.le field <= value
Restrictions.between field between value1 and value2
Restrictions.in field in(…)
Restrictions.and and
Restrictions.or or
Restrictions.like field like value
|
示例
1
2
3
4
5
6
|
Criteria c = session.createCriteria(User.class);
c.add(Restrictions.like("userName", "J"));
c.add(Restrictions.eq("id", 120));
c.add(Restrictions.or(Restrictions.eq("userName", "James"),
Restrictions.eq("userName", "Alex")));
|
获取唯一记录
1
2
3
4
5
6
7
8
9
10
|
session.beginTransaction();
Criteria c = session.createCriteria(User.class);
c.add(Restrictions.eq("id", 120));
User user = (User) c.uniqueResult();
System.out.println(user.getUserName());
session.getTransaction().commit();
|
分页
1
2
3
4
|
Criteria c = session.createCriteria(User.class);
c.setFirstResult(0);
c.setMaxResults(5);
|
1
2
3
4
5
6
7
8
9
10
|
session.beginTransaction();
Criteria c = session.createCriteria(User.class);
c.setProjection(Projections.sum("id"));
Object obj = c.uniqueResult();
System.out.println(obj);
session.getTransaction().commit();
|
Projections对象
1
2
3
4
5
6
7
8
9
|
方法名称 描述
-------------------------------------------------------
Projections.sum 等于SQL中聚合函数sum
Projections.avg 等于SQL中聚合函数avg
Projections.count 等于SQL中聚合函数count
Projections .distinct 去除重复记录
Projections.max 等于SQL中聚合函数max
Projections.min 等于SQL中聚合函数min
Projections .groupProperty 对指定的属性进行分组查询
|
多个统计与分组
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
session.beginTransaction();
Criteria c = session.createCriteria(User.class);
ProjectionList projectionList = Projections.projectionList();
projectionList.add(Projections.sum("id"));
projectionList.add(Projections.min("id"));
c.setProjection(projectionList);
// 和HQL一样,单列用Object,多列用Object[]
Object[] obj = (Object[]) c.uniqueResult();
System.out.println("sum:" + obj[0]);
System.out.println("min:" + obj[1]);
|
排序
1
2
3
|
Criteria c = session.createCriteria(User.class);
c.addOrder(Order.desc("id"));
|
示例
1
2
3
4
5
6
7
8
9
10
11
12
|
session.beginTransaction();
String sql = "select id,username,userpwd from t_user";
List list = session.createSQLQuery(sql).list();
for(Object item : list){
Object[] rows = (Object[]) item;
System.out.println("id:" + rows[0] + "username:"
+ rows[1] + "userpwd:" + rows[2]);
}
session.getTransaction().commit();
|
addEntity()示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
session.beginTransaction();
String sql = "select id,username,userpwd from t_user";
// addEntity()可以告诉Hibernate你想要封装成对象的类型,然后自动为你封装
SQLQuery query = session.createSQLQuery(sql).addEntity(User.class);
List<User> list = query.list();
for(User user : list){
System.out.println(user.getUserName());
}
session.getTransaction().commit();
|
uniqueResult示例
1
2
3
4
5
6
7
8
9
10
|
session.beginTransaction();
String sql = "select id,username,userpwd from t_user where id = 2";
SQLQuery query = session.createSQLQuery(sql).addEntity(User.class);
User user = (User) query.uniqueResult();
System.out.println(user.getUserName());
session.getTransaction().commit();
|