Hibernate的批量查询——Criteria
Posted Dream
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hibernate的批量查询——Criteria相关的知识,希望对你有一定的参考价值。
1、查询所有的学生信息:
public static void testSel() { Session session = HibernateUtils.openSession(); Transaction transaction = session.beginTransaction(); Criteria criteria=session.createCriteria(Student.class); List<Student>list= criteria.list(); System.out.println(list); transaction.commit(); session.close(); }
2、条件查询:
运算符与对应的英文缩写:
> gt >=ge <lt <=le ==eq
!=ne in:in between and::between
like:like is not null:isNotNull
or:or and:and
(1)eq的运用:
public static void testSel() { Session session = HibernateUtils.openSession(); Transaction transaction = session.beginTransaction(); Criteria criteria=session.createCriteria(Student.class); criteria.add(Restrictions.eq("sex","女")); List<Student>list= criteria.list(); System.out.println(list); transaction.commit(); sessio
(2)like的运用:
public static void testSel() { Session session = HibernateUtils.openSession(); Transaction transaction = session.beginTransaction(); Criteria criteria=session.createCriteria(Student.class); criteria.add(Restrictions.like("sname","许__")); List<Student>list= criteria.list(); System.out.println(list); transaction.commit(); session.close(); }
3、分页查询:
需要事先设置起始位置和查询的数量。
public static void testSel() { Session session = HibernateUtils.openSession(); Transaction transaction = session.beginTransaction(); Criteria criteria=session.createCriteria(Student.class); criteria.add(Restrictions.eq("sex","女")); criteria.setFirstResult(1); criteria.setMaxResults(2); List<Student>list= criteria.list(); System.out.println(list); transaction.commit(); session.close(); }
4、总记录数的获取:
public static void testSel() { Session session = HibernateUtils.openSession(); Transaction transaction = session.beginTransaction(); Criteria criteria=session.createCriteria(Student.class); criteria.setProjection(Projections.rowCount());//聚合函数获得总行数 Long rowNum= (Long) criteria.uniqueResult(); System.out.println(rowNum); List<Student>list= criteria.list(); System.out.println(list); transaction.commit(); session.close(); }
5、排序:
升序:
public static void testSel() { Session session = HibernateUtils.openSession(); Transaction transaction = session.beginTransaction(); Criteria criteria=session.createCriteria(Student.class); criteria.addOrder(Order.asc("studentno")); List<Student>list= criteria.list(); System.out.println(list); transaction.commit(); session.close(); }
降序:
public static void testSel() { Session session = HibernateUtils.openSession(); Transaction transaction = session.beginTransaction(); Criteria criteria=session.createCriteria(Student.class); criteria.addOrder(Order.desc("studentno")); List<Student>list= criteria.list(); System.out.println(list); transaction.commit(); session.close(); }
5、Criteria 和 DetachedCriteria的区别
Criteria 和 DetachedCriteria 的主要区别在于创建的形式不一样, Criteria 是在线的,所以它是由 Hibernate Session 进行创建的;而 DetachedCriteria 是离线的,创建时无需Session。
例如下面的条件查询代码写在servlet中,没有与session进行连接:
String cust_name = request.getParameter("cust_name"); DetachedCriteria dc = DetachedCriteria.forClass(Customer.class); if(cust_name!=null && !"".equals(cust_name)){ dc.add(Restrictions.like("cust_name", "%"+cust_name+"%")); }
以上是关于Hibernate的批量查询——Criteria的主要内容,如果未能解决你的问题,请参考以下文章