Hibernate必知必会——criteria使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010871004/article/details/81436025

criteria

通常我们在进行数据库查询的时候都会加上条件进行数据的筛选,例如where语句,在MySQL中我们还用limit进行数据的分页查询。

利用hibernate进行数据查询

查询所有的数据

通常我们通过criteria.list()方法来查询某个实体类的所有数据

public class App4 {

    public static void main(String[] args) {
        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        session.getTransaction().begin();
        Criteria criteria = session.createCriteria(Employee.class);
        List<Employee> employees = criteria.list();
        for (int i = 0; i < employees.size(); i++) {
            System.out.println(employees.get(i));
        }
        session.getTransaction().commit();
        session.close();
        HibernateUtil.closeSessionFactory();
    }
}

这样便可以查询出来所有的数据

Employee [id=1, firstName=Yogesh, salary=50000.0, department=Department [id=1, deptName=development]]
Employee [id=2, firstName=Aarush, salary=35000.0, department=Department [id=1, deptName=development]]
Employee [id=4, firstName=Vishal, salary=75000.0, department=Department [id=2, deptName=R&D]]
Employee [id=3, firstName=Varsha, salary=30000.0, department=Department [id=3, deptName=UI/UX]]

添加条件对数据进行筛选

要对数据进行筛选,需要使用到Restrictions类,该类提供了大量的方法,对数据进行筛选。

  • gt:数据库字段大于某个值
  • ge:数据库字段大于等于某个值
  • lt:数据库字段小于某个值
  • eq:数据库字段等于某个值
  • ne:数据库字段不等于某个值
  • like:like匹配
  • iLike:忽略大小写like匹配
    除了这些,还提供了not,between,in,or,isNull,isNotNull,isEmpty等方法。
public class App4 {

    public static void main(String[] args) {
        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        session.getTransaction().begin();
        Criteria criteria = session.createCriteria(Employee.class);
        criteria.add(Restrictions.gt("salary", 35000d));
        criteria.add(Restrictions.like("firstName", "%V%"));
        List<Employee> employees = criteria.list();
        for (int i = 0; i < employees.size(); i++) {
            System.out.println(employees.get(i));
        }
        session.getTransaction().commit();
        session.close();
        HibernateUtil.closeSessionFactory();
    }
}

执行结果

Employee [id=4, firstName=Vishal, salary=75000.0, department=Department [id=2, deptName=R&D]]

分页查询

分页查询需要用到两个方法,criteria.setFirstResult()和criteria.setMaxResults()。setFirstResult是设置从第几个开始查询,setMaxResults是设置每次查询多少条数据。

public class App4 {

    public static void main(String[] args) {
        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        session.getTransaction().begin();
        Criteria criteria = session.createCriteria(Employee.class);
        criteria.setFirstResult(1);
        criteria.setMaxResults(5);
        List<Employee> employees = criteria.list();
        for (int i = 0; i < employees.size(); i++) {
            System.out.println(employees.get(i));
        }
        session.getTransaction().commit();
        session.close();
        HibernateUtil.closeSessionFactory();
    }
}

查询结果

Employee [id=2, firstName=Aarush, salary=35000.0, department=Department [id=1, deptName=development]]
Employee [id=4, firstName=Vishal, salary=75000.0, department=Department [id=2, deptName=R&D]]
Employee [id=3, firstName=Varsha, salary=30000.0, department=Department [id=3, deptName=UI/UX]]

猜你喜欢

转载自blog.csdn.net/u010871004/article/details/81436025
0条评论
添加一条新回复