HQL(Hibernate Query Language)描写对象操作一种查询语言。Hibernate特有。
与SQL语法基本一致,不同的是HQL是面象对象的查询,查询的是对象和对象中的属性
HQL的关键字不区分大小写,但类名和属性名区分大小写
在Hibernate 提供的各种检索方式中, HQL是使用最广的一种检索方式. 它有如下功能:
在查询语句中设定各种查询条件
支持投影查询, 即仅检索出对象的部分属性
支持分页查询
支持连接查询
支持分组查询, 允许使用 HAVING 和 GROUP BY 关键字
提供内置聚集函数, 如 sum(), min() 和 max()
能够调用 用户定义的 SQL 函数或标准的 SQL 函数
支持子查询
支持动态绑定参数
测试类:
package com.my.bean;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;
import cn.itcast.util.SessionUtils;
public class TestHQL {
@Test
public void demo01(){
// 查询所有 HQL
Session session = SessionUtils.openSession();
Transaction transaction = session.beginTransaction();
Query query = session.createQuery("from Customer");
List<Customer> allCustomer = query.list();
for (Customer customer : allCustomer) {
System.out.println(customer.getName());
}
transaction.commit();
session.close();
}
@Test
public void demo02(){
// 查询 带有条件的,可以使用别名 AS
Session session = SessionUtils.openSession();
Transaction transaction = session.beginTransaction();
Query query = session.createQuery("FROM Customer AS c WHERE c.id = 5");
Customer customer = (Customer) query.uniqueResult();
System.out.println(customer.getName());
transaction.commit();
session.close();
}
@Test
public void demo03(){
// 排序 order by , asc 升序 | desc 降序
Session session = SessionUtils.openSession();
Transaction transaction = session.beginTransaction();
Query query = session.createQuery("from Customer c order by c.id desc");
List<Customer> all = query.list();
for (Customer customer : all) {
System.out.println(customer);
}
transaction.commit();
session.close();
}
@Test
public void demo04(){
// 查询部分内容
Session session = SessionUtils.openSession();
Transaction transaction = session.beginTransaction();
//将部分数据,封装到Customer 对象中
Query query = session.createQuery("select new Customer(c.name) from Customer c"); //必须提供相应的构造方式
List all = query.list();
System.out.println(all);
transaction.commit();
session.close();
}
@Test
public void demo05(){
// 分页,查询订单
Session session = SessionUtils.openSession();
Transaction transaction = session.beginTransaction();
Query query = session.createQuery("from Order");
// 添加分页条件
// 第一页
query.setFirstResult(0);
query.setMaxResults(7);
List<Order> all = query.list();
for (Order order : all) {
System.out.println(order);
}
transaction.commit();
session.close();
}
@Test
public void demo06(){
// 参数
Session session = SessionUtils.openSession();
Transaction transaction = session.beginTransaction();
//使用?
Query query = session.createQuery("from Customer c where c.id = ? ");
// 需要绑定参数,将实际参数替换?
query.setParameter(0, 5);
List<Customer> all = query.list();
for (Customer customer : all) {
System.out.println(customer);
}
//* 使用hibernate提供的别名
Query query1 = session.createQuery("from Customer c where c.id = :id ");
query1.setInteger("id", 5); //第一个参数就是hql中定义名称 :名称
List<Customer> all = query1.list();
for (Customer customer : all) {
System.out.println(customer);
}
transaction.commit();
session.close();
}
@Test
public void demo07(){
// 聚合函数
Session session = SessionUtils.openSession();
Transaction transaction = session.beginTransaction();
// 使用hibernate提供的别名
Query query = session.createQuery("select count(*) from Customer");
Object obj = query.uniqueResult();
Long totalPage = (Long) obj;
transaction.commit();
session.close();
}
@Test
public void demo08(){
// 左外连接 -- left outer join
Session session = SessionUtils.openSession();
Transaction transaction = session.beginTransaction();
//* 使用hibernate提供的别名
Query query = session.createQuery("from Customer c left outer join c.orderSet");
// List 保存数据为 List ,数组中每一个元素又一个对象数组Object[],存放的是Customer,存放是Order
List<Object[]> all = query.list();
for (Object[] objArr : all) {
System.out.println(objArr);
//objArr[0] --> Customer
//objArr[1] --> Order
}
transaction.commit();
session.close();
}
@Test
public void demo09(){
// 迫切左外连接 -- left outer join fetch , 将数据封装到对象
Session session = SessionUtils.openSession();
Transaction transaction = session.beginTransaction();
//* 使用hibernate提供的别名 , 使用 distinct进行去重
Query query = session.createQuery("select distinct c from Customer c left outer join fetch c.orderSet");
List<Customer> all = query.list();
for (Customer customer : all) {
System.out.println(customer);
}
transaction.commit();
session.close();
}
@Test
public void demo10(){
// 命名查询
Session session = SessionUtils.openSession();
Transaction transaction = session.beginTransaction();
Query query = session.getNamedQuery("findAllCustomer"); //获得配置文件中的名称
System.out.println(query.list().size());
transaction.commit();
session.close();
}
}
命名查询的hbm文件配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.my.bean">
<class name="Customer" table="t_customer">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name"></property>
<set name="orderSet" cascade="save-update">
<key column="customer_id"/>
<one-to-many class="Order"/>
</set>
</class>
<!-- 定义全局的HQL语句 -->
<query name="findAllCustomer">
<![CDATA[from Customer]]>
</query>
</hibernate-mapping>
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/12082.html