Hibernate相关的查询 --Hibernate框架基础

Posted Thanlon

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Hibernate相关的查询 --Hibernate框架基础相关的知识,希望对你有一定的参考价值。

 接着上一篇博文:Hibernate第一个程序(最基础的增删改查) --Hibernate本例是对Hibernate查询的扩展,使用HQL语句查询

/**
* HQL添加预先需要保存的测试数据
*/
@Test
public void test_01() {
  Session session = HbnUtils.getSession();
  try {
    session.beginTransaction();
    Student student1 = new Student("貂蝉", 24, 99);
    Student student2 = new Student("王昭君", 22, 93);
    Student student3 = new Student("杨玉环", 28, 94);
    Student student4 = new Student("李师师", 26, 95);
    Student student5 = new Student("苏小小", 20, 96);
    Student student6 = new Student("李师师", 25, 96);
    Student student7 = new Student("唐伯虎", 21, 91);
    Student student8 = new Student("祝枝山", 21, 92);
    session.save(student1);
    session.save(student2);
    session.save(student3);
    session.save(student4);
    session.save(student5);
    session.save(student6);
    session.save(student7);
    session.save(student8);
    session.getTransaction().commit();
  } catch (Exception e) {
// TODO: handle exception
    session.getTransaction().rollback();
    e.printStackTrace();
  }
}

/**
* 使用HQL语句查询信息并且通过升序排列
*/
@Test
public void test_02() {
  Session session = HbnUtils.getSession();
  try {
    session.beginTransaction();
    String hql = "from Student order by tscore asc";
    List<Student> list = session.createQuery(hql).list();
    for (Student student : list) {
    System.out.println(student);
  }
  session.getTransaction().commit();
  } catch (Exception e) {
    // TODO: handle exception
    e.printStackTrace();
    session.getTransaction().rollback();
  }
}

/**
* 使用SQL语句查询学生信息并且通过升序排列(对比HQL可忽略)
*/
@Test
public void testSQL_01() {
  Session session = HbnUtils.getSession();
  try {
    session.beginTransaction();
    String sql = "select *from t_student order by tscore asc";
    List<Student> list = session.createSQLQuery(sql).addEntity(Student.class).list();
    for (Student student : list) {
    System.out.println(student);
  }
  session.getTransaction().commit();
  } catch (Exception e) {
  // TODO: handle exception
    e.printStackTrace();
    session.getTransaction().rollback();
  }
}

/**
* HQL动态参数绑定查询(占位符)
*/
@Test
public void test_04() {
  Session session = HbnUtils.getSession();
  try {
    session.beginTransaction();
    String hql = "from Student where age>? and score<?";
    List<Student> list = session.createQuery(hql).setInteger(0, 25).setDouble(1, 97).list();
    for (Student student : list) {
    System.out.println(student);
  }
    session.getTransaction().commit();
  } catch (Exception e) {
    // TODO: handle exception
    e.printStackTrace();
    session.getTransaction().rollback();
  }
}

/**
* HQL动态参数绑定查询別名的方式
*/
@Test
public void testQuery_HQL_04() {
Session session = HbnUtils.getSession();
  try {
    session.beginTransaction();
    String hql = "from Student where age>:myage and score<:myscore";
    List<Student> list = session.createQuery(hql)
    .setParameter("myage", 25).setParameter("myscore", 97.0)// 必須是97.0
    .list();
    for (Student student : list) {
    System.out.println(student);
  }
  session.getTransaction().commit();
  } catch (Exception e) {
    // TODO: handle exception
    e.printStackTrace();
    session.getTransaction().rollback();
  }
}

 

/**
* HQL分页查询
*/
@Test
public void test_05() {
  Session session = HbnUtils.getSession();
  try {
    session.beginTransaction();
    String hql = "from Student";
    List<Student> list =session.createQuery(hql)
    .setFirstResult(1)
    .setMaxResults(3)
    .list();
    for (Student student : list) {
     System.out.println(student);
    }
  session.getTransaction().commit();
  } catch (Exception e) {
    // TODO: handle exception
    e.printStackTrace();
    session.getTransaction().rollback();
  }
}

 

/**
* SQL分页查询(对比,忽略即可)
*/
@Test
public void testSql_06() {
  Session session = HbnUtils.getSession();
  try {
    session.beginTransaction();
    String sql = "select *from t_student limit ?,?";
    List<Student> list =session.createSQLQuery(sql)
    .addEntity(Student.class)
    .setInteger(0, 1)//从第5条数据开始
    .setInteger(1, 3)
    .list();
    for (Student student : list) {
      System.out.println(student);
    }
  session.getTransaction().commit();
  } catch (Exception e) {
    // TODO: handle exception
    e.printStackTrace();
    session.getTransaction().rollback();
  }
}

/**
* HQL模糊查询
*/
@Test
public void testQuery_SQL_07() {
  Session session = HbnUtils.getSession();
  try {
    session.beginTransaction();
    String hql = "from Student where tname like :myname";
    List<Student> list =session.createQuery(hql)
    .setString("myname", "%王%")
    .list();
    for (Student student : list) {
      System.out.println(student);
    }
    session.getTransaction().commit();
  } catch (Exception e) {
    // TODO: handle exception
    e.printStackTrace();
    session.getTransaction().rollback();
  }
}

/**
* HQL唯一性查询
*/
@Test
public void testQuery_SQL_08() {
  Session session = HbnUtils.getSession();
  try {
    session.beginTransaction();
    String hql = "from Student where tid = :myid";
    Student student =(Student) session.createQuery(hql)
    .setInteger("myid", 1)
    .uniqueResult();
    System.out.println(student);
    session.getTransaction().commit();
  } catch (Exception e) {
    // TODO: handle exception
    e.printStackTrace();
    session.getTransaction().rollback();
  }
}

 

 

/**
* 聚合函数查询
*/
@Test
public void testQuery_HQL_06() {
  Session session = HbnUtils.getSession();
  try {
    session.beginTransaction();
    String hql = "SELECT COUNT(*) from Student";
    Long count = (Long) session.createQuery(hql).uniqueResult();
    System.out.println(count);// 输出结果有多少条数据
    session.getTransaction().commit();
  } catch (Exception e) {
    // TODO: handle exception
    e.printStackTrace();
    session.getTransaction().rollback();
  }
}

/**
* 投影查询
*/
@Test
public void testQuery_HQL04() {
  Session session = HbnUtils.getSession();
  try {
    session.beginTransaction();
    String hql = "select new Student(name,age) from Student";// 将查询到的字段通过构造方法封装到对象,然后保存在list中
    List<Student> list = session.createQuery(hql).list();
    for (Student student : list) {
      System.out.println(student);// 只查询到name、age信息,其他则为默认
    }
    session.getTransaction().commit();
  } catch (Exception e) {
    // TODO: handle exception
    e.printStackTrace();
    session.getTransaction().rollback();
  }
}

/**
* HQL分组查询 :查每个年龄段的第一个人
*/
@Test
public void testQuery_HQL05() {
  Session session = HbnUtils.getSession();
  try {
    session.beginTransaction();
    String hql = "from Student group by age";
    List<Student> list = session.createQuery(hql).list();
    for (Student student : list) {
      System.out.println(student);
    }
    session.getTransaction().commit();
  } catch (Exception e) {
    // TODO: handle exception
    e.printStackTrace();
    session.getTransaction().rollback();
  }
}

 

/**
* HQL分组查询:查询有几个年龄段
*/
@Test
public void testQuery_HQL06() {
  Session session = HbnUtils.getSession();
  try {
    session.beginTransaction();
    String hql = "select age from Student group by age";
    List<Integer> list = session.createQuery(hql).list();
    System.out.println(list);
    session.getTransaction().commit();
  } catch (Exception e) {
    // TODO: handle exception
    e.printStackTrace();
    session.getTransaction().rollback();
  }
} 

/**
* HQL分组查询:查询多于1人的年龄段
*/
@Test
public void testQuery_HQL07() {
  Session session = HbnUtils.getSession();
    try {
    session.beginTransaction();
    String hql = "select age from Student group by age having count(age) > 1";
    List<Integer> list = session.createQuery(hql).list();
    System.out.println(list);
    session.getTransaction().commit();
    } catch (Exception e) {
      // TODO: handle exception
      e.printStackTrace();
      session.getTransaction().rollback();
    }
}

 

附:个人网站www.nxl123.cn(后台采用Python Flask框架搭建,2019年1月1日将升级完成并正式启用。哎,本人是学生狗呢!网站做的不好希望大家多多提意见或建议吧!?别骂我就好!……以后SEO什么的还得多向大家学习……)

以上是关于Hibernate相关的查询 --Hibernate框架基础的主要内容,如果未能解决你的问题,请参考以下文章

请说说你对Hibernat的理解?JDBC和Hibernate各有什么优势和劣势?

Hibernate HQL 查询:如何将集合设置为查询的命名参数?

Spring & Hibernate - 使本机查询在与@Transactional 相同的事务中运行

Hibernate与Mybatis对比

SSH整合

Hibernate二级缓存 --Hibernate框架