Hibernate的三种查询方式
1.HQL (Hibernate Query Lanuage) 语句,这是类似SQL语句的一种对象化查询语句.
2.使用Criteria对象,进行按条件查询(Query by Criteria,简称QBC)和按示例查询(Query by Example 简称QBE).
3.生成原生的sql(Native SQL)语句.
Criteria的基本使用
Transaction tx = session.beginTransaction(); Criteria criteria = session.createCriteria(Testbook.class); List<Testbook> list=criteria.list(); tx.commit();
查询Testbook 中所有的数据,然后返回
Criteria 接口的核心方法
add() 增加一个代表查询条件的Criterion对象
addOrder() 增加一个代表排序的Criterion对象
createAlias() 创建关联查询,为所关联的持久化类建立别名
createCriteria() 在相互关联的持久化类之间建立条件约束
setFirstResult() 设定要获取的第一条记录的位置
setMaxResults() 设定要获取的记录的最大数量
list() 获取满足查询条件的记录的集合
uniqueResult() 获取满足查询条件的唯一记录
对查询结果进行排序
Transaction tx = session.beginTransaction(); Criteria criteria = session.createCriteria(Testbook.class).add( Restrictions.like("name","任%")).add(Restrictions.between("id",1000,1010)); criteria.addOrder(Order.desc("id")).addOrder(Order.asc("createdTime")); List<Testbook> list=criteria.list(); tx.commit();
实现分页显示数据
Transaction tx = session.beginTransaction();
Criteria criteria = session.createCriteria(Testbook.class).add(
Restrictions.like("name","任%"));
criteria.addOrder(Order.desc("id"));
criteria.setMaxResults(30);
criteria.setFirstResult(10);//setFirstResult()从0开始计数
List<Testbook> list=criteria.list();
tx.commit();
只查询一条记录
Transaction tx = session.beginTransaction(); Criteria criteria = session.createCriteria(Testbook.class).add( Restrictions.like("name","任%")); criteria.addOrder(Order.desc("id")); criteria.setMaxResults(1); Testbook tb=(Testbook)criteria.uniqueResult(); tx.commit();
Restrictions类
<----------------------------------------------------------------------------------------------------------->
条件"或"方式组合查询条件
Transaction tx = session.beginTransaction(); Criteria criteria=session.createCriteria(Testbook.class); Criterion criterion1=Restrictions.or(Restrictions.like("name","任%"), Restrictions.between("id",1000,1010)); criteria.add(criterion1); List<Testbook> list=criteria.list(); tx.commit();
使用原生SQL语句设定查询条件
Transaction tx = session.beginTransaction(); Criteria criteria=session.createCriteria(Testbook.class); criteria.add(Restrictions.sqlRestriction("{alias}.name=?","Narcissus",Hibernate.STRING)); List<Testbook> list=criteria.list(); tx.commit()
统计函数查询
Transaction tx = session.beginTransaction(); Criteria criteria=session.createCriteria(Testbook.class); criteria.setProjection(Projections.max("price")); Double maxValue=(Double)criteria.uniqueResult(); tx.commit(); System.out.println(maxValue);
统计函数组合查询条件
Transaction tx = session.beginTransaction(); Criteria criteria=session.createCriteria(Testbook.class); criteria.setProjection(Projections.max("price")); Double maxValue=(Double)criteria.uniqueResult(); tx.commit(); System.out.println(maxValue);
获取多个统计函数的值
Transaction tx = session.beginTransaction(); Criteria criteria=session.createCriteria(Testbook.class); ProjectionList proList=Projections.projectionList(); proList.add(Projections.max("price")); proList.add(Projections.min("price")); proList.add(Projections.avg("price")); proList.add(Projections.sum("price")); proList.add(Projections.count("price")); proList.add(Projections.countDistinct("price")); proList.add(Projections.rowCount()); criteria.setProjection(proList); Object values[]=(Object[])criteria.uniqueResult(); tx.commit();
分组查询
Transaction tx = session.beginTransaction(); Criteria criteria=session.createCriteria(Testbook.class); criteria.setProjection(Projections.groupProperty("price")); List<Double> list=criteria.list(); tx.commit();
分组统计
Transaction tx = session.beginTransaction(); Criteria criteria=session.createCriteria(Testbook.class); ProjectionList proList=Projections.projectionList(); proList.add(Projections.rowCount());//商品的个数 proList.add(Projections.sum("price")); proList.add(Projections.groupProperty("name")); criteria.setProjection(proList); List<Double> list=criteria.list(); tx.commit();
投影查询
在一个表中有很多个字段,投影查询只是查询某一个或多个字段,也称局部查询
Transaction tx = session.beginTransaction(); Criteria criteria=session.createCriteria(Testbook.class); criteria.setProjection(Property.forName("price")); criteria.add(Restrictions.gt("price",new Double(40.0))) List<Double> list=criteria.list(); tx.commit();
使用Property类的forName()方法实现分组统计
Transaction tx = session.beginTransaction(); Criteria criteria=session.createCriteria(Testbook.class); ProjectionList proList=Projections.projectionList(); proList.add(Projections.forName("price").max()); proList.add(Projections.forName("price").min()); proList.add(Projections.forName("price").avg()); proList.add(Projections.forName("category.id").group().as("c")); criteria.setProjection(proList); criteria.addOrder(Order.desc("c")); List<Object[]> list=criteria.list(); tx.commit();
使用DetachedCriteria对象实现离线查询
//在表现层,构造DetachedCriteria对象,保存用户动态的查询条件 DetachedCriteria detchedCriteria=DetachedCriteria.forClass(Testbook.class); detchedCriteria.add(Restrictions.like("name","任%")).add(Restrictions.between("id",new Integer(1),new Integer(10))); detchedCriteria.addOrder(Order.desc("id")); //在业务逻辑层,DetachedCriteria对象与当前的Session对象进行绑定,获取查询结果 Session session=HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession(); Transaction tx=detchedCriteria.getExecutableCriteria(session); List<Testbook> list=criteria.list(); tx.commit();
根据示例对象进行查询
先定义一个模板然后把这个模板的样式传进去,查询,例如这里定义的Testbook中的名字为Narcissus的字段,进行查询,条件可以为一个也可以为多个,对应的就是where中的条件
Testbook tb=new Testbook(); tb.setName("Narcissus"); Session session=HibernateSessionFactoryUtil.getSessionFactory().getCurrentSession(); Transaction tx = session.beginTransaction(); Criteria criteria=session.createCriteria(Testbook.class); Criteria.add(Example.creaate(tb)); List<Testbook> list=criteria.list(); tx.commit();