Criteria criteria = session.createCriteria(Employee.class); //1. AND: 使用Conjunction表示 //Conjunction 本身就是一个Criterion对象 //且其中还可以添加Criterion对象 Conjunction conjunction = Restrictions.conjunction(); conjunction.add(Restrictions.like("name", "a", MatchMode.ANYWHERE)); Department dept = new Department(); dept.setId(80); conjunction.add(Restrictions.eq("eq", dept)); System.out.println(conjunction); //2: OR Disjunction disjunction = Restrictions.disjunction(); disjunction.add(Restrictions.ge("salary", 6000F)); disjunction.add(Restrictions.isNull("email")); criteria.add(disjunction); criteria.add(conjunction); criteria.list();
结果:
... where (this_.SALARY >= ? or this_.EMAIL is null) and (this_.NAME like ? and this_.DEPT_ID = ?)
时间: 2024-10-09 06:12:43