常见Hql查询
@Test public void test01(){ Session session =null; try { session = HibernateUtil.openSession(); /** * 对于HQL而言,都是基于对象进行查询的 */ Query query=session.createQuery("from Special"); List<Special> list=query.list(); for(Special s:list){ System.out.println(s.getName()+"..."+s.getType()); } } catch (Exception e) { e.printStackTrace(); }finally{ if(session!=null) session.close(); } } @Test public void test02(){ Session session =null; try { session = HibernateUtil.openSession(); //List<Special> list=session.createQuery("select * from Special ") 不能写* /** * 可以使用链式查询方式 */ List<Special> list=session.createQuery("select spe from Special spe") .list(); for(Special s:list){ System.out.println(s.getName()+"..."+s.getType()); } } catch (Exception e) { e.printStackTrace(); }finally{ if(session!=null) session.close(); } } /** * * @Title: 使用HQL 加条件查询 * @Description: * @param * @return void 返回类型 * @throws */ @Test public void test03(){ Session session =null; try { session=HibernateUtil.openSession(); List<Student> list=session.createQuery("from Student where name like '%张%' ") .list(); for(Student s:list){ System.out.println(s.getName()+" "+s.getSex()); } } catch (Exception e) { e.printStackTrace(); }finally{ if(session!=null) session.close(); } } /** * * @Title: 使用HQL 加条件查询 ? 的方式 * @Description: * @param * @return void 返回类型 * @throws */ @Test public void test04(){ Session session =null; try { session=HibernateUtil.openSession(); /** * 基于?的条件的查询,特别注意:jdbc设置参数的最小下标是1,hibernate 是0 */ List<Student> list=session.createQuery("from Student where name like ? ") .setParameter(0, "%李%").list(); for(Student s:list){ System.out.println(s.getName()+" "+s.getSex()); } } catch (Exception e) { e.printStackTrace(); }finally{ if(session!=null) session.close(); } } /** * * @Title: test05 * @Description: 基于别名进行查询 * @param * @return void 返回类型 * @throws */ @Test public void test05(){ Session session =null; try { session=HibernateUtil.openSession(); /** * 基于?的条件的查询,特别注意:jdbc设置参数的最小下标是1,hibernate 是0 */ List<Student> list=session.createQuery("from Student where name like :name and sex=:sex ") .setParameter("name", "%李%") .setParameter("sex","男") .list(); for(Student s:list){ System.out.println(s.getName()+" "+s.getSex()); } } catch (Exception e) { e.printStackTrace(); }finally{ if(session!=null) session.close(); } } @Test public void test06() { Session session = null; try { session = HibernateUtil.openSession(); /** * 使用uniqueResult可以返回唯一的一个值 */ Long stus = (Long)session.createQuery("select count(*) from Student where name like :name and sex=:sex") .setParameter("name", "%刘%") .setParameter("sex", "男") .uniqueResult(); System.out.println(stus); } catch (Exception e) { e.printStackTrace(); } finally { HibernateUtil.close(session); } } @Test public void test07() { Session session = null; try { session = HibernateUtil.openSession(); /** * 使用uniqueResult可以返回唯一的一个值 */ Student stu = (Student)session.createQuery("select stu from Student stu where id=:id") .setParameter("id", 1) .uniqueResult(); System.out.println(stu.getName()); } catch (Exception e) { e.printStackTrace(); } finally { HibernateUtil.close(session); } } @Test public void test08() { Session session = null; try { session = HibernateUtil.openSession(); /** * 基于投影的查询,通过在列表中存储一个对象的数组 */ List<Object[]> stus = session.createQuery("select stu.sex,count(*) from Student stu group by stu.sex") .list(); for(Object[] obj:stus) { System.out.println(obj[0]+":"+obj[1]); } } catch (Exception e) { e.printStackTrace(); } finally { HibernateUtil.close(session); } } @Test public void test09() { Session session = null; try { session = HibernateUtil.openSession(); /** * 如果对象中相应的导航对象,可以直接导航完成查询 */ List<Student> stus = session.createQuery("select stu from Student stu where stu.classroom.name=? and stu.name like ?") .setParameter(0, "计算机教育班").setParameter(1, "%张%") .list(); for(Student stu:stus) { System.out.println(stu.getName()); } } catch (Exception e) { e.printStackTrace(); } finally { HibernateUtil.close(session); } } @Test public void test10() { Session session = null; try { session = HibernateUtil.openSession(); /** * 可以使用in来设置基于列表的查询,此处的查询需要使用别名进行查询 * 特别注意,使用in的查询必须在其他的查询之后 */ List<Student> stus = session.createQuery("select stu from Student stu where stu.name like ? and stu.classroom.id in (:clas)") .setParameter(0, "%张%").setParameterList("clas", new Integer[]{1,2}) .list(); for(Student stu:stus) { System.out.println(stu.getName()); } } catch (Exception e) { e.printStackTrace(); } finally { HibernateUtil.close(session); } } @Test public void test11() { Session session = null; try { session = HibernateUtil.openSession(); /** * 使用setFirstResult和setMaxResult可以完成分页的offset和pageSize的设置 */ List<Student> stus = session.createQuery("select stu from Student stu where stu.classroom.id in (:clas)") .setParameterList("clas", new Integer[]{1,2}) .setFirstResult(0).setMaxResults(15) .list(); for(Student stu:stus) { System.out.println(stu.getName()); } } catch (Exception e) { e.printStackTrace(); } finally { HibernateUtil.close(session); } } @Test public void test12() { Session session = null; try { session = HibernateUtil.openSession(); /** * 可以通过is null来查询为空的对象,和sql一样不能使用=来查询null的对象 */ List<Student> stus = session.createQuery("select stu from Student stu where stu.classroom is null") .setFirstResult(0).setMaxResults(15) .list(); for(Student stu:stus) { System.out.println(stu.getName()); } } catch (Exception e) { e.printStackTrace(); } finally { HibernateUtil.close(session); } } @Test public void test13() { Session session = null; try { session = HibernateUtil.openSession(); /** * 使用对象的导航可以完成连接,但是是基于Cross JOIN(全连接),效率不高,可以直接使用JOIN来完成连接 */ List<Student> stus = session .createQuery("select stu from Student stu left join stu.classroom cla where cla.id=2") .setFirstResult(0).setMaxResults(15) .list(); for(Student stu:stus) { System.out.println(stu.getName()); } } catch (Exception e) { e.printStackTrace(); } finally { HibernateUtil.close(session); } } @Test public void test14() { Session session = null; try { session = HibernateUtil.openSession(); //基于班级进行统计每个班男女生人数 List<Object[]> stus=session.createQuery("select c.name,s.sex,count(s.id) from Student s right join s.classroom c group by c.id,s.sex").list(); for(Object[] stu:stus){ System.out.println(stu[0]+" "+stu[1] +stu[2]); } } catch (Exception e) { e.printStackTrace(); } finally { HibernateUtil.close(session); } } @Test public void test15() { Session session = null; try { session = HibernateUtil.openSession(); /** * 直接可以使用new XXObject完成查询,注意,一定要加上Object的完整包名 * 这里使用的new XX,必须在对象中加入相应的构造函数 */ List<StudentDto> stus = session .createQuery("select new org.th.dto.StudentDto" + "(stu.id as sid,stu.name as sname,stu.sex as sex,cla.name as cname,spe.name as spename) " + "from Student stu left join stu.classroom cla left join cla.special spe") .list(); for(StudentDto stu:stus) { System.out.println(stu.getSid()+","+stu.getSname()+","+stu.getSex()+","+stu.getCname()+","+stu.getSpename()); } } catch (Exception e) { e.printStackTrace(); } finally { HibernateUtil.close(session); } }
StudentDto.java(数据传输对象)
/** * DTO对象没有存储的意义,仅仅是用来进行数据的传输的 * @author Administrator * */ public class StudentDto { private int sid; private String sname; private String sex; private String cname; private String spename; }