Hibernate Criteria查询

查询所有数据:
Criteria c = s.createCriteria(Student.class);
List list = c.list();

精确查询:
c.add(Restrictions.eq("id", new Integer(22)));

模糊查询:
c.add(Restrictions.like("name", "%ylm%"));

条件查询:
c.add(Restrictions.ge("id", new Integer(9)));

范围查询:
c.add(Restrictions.between("id", 5, 55));

逻辑语句:
and
c.add(Restrictions.ge("id", 5));
c.add(Restrictions.le("age", 25));

or
c.add(Restrictions.or(Restrictions.ge("id", 5), Restrictions.le("age", 25)));

not
c.add(Restrictions.not(Restrictions.ge("id", 5)));

分页:
c.setFirstResult(3);//从第3行开始取数据
c.setMaxResults(3);//总共取3行数据
List list = c.list();

排序:
c.addOrder(Order.desc("id"));

分组:
c.setProjection(Projections.groupProperty("age"));

查询的结果只有一个时:
Object obj = c.uniqueResult();

统计函数的使用:
avg() rowCount() count() max() min() countDistinct()
c.setProjection(Projections.avg("age"));

 

更多功能参考:

http://wenku.baidu.com/view/1a913b0103d8ce2f0066231e.html###

你可能感兴趣的:(Hibernate,list,Criteria,uniqueResult)