码迷,mamicode.com
首页 > Web开发 > 详细

Hibernate的批量查询——Criteria

时间:2019-11-10 11:38:44      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:批量查询   null   lis   info   static   span   rownum   test   英文   

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();
        }

技术图片

 

 

 

 

Hibernate的批量查询——Criteria

标签:批量查询   null   lis   info   static   span   rownum   test   英文   

原文地址:https://www.cnblogs.com/zhai1997/p/11829304.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!