本文主要是介绍hibernate教程--检索方式(hql,sql,QBC),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.1Hibernate的检索方式
1.1.1Hibernate的检索方式:
检索方式:查询的方式:
导航对象图检索方式: 根据已经加载的对象导航到其他对象
* Customer customer = (Customer)session.get(Customer.class,1);
* customer.getOrders();// 获得到客户的订单
OID 检索方式: 按照对象的 OID 来检索对象
* get()/load();方法进行检索.
HQL 检索方式: 使用面向对象的 HQL 查询语言
* Query query = session.createQuery(“HQL”);
QBC 检索方式: 使用 QBC(Query By Criteria) API 来检索对象. 这种 API 封装了基于字符串形式的查询语句, 提供了更加面向对象的查询接口.
* Criteria criteria = session.createCriteria(Customer.class);
本地 SQL 检索方式: 使用本地数据库的 SQL 查询语句
* SQLQuery query = session.createSQLQuery(“SQL”);
1.1.2HQL:
HQL:Hibernate Query Language:
* 特点:
* 面向对象的查询:
* 支持方法链编程:
* 使用:
1.查询所有记录:
List<Customer> list = session.createQuery("from Customer").list();for (Customer customer : list) {System.out.println(customer);}
2.查询使用别名:
// 使用别名// 别名as可以省略/* List<Customer> list =session.createQuery("from Customer c").list();System.out.println(list);*/// 使用别名:带参数/*List<Customer> list = session.createQuery("from Customer as c where c.cname = ?").setString(0, "小沈").list();System.out.println(list);*/// 不支持 select * from Customer写法.可以写成 select 别名 from Customer as 别名;List<Customer> list = session.createQuery("select c from Customer c").list();System.out.println(list);
3.排序:
List<Customer> list = session.createQuery("from Customer c order by c.id desc").list();for (Customer customer : list) {System.out.println(customer);}
4.分页查询:
Query query = session.createQuery("from Order");query.setFirstResult(20);query.setMaxResults(10);List<Order> list = query.list();for (Order order : list) {System.out.println(order);}
5.单个对象查询:
Customer customer = (Customer) session.createQuery("from Customer where cname = ?").setString(0, "小明").uniqueResult();System.out.println(customer);
6.参数绑定:
// 1.使用?号方式绑定/*Query query = session.createQuery("from Customer where cname = ?");query.setString(0, "小沈");List<Customer> list = query.list();System.out.println(list);*//*Query query = session.createQuery("from Customer where cname = ? and cid =?");query.setString(0, "小沈");query.setInteger(1,3);List<Customer> list = query.list();System.out.println(list);*/// 2.使用名称的方式绑定Query query = session.createQuery("from Customer where cname=:name and cid=:id");query.setString("name", "小沈");query.setInteger("id", 3);List<Customer> list = query.list();System.out.println(list);// 3.绑定实体List<Order> list = session.createQuery("from Order o where o.customer = ?").setEntity(0, customer).list();for (Order order : list) {System.out.println(order);}
7.投影操作:
// 查询客户的名称:/** List<Object> list = session.createQuery(* "select c.cname from Customer c").list(); System.out.println(list);*//** List<Object[]> list = session.createQuery(* "select c.cid,c.cname from Customer c").list(); for (Object[] objects* : list) { System.out.println(Arrays.toString(objects)); }*/List<Customer> list = session.createQuery("select new Customer(cname) from Customer").list();System.out.println(list);
8.模糊查询:
Query query = session.createQuery("from Customer where cname like ?");query.setParameter(0, "小%");List<Customer> list = query.list();System.out.println(list);
SQL多表查询:
* 连接:
* 交叉连接:
* select * from A,B;
* 内连接:查询的是两个表的交集!
* select * from A inner join B on A.字段 = B.字段;
* 隐式内连接:
* select * from A,B where A.字段 = B.字段;
* 外连接:
* 左外连接:
* select * from A left outer join B on A.字段 = B.字段;
* 右外连接:
* select * from A right outer join B on A.字段 = B.字段;
HQL多表的查询:
* 连接:
* 交叉连接:
* 内连接:
* 隐式内连接:
* 迫切内连接:
* 左外连接:
* 迫切左外连接:
* 右外连接:
* HQL的内连接和迫切内连接区别:
* 内连接查询 :将数据封装一个List<Object[]>中.
* 迫切内连接 :将数据封装一个List<Customer>中.但是迫切内连接,得到会有重复记录 ,需要使用distinct排重.
1.1.3QBC:
1.查询所有记录:
List<Customer> list = session.createCriteria(Customer.class).list();for (Customer customer : list) {System.out.println(customer);}
2.排序:
List<Customer> list = session.createCriteria(Customer.class).addOrder(org.hibernate.criterion.Order.desc("id")).list();for (Customer customer : list) {System.out.println(customer);}
3.分页:
Criteria criteria = session.createCriteria(Order.class);criteria.setFirstResult(10);criteria.setMaxResults(10);List<Order> list = criteria.list();for (Order order : list) {System.out.println(order);}
4.获取单个对象:
Customer customer = (Customer) session.createCriteria(Customer.class).add(Restrictions.eq("cname", "小明")).uniqueResult();System.out.println(customer);
5.带参数的查询:
/** List<Customer> list = session.createCriteria(Customer.class)* .add(Restrictions.eq("cname", "小明")).list();* System.out.println(list);*/List<Customer> list = session.createCriteria(Customer.class).add(Restrictions.eq("cname", "小明")).add(Restrictions.eq("cid", 2)).list();System.out.println(list);
6.模糊查询:
Criteria criteria = session.createCriteria(Customer.class);criteria.add(Restrictions.like("cname", "大%"));List<Customer> list = criteria.list();System.out.println(list);
1.1.4SQL:
1.SQL语句查询所有记录:
List<Object[]> list = session.createSQLQuery("select * from customer").list();for (Object[] objects : list) {System.out.println(Arrays.toString(objects));}List<Customer> list = session.createSQLQuery("select * from customer").addEntity(Customer.class).list();for (Customer customer : list) {System.out.println(customer);}
这篇关于hibernate教程--检索方式(hql,sql,QBC)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!