package cn.bdqn.test; import java.util.ArrayList; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.criterion.Criterion; import org.hibernate.criterion.MatchMode; import org.hibernate.criterion.Order; import org.hibernate.criterion.Projections; import org.hibernate.criterion.Restrictions; import org.hibernate.criterion.SimpleExpression; import org.junit.Test; import org.junit.experimental.theories.suppliers.TestedOn; import cn.bdqn.bean.Dept; import cn.bdqn.bean.Emp; import cn.bdqn.util.HibernateSessionFactory; /** * * @author Administrator Criteria 相关查询 */ public class CriteriaTest { // 查询所有的部门信息 @Test public void test1() { // 打开session Session session = HibernateSessionFactory.getSession(); // 创建criteria对象 Criteria criteria = session.createCriteria(Dept.class); // 获取集合 List<Dept> list = criteria.list(); for (Dept dept : list) { System.out.println("部门编号:" + dept.getId() + "\t部门名称:" + dept.getName()); } // 关闭session HibernateSessionFactory.closeSession(); } // 查询在一区的部门信息(eq) @Test public void test2() { // 打开session Session session = HibernateSessionFactory.getSession(); /* * //01.创建criteria对象 Criteria criteria = * session.createCriteria(Dept.class); //增加条件 Criterion criterion = * Restrictions.eq("loc", "一区"); criteria=criteria.add(criterion); * List<Dept> list = criteria.list(); */ // 02.拼接 List<Dept> list = session.createCriteria(Dept.class) .add(Restrictions.eq("loc", "一区")).list(); for (Dept dept : list) { System.out.println("部门编号:" + dept.getId() + "\t部门名称:" + dept.getName()); } // 关闭session HibernateSessionFactory.closeSession(); } // 查询员工薪水大于2000的(gt) @Test public void test3() { // 打开session Session session = HibernateSessionFactory.getSession(); List<Emp> list = session.createCriteria(Emp.class) .add(Restrictions.gt("salary", 4000d)).list(); for (Emp emp : list) { System.out.println("员工的姓名:" + emp.getName() + "\t员工薪水:" + emp.getSalary()); } // 关闭session HibernateSessionFactory.closeSession(); } // 查询员工薪水在2000和7000之间的员工(between) @Test public void test4() { // 打开session Session session = HibernateSessionFactory.getSession(); List<Emp> list = session.createCriteria(Emp.class) .add(Restrictions.between("salary", 2000d, 7000D)).list(); for (Emp emp : list) { System.out.println("员工的姓名:" + emp.getName() + "\t员工薪水:" + emp.getSalary()); } // 关闭session HibernateSessionFactory.closeSession(); } // 查询不属于任何部门的员工(isNull) @Test public void test5() { // 打开session Session session = HibernateSessionFactory.getSession(); List<Emp> list = session.createCriteria(Emp.class) .add(Restrictions.isNull("dept")).list(); for (Emp emp : list) { System.out.println("员工的姓名:" + emp.getName() + "\t员工的部门:" + emp.getDept()); } // 关闭session HibernateSessionFactory.closeSession(); } // 查询没有员工的部门(isEmpty) @Test public void test6() { // 打开session Session session = HibernateSessionFactory.getSession(); List<Dept> list = session.createCriteria(Dept.class) .add(Restrictions.isEmpty("emps")).list(); for (Dept dept : list) { System.out.println("没有员工的部门:" + dept.getName()); } // 关闭session HibernateSessionFactory.closeSession(); } // 查询职位是保安的员工 @Test public void test7() { // 打开session Session session = HibernateSessionFactory.getSession(); List<Emp> list = session.createCriteria(Emp.class) .add(Restrictions.eq("job", "保安")).list(); for (Emp emp : list) { System.out.println("员工的姓名:" + emp.getName() + "\t员工的部门:" + emp.getDept()); } // 关闭session HibernateSessionFactory.closeSession(); } // 01.查询职位是保安或者保姆的员工 @Test public void test8() { // 打开session Session session = HibernateSessionFactory.getSession(); 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() + "\t员工的部门:" + emp.getDept()); } // 关闭session HibernateSessionFactory.closeSession(); } // 02.查询职位是保安或者保姆的员工 @Test public void test9() { // 打开session Session session = HibernateSessionFactory.getSession(); //定义一个集合用来保存这两个职位 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() + "\t员工的部门:" + emp.getDept()); } // 关闭session HibernateSessionFactory.closeSession(); } //查询职位是保安或者保姆再或者是保姆1的员工(disjunction适合多条件) @Test public void test10() { // 打开session Session session = HibernateSessionFactory.getSession(); //增加条件 List<Emp> list = session.createCriteria(Emp.class) .add(Restrictions.disjunction() .add(Restrictions.eq("job", "保安")) .add(Restrictions.eq("job", "保姆")) .add(Restrictions.eq("job", "保姆1")) ).list(); for (Emp emp : list) { System.out.println("员工的职位:" + emp.getJob() + "\t员工的部门:" + emp.getDept()); } // 关闭session HibernateSessionFactory.closeSession(); } //查询员工姓名中包含a的所有员工(like) @Test public void test11() { // 打开session Session session = HibernateSessionFactory.getSession(); //增加条件 List<Emp> list = session.createCriteria(Emp.class) .add(Restrictions.like("name", "%a%")).list(); for (Emp emp : list) { System.out.println("员工的姓名:" + emp.getName() + "\t员工的部门:" + emp.getDept()); } // 关闭session HibernateSessionFactory.closeSession(); } //查询员工姓名中包含a的所有员工(ilike 忽略大小写) @Test public void test12() { // 打开session Session session = HibernateSessionFactory.getSession(); //增加条件 List<Emp> list = session.createCriteria(Emp.class) .add(Restrictions.ilike("name", "%a%")).list(); for (Emp emp : list) { System.out.println("员工的姓名:" + emp.getName() + "\t员工的部门:" + emp.getDept()); } // 关闭session HibernateSessionFactory.closeSession(); } /* * 查询员工姓名中包含a的所有员工(ilike 忽略大小写) 不使用% 使用MatchModle.ANYWHERE * ignoreCase()这也是忽略大小写 */ @Test public void test13() { // 打开session Session session = HibernateSessionFactory.getSession(); //增加条件 List<Emp> list = session.createCriteria(Emp.class) .add(Restrictions.ilike("name", "a",MatchMode.ANYWHERE)).list(); for (Emp emp : list) { System.out.println("员工的姓名:" + emp.getName() + "\t员工的部门:" + emp.getDept()); } // 关闭session HibernateSessionFactory.closeSession(); } /* * ************分页************** *查询员工职位是 保安 ,并且按照薪水进行降序排列(Order.desc) * */ @Test public void pageList(){ // 打开session Session session = HibernateSessionFactory.getSession(); //查询总记录数的条件 Criteria criteria = session.createCriteria(Emp.class) .add(Restrictions.eq("job", "保安")).setProjection(Projections.count("id")); //开始查询 int counts = ((Long)criteria.uniqueResult()).intValue(); //每页显示几条记录 int pageSize=2; //总页数 int totalPage=(counts%pageSize==0)?(counts/pageSize):(counts/pageSize+1); //当前页码 int pageIndex=3; //薪水降序 criteria= session.createCriteria(Emp.class) .add(Restrictions.eq("job", "保安")) .addOrder(Order.desc("salary")); //开始分页 List<Emp> list= criteria.setFirstResult((pageIndex-1)*pageSize) .setMaxResults(pageSize).list(); for (Emp emp : list) { System.out.println(emp.getName()+"\t薪水"+emp.getSalary()); } System.out.println("总记录数"+counts); System.out.println("总页数"+totalPage); System.out.println("当前页数"+pageIndex); // 关闭session HibernateSessionFactory.closeSession(); } }