在使用Hibernate时,即使我们不了解SQL的使用与撰写,也可以使用它所提供的API来进行SQL语句查询 org.hibernate.Criteria对SQL进行封装,我们可以根据具体情况组合各种查询条件,并由Hibernate自动为您产生SQL语句,而不用特别管理SQL与资料库相依的问题。
Criteria查询采用面向对象方式封装查询条件,又称为对象查询
Criteria由HibernateSession进行创建 Criteria criteria =session.createCriteria(Dept.class); ++++++>> select * from dept;
/** * (1)使用Criteria查询所有的部门 */ @Test public void test1() { Session session = HibernateUtil.currentSession(); Transaction tx = session.beginTransaction(); try { // 获取当前的对象查询 Criteria criteria = session.createCriteria(Dept.class); // 返回集合对象? List<Dept> list = criteria.list(); System.out.println(list.size()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { session.close(); } }
/** /* (2)查询位置在“NEW YORK”的部门 使用Criteria的add()方法加入“条件实例”, */ @Test public void test2() { Session session = HibernateUtil.currentSession(); Transaction tx = session.beginTransaction(); try { // 获取当前的对象查询 Criteria criteria = session.createCriteria(Dept.class).add( Restrictions.eq("loc", "NEW YORK")); // 条件条件等同 // 返回集合对象? List<Dept> list = criteria.list(); System.out.println(list.size()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { session.close(); } }
@Test public void test4() { Session session = HibernateUtil.currentSession(); Transaction tx = session.beginTransaction(); try { // 获取当前的对象查询 Criteria criteria = session .createCriteria(Emp.class, "e") // "e"别名 .add(Restrictions.ne("e.dept.deptno", 10L)) // deptno<>10 .add(Restrictions.in("job", new Object[] { "CLERK", "ANALYST" })) // job in ("CLERK","ANALYST") .add(Restrictions.like("ename", "%S")) // ename like '%S' .add(Restrictions.lt("sal", 1000D)) // sal<1000 ; // 返回集合对象? List<Emp> list = criteria.list(); for (Emp emp : list) { System.out.println(emp.getEname() + "\t" + emp.getJob() + "\t" + emp.getSal() + "\t" + emp.getDept().getDeptno()); } System.out.println(list.size()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { session.close(); } }结果集排序:使用org.hibernate.criterion.Order
@Te public void test5() { Session session = HibernateUtil.currentSession(); Transaction tx = session.beginTransaction(); try { // 获取当前的对象查询 Criteria criteria = session.createCriteria(Emp.class, "e") // "e"别名 .add(Restrictions.ilike("ename", "S", MatchMode.ANYWHERE)) // ename // like // '%S%' // .add(Restrictions.ilike("ename", "S", MatchMode.START)) // //ename like 'S%' // .add(Restrictions.ilike("ename", "S", MatchMode.END)) // //ename like '%S' // .add(Restrictions.ilike("ename", "SCOTT", // MatchMode.EXACT)) //ename ='SCOTT' // .add(Restrictions.and(Restrictions.eq("job", "CLERK"), // Restrictions.ge("sal", 1000D))) .addOrder(Order.desc("sal")) // order by sal desc ; // 返回集合对象? List<Emp> list = criteria.list(); for (Emp emp : list) { System.out.println(emp.getEname() + "\t" + emp.getJob() + "\t" + emp.getSal() + "\t" + emp.getDept().getDeptno()); } System.out.println(list.size()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { session.close(); } }
/** * 1.查询部门名称 * * select dname from dept; * */ @Test public void test1() { Session session = HibernateUtil.currentSession(); Transaction tx = session.beginTransaction(); try { // 获取当前的对象查询 Criteria criteria = session.createCriteria(Dept.class) .setProjection(Projections.property("dname")); // 返回字符串 // java.lang.ClassCastException: java.lang.String cannot be cast to // com.hlx.entity.Dept List<String> list = criteria.list(); for (String dept : list) { System.out.println(dept); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { session.close(); } }
/** * * * 2.使用setProjection(),查询员工姓名和入职时间 * * select ename,hiredate from emp; * * */ @Test public void test2() { Session session = HibernateUtil.currentSession(); Transaction tx = session.beginTransaction(); try { // 获取当前的对象查询 Criteria criteria = session.createCriteria(Emp.class) .setProjection( Projections.projectionList() .add(Projections.property("ename")) .add(Projections.property("hiredate")) ) ; // 返回字符串 // java.lang.ClassCastException: java.lang.String cannot be cast to // com.hlx.entity.Dept List<Object[]> list = criteria.list(); for (Object[] dept : list) { System.out.println(dept[0] + "\t" + Timestamp.valueOf(dept[1].toString())); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { session.close(); } }
/** * * * * 3.统计各个部门的平均工资、最高工资、最低工资 * * select deptno,avg(sal),max(sal),min(sal) from emp group by deptno order * by deptno desc */ @Test public void test3() { Session session = HibernateUtil.currentSession(); Transaction tx = session.beginTransaction(); try { // 获取当前的对象查询 Criteria criteria = session .createCriteria(Emp.class, "e") .setProjection( Projections .projectionList() .add(Projections .groupProperty("e.dept.deptno")) .add(Projections.avg("sal")) .add(Projections.max("sal")) .add(Projections.min("sal"))) .addOrder(Order.desc("e.dept.deptno")); List<Object[]> list = criteria.list(); for (Object[] dept : list) { System.out.println(dept[0] + "\t" + dept[1] + "\t" + dept[2] + "\t" + dept[3]); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { session.close(); } }
dfdf
/** * 分页 criteria.setFirstResult() criteria.setMaxResults() */ @Test public void testPage() { Session session = HibernateUtil.currentSession(); Transaction tx = session.beginTransaction(); int pageIndex = 1; // 当前页 int pageSize = 2; // 每页显示的个数 // 总个数 int pageCount = session.createCriteria(Emp.class, "e") .add(Restrictions.eq("job", "CLERK")).list().size(); // 总页数 int pageTotal = (pageCount % pageSize == 0) ? (pageCount / pageSize) : (pageCount / pageSize + 1); System.out.println(pageTotal); try { // 循环 for (int i = pageIndex; i <= pageTotal; i++) { System.out.println("第----->"+i); // 获取当前的对象查询 Criteria criteria = session.createCriteria(Emp.class, "e") .add(Restrictions.eq("job", "CLERK")) .setFirstResult((i - 1) * pageSize) .setMaxResults(pageSize); List<Emp> list = criteria.list(); for (Emp emp : list) { System.out.println(emp.getEname() + "\t" + emp.getJob() + "\t" + emp.getSal() + "\t" + emp.getDept().getDeptno()); } System.out.println("----------------------"); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { session.close(); } }