Hibernate07_Hibernate Query Language
例1:
String hql = "from People";
Query query = session.createQuery(hql);
List<People> list = query.list();
for(People people : list) {
System.out.println(people);
}
People(id=1, name=张三, money=1000.0)
People(id=2, name=李四, money=2000.0)
People(id=3, name=王五, money=3000.0)
People(id=4, name=小明, money=4000.0)
People(id=5, name=小红, money=3000.0)
People(id=6, name=小李, money=2000.0)
People(id=7, name=BLU, money=3500.0)
People(id=8, name=lily, money=4500.0)
People(id=9, name=mary, money=5500.0)
例2(分页查询):
分页查询可以通过调用query的方法来完成:
setFirstResult() 设置起始下标(从0开始)
setMaxResult() 设置截取长度
String hql = "from People";
Query query = session.createQuery(hql);
query.setFirstResult(1);
query.setMaxResults(3);
List<People> list = query.list();
for(People people : list) {
System.out.println(people);
}
People(id=2, name=李四, money=2000.0)
People(id=3, name=王五, money=3000.0)
People(id=4, name=小明, money=4000.0)
例3(where条件查询):
String hql = "from People where id = 7";
Query query = session.createQuery(hql);
People people = (People) query.list().get(0);
System.out.println(people);
People(id=7, name=BLU, money=3500.0)
注:通过以下两种方式都可以查出一条结果的值,但是用 list() 方法查出来的值不能为空,否则会报数组越界异常:java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
People people = (People) query.list().get(0);
People people = (People) query.uniqueResult();
例4(模糊查询):
String hql = "from People where name like '%小%'";
Query query = session.createQuery(hql);
List<People> list = query.list();
for(People people : list) {
System.out.println(people);
}
People(id=4, name=小明, money=4000.0)
People(id=5, name=小红, money=3000.0)
People(id=6, name=小李, money=2000.0)
例5(order by)asc/desc:
String hql = "from People order by id desc";
Query query = session.createQuery(hql);
List<People> list = query.list();
for(People people : list) {
System.out.println(people);
}
People(id=9, name=mary, money=5500.0)
People(id=8, name=lily, money=4500.0)
People(id=7, name=BLU, money=3500.0)
People(id=6, name=小李, money=2000.0)
People(id=5, name=小红, money=3000.0)
People(id=4, name=小明, money=4000.0)
People(id=3, name=王五, money=3000.0)
People(id=2, name=李四, money=2000.0)
People(id=1, name=张三, money=1000.0)
例6(查询实体属性):
String hql = "select name from People where id = 7";
Query query = session.createQuery(hql);
String name = (String) query.uniqueResult();
System.out.println(name);
BLU
例7(占位符):
String hql = "from People where name = :name";
Query query = session.createQuery(hql);
query.setString("name", "BLU");
People people = (People) query.uniqueResult();
System.out.println(people);
People(id=7, name=BLU, money=3500.0)
例8(级联查询):
String hql1 = "from Customer where name = :name";
Query query1 = session.createQuery(hql1);
query1.setString("name", "张三");
Customer customer = (Customer) query1.uniqueResult();
String hql2 = "from Orders where customer = :customer";
Query query2 = session.createQuery(hql2);
query2.setEntity("customer", customer);
List<Orders> list = query2.list();
for(Orders orders : list) {
System.out.println(orders);
}
Orders [id=1, name=订单1]
Orders [id=2, name=订单2]
Orders [id=4, name=订单3]
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blucoding.blog.csdn.net/article/details/107845302
内容来源于网络,如有侵权,请联系作者删除!