---恢复内容开始---
Hibernate的检索方式分为五种:
1.对象导航检索.根据已加载的对象,调用方法.导航到其他对象
Customer c=session.get(Customer.class,2); c.getOrders().size()
2.OID检索:按照对象的OID检索对象:通过get/load方法查询指定的对象,要通过OID检索,也就是id
get和load的区别:
1.get是立即加载,load方法是延迟加载策略,当我们获取加载出来的对象的相关属性时,才发送sql,查询返回的是一个代理对象.
2.get方法查询的时候,如果这个对象不存在,会返回null;
load方法查询时,如果对象不存在,会报错.
3.HQL(Hibernate Query Language),面向对象的查询语言
Select/update/delete…… from …… where …… group by …… having …… order by …… asc/desc
基本步骤:1.得到session 2.编写hql语句 3.通过session.createQuery(hql) 创建query对象 4.为Query对象设置条件参数 5.执行list()查询所有,他返回的是list集合.uniqueResult()返回的是唯一查询结果.
1 //基本检索 2 @Test 3 public void test5(){ 4 Session session = HibernateUtils.openSession(); 5 session.beginTransaction(); 6 //1.编写hql语句 7 String hql = "from Customer" ; 8 //2.获得query,获得list集合 9 Query query = session.createQuery(hql); 10 List<Customer> list = query.list(); 11 12 System.out.println(list.get(0)); 13 14 session.getTransaction().commit(); 15 session.close(); 16 } 17 //排序检索 18 @Test 19 public void test6(){ 20 Session session = HibernateUtils.openSession(); 21 session.beginTransaction(); 22 String hql ="from Order Order by money asc"; 23 Query query = session.createQuery(hql); 24 List<Order> list = query.list(); 25 System.out.println(list); 26 session.getTransaction().commit(); 27 session.close(); 28 } 29 //条件检索 30 @Test 31 public void test7(){ 32 Session session = HibernateUtils.openSession(); 33 session.beginTransaction(); 34 String hql ="from Order where money > ? "; 35 Query query = session.createQuery(hql).setParameter(0, 2000d); 36 List<Order> list = query.list(); 37 System.out.println(list); 38 session.getTransaction().commit(); 39 session.close(); 40 } 41 //分页检索 42 @Test 43 public void test8(){ 44 Session session = HibernateUtils.openSession(); 45 session.beginTransaction(); 46 String hql = "from Order"; 47 Query query = session.createQuery(hql); 48 //每页显示6,得到第二页的数据 49 query.setFirstResult((2-1)*6); 50 query.setMaxResults(6); 51 List<Order> list = query.list(); 52 System.out.println(list); 53 session.getTransaction().commit(); 54 session.close(); 55 } 56 //分组统计检索 57 @Test 58 public void test9(){ 59 Session session = HibernateUtils.openSession(); 60 session.beginTransaction(); 61 //分组统计每一个人的订单总价 62 String hql = "select c.name,sum(money) from Order group by c "; 63 Query query = session.createQuery(hql); 64 65 List<Object[]> list = query.list(); 66 for (Object[] objects : list) { 67 for ( Object o: objects) { 68 System.out.println(o); 69 } 70 } 71 //System.out.println(list); 72 session.getTransaction().commit(); 73 session.close(); 74 } 75 //投影检索,必须在Customer类创建无参(创建有参就不会提供默认的无参)和有参构造 76 @Test 77 public void test10(){ 78 Session session = HibernateUtils.openSession(); 79 session.beginTransaction(); 80 String hql = "select new Customer(id,name) from Customer "; 81 Query query = session.createQuery(hql); 82 List<Customer> list = query.list(); 83 System.out.println(list); 84 session.getTransaction().commit(); 85 session.close(); 86 } 87 @Test 88 //命名检索,在Customer类定义@NamedQuery(name="myHql",query="from Customer") 89 //把创建Query封装到实体类的namedQuery 90 public void test11(){ 91 Session session = HibernateUtils.openSession(); 92 session.beginTransaction(); 93 Query query = session.getNamedQuery("myHql"); 94 List<Customer> list = query.list(); 95 System.out.println(list); 96 session.getTransaction().commit(); 97 session.close(); 98 }
4.QBC检索方式,使用QBC(Query by Criteria)API来检索对象,这种API封装了基于字符串形式的查询语句,提供了更加面向对象的查询接口.步骤:
1.通过session得到一个Criteria对象: session.getCriteria(); 2.设置条件:add方法设置条件,Retrictions获取Criteria实例(大于等于等)
3.调用list(),获取集合数据.
@Test public void test001() { // 基本检索----查询所有Customer Session session = HibernateUtils.openSession(); session.beginTransaction(); Criteria criteria = session.createCriteria(Customer.class); List l = criteria.list(); System.err.println(l); session.getTransaction().commit(); session.close(); } @Test public void test002() { // 排序检索----查询订单信息,根据订单的价格进行排序 Session session = HibernateUtils.openSession(); session.beginTransaction(); Criteria criteria = session.createCriteria(Order.class); // 这个路径的Order是排序,是为了区别domain中的Order criteria.addOrder(org.hibernate.criterion.Order.asc("money")); List l = criteria.list(); System.err.println(l); session.getTransaction().commit(); session.close(); } @Test public void test003() { // 条件检索----查询客户名称为张某:张_ Session session = HibernateUtils.openSession(); session.beginTransaction(); Criteria criteria = session.createCriteria(Customer.class); SimpleExpression like = Restrictions.like("name", "张_"); criteria.add(like); Customer c = (Customer) criteria.uniqueResult(); // System.err.println(l); // 查询订单价格在2050以上,并且客户时张某 Criteria cri = session.createCriteria(Order.class); SimpleExpression gt = Restrictions.gt("money", 2050d); SimpleExpression eq = Restrictions.eq("c", c); LogicalExpression and = Restrictions.and(gt, eq); cri.add(and); List list = cri.list(); System.out.println(list); session.getTransaction().commit(); session.close(); } @Test public void test004() { // 分页检索---- Session session = HibernateUtils.openSession(); session.beginTransaction(); Criteria criteria = session.createCriteria(Order.class); criteria.setFirstResult((2 - 1) * 6); criteria.setMaxResults(6); List<Order> list = criteria.list(); System.out.println(list); session.getTransaction().commit(); session.close(); } @Test public void test005() { // 统计分组检索----统计订单总数 Session session = HibernateUtils.openSession(); session.beginTransaction(); Criteria criteria = session.createCriteria(Order.class); /* * List list = criteria.setProjection(Projections.rowCount()).list(); * System.out.println(list); */ // 2.统计订单的总价格---分组统计根据客户 // List list = // criteria.setProjection(Projections.sum("money")).list();--总价格 criteria.setProjection(Projections.projectionList() .add(Projections.sum("money")) .add(Projections.groupProperty("c"))); List<Object[]> list = criteria.list(); for (Object[] objects : list) { for (Object obj : objects) { System.out.println(obj); } } session.getTransaction().commit(); session.close(); } @Test //离线条件检索 public void test006() { //1.得到一个Detached DetachedCriteria dc = DetachedCriteria.forClass(Customer.class); dc.add(Restrictions.like("name","张_")); //2.生成Criteria执行操作 Session session = HibernateUtils.openSession(); session.beginTransaction(); Criteria criteria = dc.getExecutableCriteria(session); List<Customer> list = criteria.list(); System.out.println(list); session.getTransaction().commit(); session.close(); }
5.使用本地数据库的sql语言
//执行本地sql @Test public void test1(){ Session session = HibernateUtils.openSession(); session.beginTransaction(); SQLQuery sqlQuery = session.createSQLQuery("select*from t_customer"); sqlQuery.addEntity(Customer.class);//将查询结果绑定到指定对象 //sqlQuery.setParameter(position, val);设置参数 List list = sqlQuery.list(); System.out.println(list); session.getTransaction(); session.close(); } //测试执行本地sql的命名查询 @Test public void test2(){ Session session = HibernateUtils.openSession(); session.beginTransaction(); Query query = session.getNamedQuery("findCustomer"); List list = query.list(); System.out.println(list); session.getTransaction(); session.close(); //由于hibernate执行sql语句后,不知道结果该如何封装,所以用下面的注解 //@SqlResultSetMapping(name="mapping",entities={@EntityResult(entityClass=Customer.class,fields={@FieldResult(name="id",column="id"),@FieldResult(name="name",column="name")})}) //@NamedNativeQuery(name="findCustomer",query="select*from t_customer",resultSetMapping="mapping") }
---恢复内容结束---