1 / 27

第七章

第七章. Hibernate 查询. 回顾. 写出下面两个类的 Hibernate 配置文件,注意关联的配置。. // 订单明细实体类 public class OrderLine{ private long lineId ; private Order order; private String productName; private long count; //setters & getters ... }. // 订单实体类 public class Order{

hateya
Download Presentation

第七章

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第七章 Hibernate查询

  2. 回顾 • 写出下面两个类的Hibernate配置文件,注意关联的配置。 //订单明细实体类 public class OrderLine{ private long lineId ; private Order order; private String productName; private long count; //setters & getters ... } //订单实体类 public class Order{ private long orderId ; private String customerName; private java.sql.Date orderDate; //setters & getters ... }

  3. 预习检查 • Hibernate支持哪两种查询? • HQL是什么?

  4. 本章任务 • 使用Hibernate实现,房屋查询DAO • 对title模糊查询 • 对街道精确查询 • 对租金使用一个范围查询 • 查询特定联系人都在哪些街道发布了信息 • 提供分页查询方法

  5. 本章目标 • 掌握HQL查询 • 掌握Criteria查询

  6. 为什么使用HQL Hibernate中如何实现查询 • 如何使用Hibernate查询所有房屋信息? SQL语句: select * from tbl_fwxx HQL Hibernate Query Lanuage Hibernate查询语言 ?

  7. 如何使用HQL • 使用HQL的四步 1、得到Session 1 可以没有select子句 2 3 4 2、编写HQL语句 from TblFwxx from关键字,类似于SQL语句 实体类名,而不是数据库表名 3、创建Query 4、执行查询

  8. 如何使用HQL • 类似SQL SELECT语句,可以使用表的别名 HQL是面向对象的查询语言。 select fw表示查询fw对象 select fw from TblFwxx as fw 表的别名,as可以省略 执行结果与from TblFwxx相同

  9. 常见错误 • 下面代码中有什么错误,怎么更正? tbl_fwxx是表名。HQL是对象查询语言,应该是类名:TblFwxx

  10. 常见错误 • 下面代码中有什么错误,怎么更正? 类名是区分大小写的。应该是TblFwxx。 但SELECT、FROM等关键字是不区分大小写的。

  11. 属性查询 • select fw from TblFwxx fw将查询整个对象信息,我们只想查询date和title怎么做? ? select fw.title, fw.date from TblFwxx fw 查询结果仍保存在list中 每条数据封装成一个Object数组

  12. 小结 • 实现QxDAO接口: public interface QxDAO{ public List listAllQx(); }

  13. 参数查询 • 如何实现:根据title模糊查询房屋信息? select fw from TblFwxx fw where fw.title like '%健翔桥%' ? 支持'%'通配符 支持like关键字 这样拼装HQL字符串容易带来安全隐患 where子句

  14. 参数查询 • HQL提供类似preparedStatement的参数查询 以'?'为占位符 Query提供setLong,setDouble,setDate等方法用于设置不同类型的参数值 设置参数的值 注意: 1. 必须保证:query设置参数的数目 == hql语句中占位符的数目 2. 占位符下标从 0 开始。

  15. 参数查询 支持: >、< 、 = 、 >= 、 <= 、<> 和is null; and、or、not和括号; in和between • 查询租金在zj1到zj2范围内的租房信息:public List searchByZj(int zj1, int zj2)。 public List searchByZj(int zj1, int zj2){ Session session = this.getSession(); String hql = "from TblFwxx fw " +"where fw.zj >= ? and fw.zj <= ?"; Query query = session.createQuery(hql); query.setInteger(1, zj1); query.setInteger(2,zj2); return query.list(); } 当参数数目增多时,代码可读性下降;将下标顺序硬编码,参数顺序有调整则代码也要调整 使用between关键字上面的hql语句怎么写? 当参数数目增多的时候,上面的代码会带来什么问题?

  16. 参数查询:命名参数 • 查询租金在zj1到zj2范围内的租房信息。 使用“:参数名”的格式定义命名参数 设定命名参数的值

  17. 小结 • 根据房屋类型查询:public List searchByJd(int jdId); 提示: ...where fw.jd.jdid = ? 或者 ...where fw.jd.jdid = :jdid

  18. 关联查询 • 查询:联系人为“伊先生”的房屋信息都分布在哪些街道。 从两类对象中检索数据 设置关联条件。注意:jd是对象。 生成的SQL语句

  19. 小结 • 写出HQL语句: 查询“亚运村”街道的房屋信息中,涉及的房屋类型。 提示: 亚运村街道的房屋信息: fw.jd.jdid=39

  20. 分页查询 • 实现分页查询方法:public List search(int pageNo, int pageSize) 。 使用order by对结果排序 分页代码

  21. 统计函数 • 在实现分页功能时,我们需要知道总记录数以便计算总页数。 使用count()函数 当结果只有一条记录时,可以使用uniqueResult()得到结果 可以使用的函数还有:min()、max()、avg()

  22. 小结 • 写出程序代码: 1、根据租金排序,查询从高到低前10条记录。 2、查询朝阳区房屋租金的平均值。 String hql = "from TblFwxx fw order by fw.zj desc"; ... query.setFirstResult(0); query.setMaxResult(10); String hql = "select avg(fw.zj) from TblFwxx fw " + +"where fw.jd. tblQx.qx='朝阳区'"; ... double avgZj = (Double)query.uniqueResult();

  23. 对象查询 • 使用一个查询方法,同时支持三项功能: • 对title模糊查询 • 对房屋类型精确查询 • 对租金使用一个范围查询 public List search(TblFwxx condition){ String hql = "select fw from TblFwxx fw " hql += "where 1=1 "; if (condition.getTitle()!=null){ hql += "and fw.title like '%" + condition.getTitle() + "%' "; } ... } 代码啰嗦,不易维护 不方便使用参数查询,安全性和执行效率不好 使用Criteria查询代码更简洁

  24. 使用Criteria查询 创建Criteria对象 • 以对象的方式构建查询 模糊查询 大于等于 和 小于等于 支持类似EL表达式的属性浏览语法 支持in和between 数组类型 可以增加多个排序规则

  25. 使用Criteria查询 • 测试程序和运行效果 生成的SQL语句: Hibernate: select this_.fwid as fwid1_0_, this_.uid as uid1_0_, this_.jdid as jdid1_0_, this_.lxid as lxid1_0_, this_.shi as shi1_0_, this_.ting as ting1_0_, this_.fwxx as fwxx1_0_, this_.zj as zj1_0_, this_.title as title1_0_, this_.date as date1_0_, this_.telephone as telephone1_0_, this_.lxr as lxr1_0_ from TBL_FWXX this_ where this_.title like ? and this_.zj>=? and this_.zj<=? and this_.lxid in (?, ?) order by this_.fwid asc

  26. 总结 • HQL的全称是? • 和SQL相比,HQL有哪些特点? • HQL语句为:select jd.jdid,jd.jd from TblJd jd。怎样获得并显示查询结果? • 使用'?'做占位符的参数查询,怎样设置参数的值? • 命名参数查询的语法是? • 怎样创建Criteria查询对象?

More Related