首页 > 代码库 > Hibernate的检索方式
Hibernate的检索方式
Hibernate的检索方式
检索方式(查询的方式)
导航对象图检索方式: 根据已经加载的对象导航到其他对象
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”);
HQL
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);
9.聚集函数
Long count = (Long) session.createQuery("select count(*) from Order").uniqueResult();
System.out.println(count);
10.命名查询(在配置文件中配置查询语句)
List<Customer> list = session.getNamedQuery("findAll").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多表的查询
交叉连接,内连接,隐式内连接,迫切内连接,左外连接,迫切左外连接,右外连接
隐式内连接(了解)
createQuery("from Order o where o.customer.name like ‘T%‘");
等价于:from Order o join o.customer c where c.name like ‘T%‘
HQL的内连接和迫切内连接区别(其他类似):
内连接查询 :将数据封装一个List<Object[]>中.
迫切内连接 :将数据封装一个List<Customer>中.但是迫切内连接,得到会有重复记录 ,需要使用distinct排重.
// 内连接查询: Query query = session.createQuery("from Customer c inner join c.orders"); List<Object[]> list = query.list(); for (Object[] objects : list) { Customer c = (Customer)objects[0]; Order o = (Order)objects[1]; System.out.println(c.getCid()+" "+c.getCname()+" "+c.getAge()); System.out.println(o.getOid()+" "+o.getAddr()); } // 迫切内连接:使用一个关键字 fetch(HQL) Query query = session.createQuery("select distinct c from Customer c inner join fetch c.orders"); List<Customer> list = query.list(); for (Customer customer : list) { System.out.println(customer); }
QBC
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);
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); }
离线查询
@Test /* * 离线条件查询 */ public void demo(){ // web层的封装 DetachedCriteria criteria = DetachedCriteria.forClass(Customer.class); criteria.add(Restrictions.eq("cname", "小明")); criteria.add(Restrictions.eq("cid", 2)); // 传递到DAO层 Session session = HibernateUtils.openSession(); Transaction tx = session.beginTransaction(); Criteria c1 = criteria.getExecutableCriteria(session); List<Customer> list = c1.list(); System.out.println(list); tx.commit(); session.close(); }
Hibernate的检索方式