QBC:Query By Criteria
- 条件查询
- 比较适合组合条件查询
QBC查询
简单查询
创建Criteria对象:Criteria criteria = session.createCriteria(Children.class);
获取查询的对象集合:List<Children> list = criteria.list();
@Test
public void query() {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
Criteria criteria = session.createCriteria(Children.class);
List<Children> list = criteria.list();
for (Children children : list) {
System.out.println(children);
}
tx.commit();
}
排序查询
addOrder()
- 添加排序方法
Order.desc(属性名)、Order.asc(属性名)
- criteria.addOrder(Order.desc("cid"));//降序
- criteria.addOrder(Order.asc("cid"));//升序
@Test
public void query() {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
Criteria criteria = session.createCriteria(Children.class);
criteria.addOrder(Order.desc("cid"));//降序
//criteria.addOrder(Order.asc("cid"));//升序
List<Children> list = criteria.list();
for (Children children : list) {
System.out.println(children);
}
tx.commit();
}
分页查询
从第几条记录开始:criteria.setFirstResult(int);
一页总共多少条记录:criteria.setMaxResults(int);
@Test
public void query() {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
Criteria criteria = session.createCriteria(Children.class);
criteria.setFirstResult(3);
criteria.setMaxResults(5);
List<Children> list = criteria.list();
for (Children children : list) {
System.out.println(children);
}
tx.commit();
}
条件查询
criteria.add(Restrictions.eq("sex", '0'));
- add():主要用于添加where后面的条件
- Restrictions:条件
- 条件设置方法
- eq():=
- gt():>
- ge:>=
- le:<=
- lt:<
- ne:<>
- like()、in()、and()、or()、...........
@Test
public void query() {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
Criteria criteria = session.createCriteria(Children.class);
criteria.add(Restrictions.eq("sex", '0'));
List<Children> list = criteria.list();
for (Children children : list) {
System.out.println(children);
}
tx.commit();
}
分组查询
criteria.setProjection(Projection)
- 添加聚合函数以及group by 、having 的条件
@Test
public void query() {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
Criteria criteria = session.createCriteria(Children.class);
criteria.setProjection(Projections.count("sex"));
Object obj = criteria.uniqueResult();
System.out.println(obj);
tx.commit();
}
离线条件查询
DetachedCriteria类:可以脱离session使用
使用DetachedCriteria的原因
- 多条件查询的时,需要在Web层封装相应的参数到Criteria,而在Dao层才真正的去执行查询数据库的操作
- session是在dao层开始创建使用的,web层一般不出现session,所以需要从web传递参数并且在dao层拼接sql
- DetachedCriteria可以脱离session使用,可以在web层将查询条件设置完毕,然后传递DetachedCriteria对象即可,不需要传递大量参数、拼接sql
DetachedCriteria的使用
- 创建:DetachedCriteria dc = DetachedCriteria.forClass(Children.class);
- 添加查询条件:dc.add(Restrictions.eq("sex", '1'));
- 绑定session并获取Criteria对象:Criteria criteria = dc.getExecutableCriteria(session);
@Test
public void query() {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
//模拟web层,不需要session的创建
DetachedCriteria dc = DetachedCriteria.forClass(Children.class);
dc.add(Restrictions.eq("sex", '1'));
//模拟dao层,绑定session使用
Criteria criteria = dc.getExecutableCriteria(session);
List<Children> list = criteria.list();
for (Children children : list) {
System.out.println(children);
}
tx.commit();
}