1.查询所有
Criteria criteria=session.createCriteria(Dept.class);
//调用list()方法
List<Dept> list=criteria.list();
for (Dept dept : list) {
System.out.println(dept.getName());
}
2.查询不属于任何部门的员工
List<Emp> list=session.createCriteria(Emp.class).
add(Restrictions.isNull("dept")).list();
for (Emp emp : list) {
System.out.println(emp.getName());
}
3.查询职位是"教员"或"班主任"
List jobList=new ArrayList();
jobList.add("教员");
jobList.add("班主任");
List<Emp> list=session.createCriteria(Emp.class).
add(Restrictions.in("job", jobList)).list();
for (Emp emp : list) {
System.out.println(emp.getName());
}
4.查询工作从2000到5000范围的
List<Emp> list=session.createCriteria(Emp.class).
add(Restrictions.between("salary", 2000D, 5000D)).list();
for (Emp emp : list) {
System.out.println(emp.getName());
}
5.查询职位是1或2
List<Emp> list=session.createCriteria(Emp.class).
add(Restrictions.or(Restrictions.eq("job", "教员"), Restrictions.eq("job", "班主任"))).list();
for (Emp emp : list) {
System.out.println(emp.getName());
}
6.查询姓名中包含X的员工
List<Emp> list=session.createCriteria(Emp.class).
add(Restrictions.ilike("name", "三", MatchMode.ANYWHERE)).list();
for (Emp emp : list) {
System.out.println(emp.getName());
}
7.没有员工的部门
List<Dept> list=session.createCriteria(Dept.class).
add(Restrictions.isEmpty("emps")).list();
for (Dept dept : list) {
System.out.println(dept.getName());
}