Query接口
Query类型与org.hibernate.Criteria很接近,封装了对底层数据库表的查询请求
Query接口的常用方法
list()
iterate()
setMaxResults()
setFirstResult()
uniqueResult()
HQL查询
1.查询所有持久化对象
Transaction tx=session.beginTransaction();
Query query=session.ccreateQuery("from Testbook");
List list=query.list();
tx.commit();
2.投影查询
Transaction tx=session.beginTransaction(); Query query=session.ccreateQuery("select title from Testbook"); List<String> list=query.list(); tx.commit(); for(String title : list){ System.out.println(title); }
3.查询持久化对象的多个属性
Transaction tx=session.beginTransaction(); Query query=session.ccreateQuery("select new map(id,name,email) from Testbook"); List<Map> list=query.list(); tx.commit(); for(Map map : list){ System.out.println(map.get("0")+",\t"+map.get("1")+map.get("2")); }
这里的 id,name,email 分别对应 0,1,2
4.HQL 中使用别名
Query query=session.ccreateQuery("select tb name as name from Testbook as tb"); List<String> list=query.list();
可省略as,指定持久化类的别名
Query query=session.ccreateQuery("select tb name as name from Testbook tb"); List<String> list=query.list();
5.别名与属性值组成key/value对
Transaction tx=session.beginTransaction(); Query query=session.ccreateQuery("select new map(tb.id,tb.name,tb.email) from Testbook"); List<Map> list=query.list(); tx.commit(); for(Map map : list){ System.out.println(map.get("id")+",\t"+map.get("name")+map.get("email")); }
6.使用distinct 查询独一无二的
Session session=HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession(); Transaction tx=session.beginTransaction(); Query query=session.ccreateQuery("select distinct name from Testbook"); List<String> list=query.list(); tx.commit(); for(String name : list){ System.out.println(name); }
7.HQL中的运算符
8.where子句,限定查询的条件
Transaction tx=session.beginTransaction(); Query query=session.ccreateQuery("from Testbook where name='Narcissus'"); List<Testbook> list=query.list(); tx.commit();
9.使用between/not between(两者之间)
Transaction tx=session.beginTransaction(); Query query=session.ccreateQuery("from Testbook where id between 1 and 10"); List<Testbook> list=query.list(); tx.commit();
10.使用in/not in(在范围内)
Transaction tx=session.beginTransaction(); Query query=session.ccreateQuery("from Testbook where name in ('Narcissus','Tom')"); List<Testbook> list=query.list(); tx.commit();
11.使用like/not like (模糊查询)
Transaction tx=session.beginTransaction(); Query query=session.ccreateQuery("from Testbook where name like 'Na%'"); List<Testbook> list=query.list(); tx.commit();
12.使用and(几个或者多个条件需要同时满足)/or(只要满足一个条件)/not(条件求反)
Transaction tx=session.beginTransaction(); Query query=session.ccreateQuery("from Testbook where name='Narcissus' and id<100"); List<Testbook> list=query.list(); tx.commit();
13. 使用is null (某个属性值是否为空)/is not null(某属性值不为空)
Transaction tx=session.beginTransaction(); Query query=session.ccreateQuery("from Testbook where email is null"); List<Testbook> list=query.list(); tx.commit();
14.一个或多个熟悉的数据排序(逗号分隔即可)
Transaction tx=session.beginTransaction(); Query query=session.ccreateQuery("from Testbook where name like 'Nar%' order by name,id desc"); List<Testbook> list=query.list(); tx.commit();
15.使用"?"作为参数占位符
Transaction tx=session.beginTransaction(); Query query=session.ccreateQuery("from Testbook where id<?"); query.setInteger(0,12);//起始值从0开始 对应不同的类型 List<Testbook> list=query.list(); tx.commit();
16.使用统计函数
Transaction tx=session.beginTransaction(); Query query=session.ccreateQuery("select min(price),max(price) from Testbook"); Object[] values=(Object[])query.uniqueResult(); tx.commit(); System.out.println("最低价"+"\t"+"最高价"); System.out.println(values[0]+"\t"+values[1]);
17.在分组中应用统计函数
Transaction tx=session.beginTransaction(); Query query=session.ccreateQuery("select avg(price),count(*) from Testbook group by category.id"); List<Object[]> list=query.list(); tx.commit(); System.out.println("平均价"+"\t"+"总数"); for(Object[] values :list){ System.out.println(values[0]+"\t"+values[1]); }
18.使用having(限定条件)
Transaction tx=session.beginTransaction(); Query query=session.ccreateQuery("select avg(price),count(*) from Testbook group by price having price>50.0"); List<Object[]> list=query.list(); tx.commit();
19.使用函数
Transaction tx=session.beginTransaction(); Query query=session.ccreateQuery("select lower(name) from Testbook where id<100"); List<String> list=query.list(); tx.commit();
20.HQL 的批量更新/删除
Transaction tx=session.beginTransaction(); Query query=session.ccreateQuery("update Testbook set name='Narcissus' where id=1"); int rowCount =query.executeUpdate(); tx.commit();
HQL 中的连接查询
1.使用交叉连接
Transaction tx=session.beginTransaction(); String hql="from Product p,Category c"; Query query = session.createQuery(hql); List list=query.list(); tx.commit();
使用内连接
QBC中实现内连接
Transaction tx=session.beginTransaction(); Criteria criteria = session.createCriteria(Product.class).createCriteria("category"); List<Product> list=query.list(); tx.commit();
使用左外连接
使用右外连接
迫切内,左,右(加fetch关键字)连接
Transaction tx=session.beginTransaction(); String hql="select c from Category c join fetch c.products"; Query query = session.createQuery(hql); List<Category> list=query.list(); tx.commit();