目錄
概述:
1、查詢對象
查詢結果:
2、分⻚查詢
結果:
3、where 條件查詢
結果:
4、模糊查詢
結果:
5、order by
結果:
6、查詢實體對象的屬性
結果:
7、佔位符
結果:
8、級聯查詢
結果:
概述:
HQL : Hibernate Query Language ,是 Hibernate 框架提供的⼀種查詢機制,它和 SQL 類似,不同的
是 HQL 是⾯向對象的查詢語句,讓開發者能夠以⾯向對象的思想來編寫查詢語句,對 Java 編程是⼀種很友好的⽅式。
HQL 不能直接參與數據庫的交互,中間層語⾔。
Java --- 》 HQL --- 〉 Hibernate --- 》 SQL --- 〉 DB
HQL 只能完成查詢、修改、刪除,新增是⽆法操作的。
1、查詢對象
查詢表中所有數據,⾃動完成對象的封裝,返回 List 集合。
HQL 進⾏查詢, from 關鍵字後⾯不能寫表名,必須寫表對應的實體類名。
//1、實體類
package com.southwind.entity;
import lombok.Data;
@Data
public class People {
private Integer id;
private String name;
private Double money;
}
<!--2、People.hbm.xml文件-->
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping> <!--文件名的hbm就是hibernate-mapping 的縮寫,在這裏面完成類和表的映射-->
<!-- class 就是指類,name就是指類名 table 就是指對應的表名,表customer裏面有id和name兩個字段-->
<class name="com.southwind.entity.People" table="people">
<!-- 主鍵映射 name指的是實體類裏面的屬性:id type指的是id屬性的類型-->
<id name="id" type="java.lang.Integer">
<!--column是字段,指的是對應類裏面id的表裏面的名稱-->
<column name="id"></column>
<!--這個是配置主鍵自增的方式,identity的意思就是自增-->
<generator class="identity"></generator>
</id>
<!-- 下面繼續配置類裏面的其他信息,name type指的是name屬性的類型-->
<property name="name" type="java.lang.String">
<!--column是字段,指的是對應類裏面name的表裏面的名稱-->
<column name="name"></column>
</property>
<!-- 下面繼續配置類裏面的其他信息,money type指的是money屬性的類型-->
<property name="money" type="java.lang.Double">
<!--column是字段,指的是對應類裏面money的表裏面的名稱-->
<column name="money"></column>
</property>
</class>
</hibernate-mapping>
<!--3、hibernate.cfg.xml文件-->
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<!-- 核⼼配置:session-factory-->
<!-- SessionFactory:針對單個數據庫映射經過編譯的內存鏡像⽂件,將數據庫轉換為⼀個 Java 可以識別的鏡像⽂件。-->
<!-- 構建 SessionFactory ⾮常耗費資源,所以通常⼀個⼯程只需要創建⼀個 SessionFactory。-->
<session-factory>
<!-- datasource 數據源配置 -->
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT</property>
<!--?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT" />-->
<!-- C3P0 連接池 -->
<property name="hibernate.c3p0.acquire_increment">10</property> <!-- 每次不夠的話就會增加的數量-->
<property name="hibernate.c3p0.idle_test_period">10000</property> <!--釋放資源時間的設置,s為單位-->
<property name="hibernate.c3p0.timeout">5000</property> <!-- 超時時間-->
<property name="hibernate.c3p0.max_size">30</property> <!-- 最大連接數-->
<property name="hibernate.c3p0.min_size">5</property> <!-- 最小連接數-->
<property name="hibernate.c3p0.max_statements">10</property> <!-- 最大線程數數-->
<!-- 數據庫⽅⾔ oracle或mysql-->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 打印SQL語句,固定寫法 -->
<property name="show_sql">true</property>
<!-- 格式化SQL語句,固定寫法 -->
<property name="format_sql">true</property>
<!-- 是否⾃動⽣成數據表-->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- create:每次加載hibernate時都會刪除上一次的生成的表,然後根據你的model類再重新來生成新表,
哪怕兩次沒有任何改變也要這樣執行,這就是導致數據庫表數據丟失的一個重要原因。
create-drop :每次加載hibernate時根據model類生成表,但是sessionFactory一關閉,表就自動刪除。
update:最常用的屬性,第一次加載hibernate時根據model類會自動建立起表的結構(前提是先建立好數據庫),
以後加載hibernate時根據 model類自動更新表結構,即使表結構改變了但表中的行仍然存在不會刪除以前的行。
要注意的是當部署到服務器後,表結構是不會被馬上建立起來的,是要等 應用第一次運行起來後才會。
validate :每次加載hibernate時,驗證創建數據庫表結構,只會和數據庫中的表進行比較,不會創建新表,但是會插入新值。
-->
<!-- 註冊實體關係映射文件 -->
<mapping resource="com/southwind/entity/People.hbm.xml"></mapping>
<mapping resource="com/southwind/entity/Customer.hbm.xml"></mapping>
<mapping resource="com/southwind/entity/Orders.hbm.xml"></mapping>
<mapping resource="com/southwind/entity/Account.hbm.xml"></mapping>
<mapping resource="com/southwind/entity/Course.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
//4、test
package com.southwind.test;
import com.southwind.entity.Customer;
import com.southwind.entity.Orders;
import com.southwind.entity.People;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
import java.util.List;
public class Test11 {
public static void main(String[] args) {
//創建 Configuration
Configuration configuration = new Configuration().configure();
//獲取 SessionFactory
SessionFactory sessionFactory = configuration.buildSessionFactory();
//獲取 Session
Session session = sessionFactory.openSession();
//查詢對象
String hql = "from People";
Query query = session.createQuery(hql);
List<People> list = query.list();
for(People people:list){
System.out.println(people);
}
//分⻚查詢
// String hql = "from People";
// Query query = session.createQuery(hql);
// query.setFirstResult(1);//設置起始下標
// query.setMaxResults(3);//設置截取⻓度
// List<People> list = query.list();
// for(People people:list){
// System.out.println(people);
// }
//where 條件查詢
// String hql = "from People where id = 4";
// Query query = session.createQuery(hql);
// People people = (People) query.uniqueResult();
// System.out.println(people);
//模糊查詢
// String hql = "from People where name like '%四%'";
// Query query = session.createQuery(hql);
// List<People> list = query.list();
// for(People people:list){
// System.out.println(people);
// }
//order by排序查詢
// String hql = "from People order by id desc ";
// Query query = session.createQuery(hql);
// List<People> list = query.list();
// for(People people:list){
// System.out.println(people);
// }
//查詢實體對象的屬性,姓名
// String hql = "select name from People where id = 6";
// Query query = session.createQuery(hql);
// String name = (String) query.uniqueResult();
// System.out.println(name);
//佔位符
// String hql = "from People where name = :name";
// Query query = session.createQuery(hql);
// query.setString("name","張三");
// List<People> list = query.list();
// for (People people:list){
// System.out.println(people);
// }
//級聯查詢
// String hql1 = "from Customer where name = :name";
// Query query1 = session.createQuery(hql1);
// query1.setString("name","張三");
// Customer customer = (Customer) query1.uniqueResult();
// String hql2 = "from Orders where customer = :customer";
// Query query2 = session.createQuery(hql2);
// query2.setEntity("customer",customer);
// List<Orders> list = query2.list();
// for(Orders orders:list){
// System.out.println(orders);
// }
session.close();
}
}
查詢結果:
2、分⻚查詢
HQL 分⻚查詢可以通過調⽤ query 的⽅法來完成。
1、setFirstResult() 設置起始下標
2、setMaxResults() 設置截取⻓度
package com.southwind.test;
import com.southwind.entity.People;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
import java.util.List;
public class Test11 {
public static void main(String[] args) {
//創建 Configuration
Configuration configuration = new Configuration().configure();
//獲取 SessionFactory
SessionFactory sessionFactory = configuration.buildSessionFactory();
//獲取 Session
Session session = sessionFactory.openSession();
// String hql = "from People";
// Query query = session.createQuery(hql);
// List<People> list = query.list();
// for(People people:list){
// System.out.println(people);
// }
String hql = "from People";
Query query = session.createQuery(hql);
//從第一個開始,往後面3個
query.setFirstResult(1);//設置起始下標
query.setMaxResults(3);//設置截取⻓度
List<People> list = query.list();
for(People people:list){
System.out.println(people);
}
// String hql = "from People where id = 0";
// Query query = session.createQuery(hql);
// People people = (People) query.uniqueResult();
// System.out.println(people);
// String hql = "from People where name like '%三%'";
// Query query = session.createQuery(hql);
// List<People> list = query.list();
// for(People people:list){
// System.out.println(people);
// }
// String hql = "from People order by id asc ";
// Query query = session.createQuery(hql);
// List<People> list = query.list();
// for(People people:list){
// System.out.println(people);
// }
// String hql = "select name from People where id = 6";
// Query query = session.createQuery(hql);
// String name = (String) query.uniqueResult();
// System.out.println(name);
// String hql = "from People where name = :name";
// Query query = session.createQuery(hql);
// query.setString("name","張三");
// List<People> list = query.list();
// for (People people:list){
// System.out.println(people);
// }
// String hql1 = "from Customer where name = :name";
// Query query1 = session.createQuery(hql1);
// query1.setString("name","張三");
// Customer customer = (Customer) query1.uniqueResult();
// String hql2 = "from Orders where customer = :customer";
// Query query2 = session.createQuery(hql2);
// query2.setEntity("customer",customer);
// List<Orders> list = query2.list();
// for(Orders orders:list){
// System.out.println(orders);
// }
session.close();
}
}
結果:
3、where 條件查詢
HQL 直接追加 where 關鍵字作為查詢條件,與 SQL 沒有區別。
package com.southwind.test;
import com.southwind.entity.People;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
import java.util.List;
public class Test11 {
public static void main(String[] args) {
//創建 Configuration
Configuration configuration = new Configuration().configure();
//獲取 SessionFactory
SessionFactory sessionFactory = configuration.buildSessionFactory();
//獲取 Session
Session session = sessionFactory.openSession();
// String hql = "from People";
// Query query = session.createQuery(hql);
// List<People> list = query.list();
// for(People people:list){
// System.out.println(people);
// }
// String hql = "from People";
// Query query = session.createQuery(hql);
// query.setFirstResult(1);//設置起始下標
// query.setMaxResults(3);//設置截取⻓度
// List<People> list = query.list();
// for(People people:list){
// System.out.println(people);
// }
String hql = "from People where id = 4";
Query query = session.createQuery(hql);
People people = (People) query.uniqueResult();
System.out.println(people);
// String hql = "from People where name like '%三%'";
// Query query = session.createQuery(hql);
// List<People> list = query.list();
// for(People people:list){
// System.out.println(people);
// }
// String hql = "from People order by id asc ";
// Query query = session.createQuery(hql);
// List<People> list = query.list();
// for(People people:list){
// System.out.println(people);
// }
// String hql = "select name from People where id = 6";
// Query query = session.createQuery(hql);
// String name = (String) query.uniqueResult();
// System.out.println(name);
// String hql = "from People where name = :name";
// Query query = session.createQuery(hql);
// query.setString("name","張三");
// List<People> list = query.list();
// for (People people:list){
// System.out.println(people);
// }
// String hql1 = "from Customer where name = :name";
// Query query1 = session.createQuery(hql1);
// query1.setString("name","張三");
// Customer customer = (Customer) query1.uniqueResult();
// String hql2 = "from Orders where customer = :customer";
// Query query2 = session.createQuery(hql2);
// query2.setEntity("customer",customer);
// List<Orders> list = query2.list();
// for(Orders orders:list){
// System.out.println(orders);
// }
session.close();
}
}
結果:
4、模糊查詢
查詢名稱包含 “四 ” 的所有記錄
package com.southwind.test;
import com.southwind.entity.People;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
import java.util.List;
public class Test11 {
public static void main(String[] args) {
//創建 Configuration
Configuration configuration = new Configuration().configure();
//獲取 SessionFactory
SessionFactory sessionFactory = configuration.buildSessionFactory();
//獲取 Session
Session session = sessionFactory.openSession();
// String hql = "from People";
// Query query = session.createQuery(hql);
// List<People> list = query.list();
// for(People people:list){
// System.out.println(people);
// }
// String hql = "from People";
// Query query = session.createQuery(hql);
// query.setFirstResult(1);//設置起始下標
// query.setMaxResults(3);//設置截取⻓度
// List<People> list = query.list();
// for(People people:list){
// System.out.println(people);
// }
// String hql = "from People where id = 4";
// Query query = session.createQuery(hql);
// People people = (People) query.uniqueResult();
// System.out.println(people);
String hql = "from People where name like '%四%'";
Query query = session.createQuery(hql);
List<People> list = query.list();
for(People people:list){
System.out.println(people);
}
// String hql = "from People order by id asc ";
// Query query = session.createQuery(hql);
// List<People> list = query.list();
// for(People people:list){
// System.out.println(people);
// }
// String hql = "select name from People where id = 6";
// Query query = session.createQuery(hql);
// String name = (String) query.uniqueResult();
// System.out.println(name);
// String hql = "from People where name = :name";
// Query query = session.createQuery(hql);
// query.setString("name","張三");
// List<People> list = query.list();
// for (People people:list){
// System.out.println(people);
// }
// String hql1 = "from Customer where name = :name";
// Query query1 = session.createQuery(hql1);
// query1.setString("name","張三");
// Customer customer = (Customer) query1.uniqueResult();
// String hql2 = "from Orders where customer = :customer";
// Query query2 = session.createQuery(hql2);
// query2.setEntity("customer",customer);
// List<Orders> list = query2.list();
// for(Orders orders:list){
// System.out.println(orders);
// }
session.close();
}
}
結果:
5、order by
按照 id 進⾏降序排序
package com.southwind.test;
import com.southwind.entity.People;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
import java.util.List;
public class Test11 {
public static void main(String[] args) {
//創建 Configuration
Configuration configuration = new Configuration().configure();
//獲取 SessionFactory
SessionFactory sessionFactory = configuration.buildSessionFactory();
//獲取 Session
Session session = sessionFactory.openSession();
// String hql = "from People";
// Query query = session.createQuery(hql);
// List<People> list = query.list();
// for(People people:list){
// System.out.println(people);
// }
// String hql = "from People";
// Query query = session.createQuery(hql);
// query.setFirstResult(1);//設置起始下標
// query.setMaxResults(3);//設置截取⻓度
// List<People> list = query.list();
// for(People people:list){
// System.out.println(people);
// }
// String hql = "from People where id = 4";
// Query query = session.createQuery(hql);
// People people = (People) query.uniqueResult();
// System.out.println(people);
// String hql = "from People where name like '%四%'";
// Query query = session.createQuery(hql);
// List<People> list = query.list();
// for(People people:list){
// System.out.println(people);
// }
String hql = "from People order by id desc ";
Query query = session.createQuery(hql);
List<People> list = query.list();
for(People people:list){
System.out.println(people);
}
// String hql = "select name from People where id = 6";
// Query query = session.createQuery(hql);
// String name = (String) query.uniqueResult();
// System.out.println(name);
// String hql = "from People where name = :name";
// Query query = session.createQuery(hql);
// query.setString("name","張三");
// List<People> list = query.list();
// for (People people:list){
// System.out.println(people);
// }
// String hql1 = "from Customer where name = :name";
// Query query1 = session.createQuery(hql1);
// query1.setString("name","張三");
// Customer customer = (Customer) query1.uniqueResult();
// String hql2 = "from Orders where customer = :customer";
// Query query2 = session.createQuery(hql2);
// query2.setEntity("customer",customer);
// List<Orders> list = query2.list();
// for(Orders orders:list){
// System.out.println(orders);
// }
session.close();
}
}
結果:
6、查詢實體對象的屬性
package com.southwind.test;
import com.southwind.entity.People;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
import java.util.List;
public class Test11 {
public static void main(String[] args) {
//創建 Configuration
Configuration configuration = new Configuration().configure();
//獲取 SessionFactory
SessionFactory sessionFactory = configuration.buildSessionFactory();
//獲取 Session
Session session = sessionFactory.openSession();
//查詢對象
// String hql = "from People";
// Query query = session.createQuery(hql);
// List<People> list = query.list();
// for(People people:list){
// System.out.println(people);
// }
//分⻚查詢
// String hql = "from People";
// Query query = session.createQuery(hql);
// query.setFirstResult(1);//設置起始下標
// query.setMaxResults(3);//設置截取⻓度
// List<People> list = query.list();
// for(People people:list){
// System.out.println(people);
// }
//where 條件查詢
// String hql = "from People where id = 4";
// Query query = session.createQuery(hql);
// People people = (People) query.uniqueResult();
// System.out.println(people);
//模糊查詢
// String hql = "from People where name like '%四%'";
// Query query = session.createQuery(hql);
// List<People> list = query.list();
// for(People people:list){
// System.out.println(people);
// }
//order by排序查詢
// String hql = "from People order by id desc ";
// Query query = session.createQuery(hql);
// List<People> list = query.list();
// for(People people:list){
// System.out.println(people);
// }
//查詢實體對象的屬性,姓名
String hql = "select name from People where id = 6";
Query query = session.createQuery(hql);
String name = (String) query.uniqueResult();
System.out.println(name);
//佔位符
// String hql = "from People where name = :name";
// Query query = session.createQuery(hql);
// query.setString("name","張三");
// List<People> list = query.list();
// for (People people:list){
// System.out.println(people);
// }
//級聯查詢
// String hql1 = "from Customer where name = :name";
// Query query1 = session.createQuery(hql1);
// query1.setString("name","張三");
// Customer customer = (Customer) query1.uniqueResult();
// String hql2 = "from Orders where customer = :customer";
// Query query2 = session.createQuery(hql2);
// query2.setEntity("customer",customer);
// List<Orders> list = query2.list();
// for(Orders orders:list){
// System.out.println(orders);
// }
session.close();
}
}
結果:
7、佔位符
package com.southwind.test;
import com.southwind.entity.People;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
import java.util.List;
public class Test11 {
public static void main(String[] args) {
//創建 Configuration
Configuration configuration = new Configuration().configure();
//獲取 SessionFactory
SessionFactory sessionFactory = configuration.buildSessionFactory();
//獲取 Session
Session session = sessionFactory.openSession();
//查詢對象
// String hql = "from People";
// Query query = session.createQuery(hql);
// List<People> list = query.list();
// for(People people:list){
// System.out.println(people);
// }
//分⻚查詢
// String hql = "from People";
// Query query = session.createQuery(hql);
// query.setFirstResult(1);//設置起始下標
// query.setMaxResults(3);//設置截取⻓度
// List<People> list = query.list();
// for(People people:list){
// System.out.println(people);
// }
//where 條件查詢
// String hql = "from People where id = 4";
// Query query = session.createQuery(hql);
// People people = (People) query.uniqueResult();
// System.out.println(people);
//模糊查詢
// String hql = "from People where name like '%四%'";
// Query query = session.createQuery(hql);
// List<People> list = query.list();
// for(People people:list){
// System.out.println(people);
// }
//order by排序查詢
// String hql = "from People order by id desc ";
// Query query = session.createQuery(hql);
// List<People> list = query.list();
// for(People people:list){
// System.out.println(people);
// }
//查詢實體對象的屬性,姓名
// String hql = "select name from People where id = 6";
// Query query = session.createQuery(hql);
// String name = (String) query.uniqueResult();
// System.out.println(name);
//佔位符
String hql = "from People where name = :name";
Query query = session.createQuery(hql);
query.setString("name","張三");
List<People> list = query.list();
for (People people:list){
System.out.println(people);
}
//級聯查詢
// String hql1 = "from Customer where name = :name";
// Query query1 = session.createQuery(hql1);
// query1.setString("name","張三");
// Customer customer = (Customer) query1.uniqueResult();
// String hql2 = "from Orders where customer = :customer";
// Query query2 = session.createQuery(hql2);
// query2.setEntity("customer",customer);
// List<Orders> list = query2.list();
// for(Orders orders:list){
// System.out.println(orders);
// }
session.close();
}
}
結果:
8、級聯查詢
package com.southwind.test;
import com.southwind.entity.Customer;
import com.southwind.entity.Orders;
import com.southwind.entity.People;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
import java.util.List;
public class Test11 {
public static void main(String[] args) {
//創建 Configuration
Configuration configuration = new Configuration().configure();
//獲取 SessionFactory
SessionFactory sessionFactory = configuration.buildSessionFactory();
//獲取 Session
Session session = sessionFactory.openSession();
//查詢對象
// String hql = "from People";
// Query query = session.createQuery(hql);
// List<People> list = query.list();
// for(People people:list){
// System.out.println(people);
// }
//分⻚查詢
// String hql = "from People";
// Query query = session.createQuery(hql);
// query.setFirstResult(1);//設置起始下標
// query.setMaxResults(3);//設置截取⻓度
// List<People> list = query.list();
// for(People people:list){
// System.out.println(people);
// }
//where 條件查詢
// String hql = "from People where id = 4";
// Query query = session.createQuery(hql);
// People people = (People) query.uniqueResult();
// System.out.println(people);
//模糊查詢
// String hql = "from People where name like '%四%'";
// Query query = session.createQuery(hql);
// List<People> list = query.list();
// for(People people:list){
// System.out.println(people);
// }
//order by排序查詢
// String hql = "from People order by id desc ";
// Query query = session.createQuery(hql);
// List<People> list = query.list();
// for(People people:list){
// System.out.println(people);
// }
//查詢實體對象的屬性,姓名
// String hql = "select name from People where id = 6";
// Query query = session.createQuery(hql);
// String name = (String) query.uniqueResult();
// System.out.println(name);
//佔位符
// String hql = "from People where name = :name";
// Query query = session.createQuery(hql);
// query.setString("name","張三");
// List<People> list = query.list();
// for (People people:list){
// System.out.println(people);
// }
//級聯查詢
String hql1 = "from Customer where name = :name";
Query query1 = session.createQuery(hql1);
query1.setString("name","張三");
Customer customer = (Customer) query1.uniqueResult();
String hql2 = "from Orders where customer = :customer";
Query query2 = session.createQuery(hql2);
query2.setEntity("customer",customer);
List<Orders> list = query2.list();
for(Orders orders:list){
System.out.println(orders);
}
session.close();
}
}