在以前的批量查詢練習的時候練習基本五種查詢方法的使用:html
1.OID查詢---根據對象ID進行查詢java
2.對象屬性導航查詢: obj.getXXX程序員
3.HQL查詢:Query對象查詢web
4.QBC(QueryByCriteria):Criteria對象查詢sql
5.原生SQL查詢:SQLQuery查詢數據庫
測試代碼以下:編程
package cn.qlq.query; import java.util.Set; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.SQLQuery; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.criterion.Restrictions; import org.junit.Test; import cn.qlq.domain.Customer; import cn.qlq.domain.LinkMan; import cn.qlq.util.HibernateUtil; /** * 以前的查詢總結 * @author liqiang * */ public class HibernateQuery { @Test /** * OID查詢(get方法) */ public void fun1(){ Session session = HibernateUtil.openSession(); Transaction tx = session.beginTransaction(); Customer customer = session.get(Customer.class, 7l); System.out.println(customer); tx.commit(); session.close(); } @Test /** * 屬性導航語言(對象.getXXX) */ public void fun2(){ Session session = HibernateUtil.openSession(); Transaction tx = session.beginTransaction(); Customer customer = session.get(Customer.class, 7l); Set<LinkMan> linkMens = customer.getLinkMens(); System.out.println(linkMens); tx.commit(); session.close(); } @Test /** * HQL查詢(Query對象進行查詢) */ public void fun3(){ Session session = HibernateUtil.openSession(); Transaction tx = session.beginTransaction(); String hql = "from cn.qlq.domain.Customer";//若是整個項目只有一個類名,能夠省去包名 Query query = session.createQuery(hql); System.out.println(query.list()); tx.commit(); session.close(); } @Test /** * Criteria查詢(也被叫作QBC---query by criteria) */ public void fun4(){ Session session = HibernateUtil.openSession(); Transaction tx = session.beginTransaction(); Criteria criteria = session.createCriteria(Customer.class); // criteria.add(Restrictions.ilike("name", "xxx"));//添加過濾條件 System.out.println(criteria.list()); tx.commit(); session.close(); } @Test /** * 原生SQL查詢(SQLQuery對象進行查詢) */ public void fun5(){ Session session = HibernateUtil.openSession(); Transaction tx = session.beginTransaction(); String sql = "select * from cst_customer"; SQLQuery sqlQuery = session.createSQLQuery(sql); sqlQuery.addEntity(Customer.class); System.out.println(sqlQuery.list()); tx.commit(); session.close(); } }
補充:也能夠用query對象將查詢結果映射爲Map數組
@Test /** * 原生SQL查詢(SQLQuery映射查詢結果爲map) */ public void fun6(){ Session session = HibernateUtil.openSession(); Transaction tx = session.beginTransaction(); String sql = "select * from cst_customer"; SQLQuery sqlQuery = session.createSQLQuery(sql); sqlQuery.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP); System.out.println(sqlQuery.list()); tx.commit(); session.close(); }
結果:session
[{cust_phone=null, cust_name=測試名稱1, cust_mobile=null, cust_linkman=null, cust_industry=null, cust_level=null, cust_source=null, cust_id=7}, {cust_phone=null, cust_name=測試名稱2, cust_mobile=null, cust_linkman=null, cust_industry=null, cust_level=null, cust_source=null, cust_id=8}, {cust_phone=null, cust_name=測試名稱3, cust_mobile=null, cust_linkman=null, cust_industry=null, cust_level=null, cust_source=null, cust_id=9}, {cust_phone=null, cust_name=測試名稱4, cust_mobile=null, cust_linkman=null, cust_industry=null, cust_level=null, cust_source=null, cust_id=10}, {cust_phone=null, cust_name=測試名稱5, cust_mobile=null, cust_linkman=null, cust_industry=null, cust_level=null, cust_source=null, cust_id=11}, {cust_phone=null, cust_name=測試名稱6, cust_mobile=null, cust_linkman=null, cust_industry=null, cust_level=null, cust_source=null, cust_id=12}, {cust_phone=null, cust_name=測試名稱7, cust_mobile=null, cust_linkman=null, cust_industry=null, cust_level=null, cust_source=null, cust_id=13}]框架
HQL是面向對象的查詢語言,它和SQL查詢有些相似,但它使用的是類、對象、屬性的概念,而沒有表和字段的概念,也就是在HQL中若是出現DB中的表和字段名是錯誤的。在Hibernate的各類檢索方式中,官方推薦的是HQL檢索,也是使用最普遍的一種查詢方式。它具備以下功能:
Hibernate提供的Query接口是專門的HQL查詢接口,它可以執行各類複雜的HQL查詢語句。完整的SQL語句結構以下:
Select ... from... where ... group by ... having...order by ...desc/asc
HQL很是相似於SQL。一般狀況下若是查詢表中的全部記錄的時候,查詢語句中可省略select關鍵字,以下:
from Customer
若是執行該語句,則會返回應用程序中的全部的Customer對象,須要注意的是Customer是類名而不是對象名,類名區分大小寫,from不區分大小寫,若是項目中有多個相同的類名須要加包名。
補充:若是但願查詢結果映射爲map,能夠採用:
query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
@Test // HQL查詢全部數據 public void fun1() { // 1 得到session Session session = HibernateUtil.openSession(); // 2.書寫HQL語句 // String hql = "from cn.qlq.domain.Customer";// from 類名全路徑 String hql1 = "from Customer";//整個項目只有一個類名能夠省略包名 // String hql2 = "from java.lang.Object";//寫Object的時候會查詢項目中的全部類,切勿使用,浪費內存 // 3.根據hql建立查詢對象 // Query query = session.createQuery(hql); Query query = session.createQuery(hql1); // Query query = session.createQuery(hql2); // 4.根據查詢對象獲取查詢結果 List<Customer> objs = query.list(); System.out.println(objs); }
結果:
Hibernate: select customer0_.cust_id as cust_id1_0_, customer0_.cust_name as cust_nam2_0_, customer0_.cust_source as cust_sou3_0_, customer0_.cust_industry as cust_ind4_0_, customer0_.cust_level as cust_lev5_0_, customer0_.cust_linkman as cust_lin6_0_, customer0_.cust_phone as cust_pho7_0_, customer0_.cust_mobile as cust_mob8_0_ from cst_customer customer0_ [Customer [cust_id=7, cust_name=測試名稱1], Customer [cust_id=8, cust_name=測試名稱2], Customer [cust_id=9, cust_name=測試名稱3], Customer [cust_id=10, cust_name=測試名稱4], Customer [cust_id=11, cust_name=測試名稱5], Customer [cust_id=12, cust_name=測試名稱6], Customer [cust_id=13, cust_name=測試名稱7]]
@Test //排序查詢 public void fun2() { // 1 得到session Session session = HibernateUtil.openSession(); // 2.書寫HQL語句 String hql1 = "from Customer order by cust_id asc,cust_name desc";//整個項目只有一個類名能夠省略包名 // 3.根據hql建立查詢對象 Query query = session.createQuery(hql1); // 4.根據查詢對象獲取查詢結果 List<Customer> objs = query.list(); System.out.println(objs); }
結果:
Hibernate: select customer0_.cust_id as cust_id1_0_, customer0_.cust_name as cust_nam2_0_, customer0_.cust_source as cust_sou3_0_, customer0_.cust_industry as cust_ind4_0_, customer0_.cust_level as cust_lev5_0_, customer0_.cust_linkman as cust_lin6_0_, customer0_.cust_phone as cust_pho7_0_, customer0_.cust_mobile as cust_mob8_0_ from cst_customer customer0_ order by customer0_.cust_id asc, customer0_.cust_name desc [Customer [cust_id=7, cust_name=測試名稱1], Customer [cust_id=8, cust_name=測試名稱2], Customer [cust_id=9, cust_name=測試名稱3], Customer [cust_id=10, cust_name=測試名稱4], Customer [cust_id=11, cust_name=測試名稱5], Customer [cust_id=12, cust_name=測試名稱6], Customer [cust_id=13, cust_name=測試名稱7]]
(1)?佔位符查詢
相似於JDBC的佔位符,只是hibernate的?下標從0開始,而JDBC的下標從1開始,基本上全部的編程索引都從0開始,惟獨JDBC從1開始。。。。
@Test // HQL的?佔位符查詢 public void fun3() { // 1 得到session Session session = HibernateUtil.openSession(); // 2.書寫HQL語句 // String hql = "from cn.qlq.domain.Customer";// from 類名全路徑 String hql = "from Customer where cust_id = ?";// 若是整個項目中只有這一個類名能夠直接寫名字 // 3.根據hql建立查詢對象 Query query = session.createQuery(hql); // query.setLong(0, 1l);//相似於JDBC的佔位符,只是JDBC的佔位符下標從0開始,hibernate從1開始 query.setParameter(0, 1l);//這種寫法不用管類型 // 4.根據查詢對象獲取查詢結果 Customer customer = (Customer) query.uniqueResult(); System.out.println(customer); }
(2)命令佔位符 :name格式的查詢,固定格式,name隨便起,習慣性的起作和條件名字同樣
@Test // HQL的命令佔位符查詢 public void fun4() { // 1 得到session Session session = HibernateUtil.openSession(); // 2.書寫HQL語句 // String hql = "from cn.qlq.domain.Customer";// from 類名全路徑 String hql = "from Customer where cust_id = :cust_id";// :cust_id的名字隨便起,只不過習慣性的起作同樣 // 3.根據hql建立查詢對象 Query query = session.createQuery(hql); // query.setLong(0, 1l);//相似於JDBC的佔位符,只是JDBC的佔位符下標從0開始,hibernate從1開始 query.setParameter("cust_id",1l); // 4.根據查詢對象獲取查詢結果 Customer customer = (Customer) query.uniqueResult(); System.out.println(customer); }
@Test // HQL分頁查詢 public void fun5() { // 1 得到session Session session = HibernateUtils.openSession(); // 2.書寫HQL語句 // String hql = "from cn.qlq.domain.Customer";// from 類名全路徑 String hql = "from Customer order by cust_id desc";// 3.根據hql建立查詢對象 Query query = session.createQuery(hql); /** * 相似於 limit start,pageSize; * 假設頁大小是2 * 頁號 起始值 頁大小 * 1 0 2 * 2 2 2 */ //例如取第二頁數據 query.setFirstResult(2); query.setMaxResults(2); // 4.根據查詢對象獲取查詢結果 List<Customer> customers = query.list(); System.out.println(customers); }
@Test //彙集函數的使用 public void fun3() { // 1 得到session Session session = HibernateUtil.openSession(); // 2.書寫HQL語句 String hql1 = "select count(*) from Customer";//統計總數 String hql2 = "select avg(cust_id) from Customer";//平均 String hql3 = "select sum(cust_id) from Customer";//和 String hql4 = "select max(cust_id) from Customer";//最大 String hql5 = "select min(cust_id) from Customer";//最小 // 3.根據hql建立查詢對象 Query query = session.createQuery(hql4); // 4.根據查詢對象獲取查詢結果 Number number = (Number) query.uniqueResult();//Number是全部數字類型的父類 System.out.println(number); }
(1)投影單個屬性
@Test //投影單個 public void fun7() { // 1 得到session Session session = HibernateUtil.openSession(); // 2.書寫HQL語句 String hql1 = "select cust_name from Customer";//整個項目只有一個類名能夠省略包名 // 3.根據hql建立查詢對象 Query query = session.createQuery(hql1); // 4.根據查詢對象獲取查詢結果 List<String> objs = query.list();//那一列是什麼類型用什麼類型,也能夠省去不寫 System.out.println(objs); }
結果:
Hibernate: select customer0_.cust_name as col_0_0_ from cst_customer customer0_ [測試名稱1, 測試名稱2, 測試名稱3, 測試名稱4, 測試名稱5, 測試名稱6, 測試名稱7]
(2)投影多列---映射爲數組
@Test //投影多列 public void fun7() { // 1 得到session Session session = HibernateUtil.openSession(); // 2.書寫HQL語句 String hql1 = "select cust_id,cust_name from Customer";//整個項目只有一個類名能夠省略包名 // 3.根據hql建立查詢對象 Query query = session.createQuery(hql1); // 4.根據查詢對象獲取查詢結果 List<Object[]> objs = query.list();//那一列是什麼類型用什麼類型,也能夠省去不寫 System.out.println(objs); }
結果:
Hibernate: select customer0_.cust_id as col_0_0_, customer0_.cust_name as col_1_0_ from cst_customer customer0_ [[Ljava.lang.Object;@29bd3793, [Ljava.lang.Object;@458ba94d, [Ljava.lang.Object;@541821e6, [Ljava.lang.Object;@7c59ae2b, [Ljava.lang.Object;@63b9c8d4, [Ljava.lang.Object;@3a300972, [Ljava.lang.Object;@27bc3936]
(3)將投影映射爲對象:(構造方式查詢)
在原來的類中建立須要投影的屬性的構造方法,同時須要顯示的建立無參構造函數:
測試代碼:
@Test //投影多列---映射爲JavaBean public void fun7() { // 1 得到session Session session = HibernateUtil.openSession(); // 2.書寫HQL語句 String hql1 = "select new Customer(cust_id,cust_name) from Customer";//整個項目只有一個類名能夠省略包名 // 3.根據hql建立查詢對象 Query query = session.createQuery(hql1); // 4.根據查詢對象獲取查詢結果 List<Customer> objs = query.list();//那一列是什麼類型用什麼類型,也能夠省去不寫 System.out.println(objs); }
結果
Hibernate: select customer0_.cust_id as col_0_0_, customer0_.cust_name as col_1_0_ from cst_customer customer0_ [Customer [cust_id=7, cust_name=測試名稱1], Customer [cust_id=8, cust_name=測試名稱2], Customer [cust_id=9, cust_name=測試名稱3], Customer [cust_id=10, cust_name=測試名稱4], Customer [cust_id=11, cust_name=測試名稱5], Customer [cust_id=12, cust_name=測試名稱6], Customer [cust_id=13, cust_name=測試名稱7]]
@Test public void fun8() { // 1 得到session Session session = HibernateUtil.openSession(); // 2.書寫HQL語句 String hql1 = "select cust_id,cust_name from Customer";//整個項目只有一個類名能夠省略包名 // 3.根據hql建立查詢對象 Query query = session.createQuery(hql1); query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP); // 4.根據查詢對象獲取查詢結果 List<Map<String,Object>> objs = query.list();//那一列是什麼類型用什麼類型,也能夠省去不寫 System.out.println(objs); }
結果:
[{1=測試名稱1, 0=7}, {1=測試名稱2, 0=8}, {1=測試名稱3, 0=9}, {1=測試名稱4, 0=10}, {1=測試名稱5, 0=11}, {1=測試名稱6, 0=12}, {1=測試名稱7, 0=13}]
多表查詢分爲交叉鏈接(笛卡爾積)、內鏈接(隱士內鏈接、顯示內鏈接),外連接(左外鏈接和右外鏈接),測試語句以下:
package cn.qlq.HQL; import java.util.Arrays; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; import org.junit.Test; import cn.qlq.domain.Customer; import cn.qlq.util.HibernateUtil; //學習HQL語法(不經常使用) - 多表查詢語法 public class Demo2 { //回顧-原生SQL // 交叉鏈接-笛卡爾積(避免) // select * from A,B // 內鏈接 // |-隱式內鏈接 // select * from A,B where b.aid = a.id // |-顯式內鏈接 // select * from A inner join B on b.aid = a.id // 外鏈接 // |- 左外 // select * from A left [outer] join B on b.aid = a.id // |- 右外 // select * from A right [outer] join B on b.aid = a.id //--------------------------------------------------------------------- //HQL的多表查詢 //內鏈接(迫切) //外鏈接 // |-左外(迫切) // |-右外(迫切) @Test //HQL 內鏈接 => 將鏈接的兩端對象分別返回.放到數組中. public void fun0(){ Session session = HibernateUtil.openSession(); Transaction tx = session.beginTransaction(); //---------------------------------------------------- String hql = " from Customer c inner join c.linkMens "; Query query = session.createQuery(hql); List<Object[]> list = query.list(); for(Object[] arr : list){ System.out.println(Arrays.toString(arr)); } //---------------------------------------------------- tx.commit(); session.close(); } @Test //HQL 內鏈接 => 帶查詢條件,查詢cust_id爲9的數據 public void fun1(){ Session session = HibernateUtil.openSession(); Transaction tx = session.beginTransaction(); //---------------------------------------------------- String hql = " from Customer c inner join c.linkMens where cust_id = 9l"; Query query = session.createQuery(hql); List<Object[]> list = query.list(); for(Object[] arr : list){ System.out.println(Arrays.toString(arr)); } //---------------------------------------------------- tx.commit(); session.close(); } @Test //HQL 迫切內鏈接 => 幫咱們進行封裝.返回值就是一個對象 public void fun2(){ Session session = HibernateUtil.openSession(); Transaction tx = session.beginTransaction(); //---------------------------------------------------- String hql = " from Customer c inner join fetch c.linkMens "; Query query = session.createQuery(hql); List<Customer> list = query.list(); System.out.println(list); //---------------------------------------------------- tx.commit(); session.close(); } @Test //HQL 左外鏈接 => 將鏈接的兩端對象分別返回.放到數組中. public void fun3(){ Session session = HibernateUtil.openSession(); Transaction tx = session.beginTransaction(); //---------------------------------------------------- String hql = " from Customer c left join c.linkMens "; Query query = session.createQuery(hql); List<Object[]> list = query.list(); for(Object[] arr : list){ System.out.println(Arrays.toString(arr)); } //---------------------------------------------------- tx.commit(); session.close(); } @Test //HQL 右外鏈接 => 將鏈接的兩端對象分別返回.放到數組中. public void fun4(){ Session session = HibernateUtil.openSession(); Transaction tx = session.beginTransaction(); //---------------------------------------------------- String hql = " from Customer c right join c.linkMens "; Query query = session.createQuery(hql); List<Object[]> list = query.list(); for(Object[] arr : list){ System.out.println(Arrays.toString(arr)); } //---------------------------------------------------- tx.commit(); session.close(); } }
QBC(Query By Criteria)是Hibernate創提供的另外一種檢索對象的方式,它主要由Criteria接口、Criterion接口和Expression類組成。Criteria是HibernateAPI中的一個查詢接口,它須要由session進行建立(由session建立的Criteria稱爲在線Criteria對象,查詢哪一個實體將哪一個實體的class字節碼對象傳給session)。Criterion是Criteria的查詢條件,在Criteria中提供了add(Criterion c)方法來添加查詢條件。使用QBC檢索對象的示例代碼以下:
Criteria criteria = session.createCriteria(Customer.class); criteria.add(Restrictions.eq("cust_id", 1l)); Customer customer = (Customer) criteria.uniqueResult();
@Test // 查詢全部 public void test1() { // 1 得到session Session session = HibernateUtil.openSession(); // 2.創建criteria進行查詢 Criteria criteria = session.createCriteria(Customer.class); List<Customer> list = criteria.list(); System.out.println(list); }
@Test // 根據ID查詢單個,條件查詢 public void test2() { // 1 得到session Session session = HibernateUtils.openSession(); // 2.創建criteria進行查詢 Criteria criteria = session.createCriteria(Customer.class); criteria.add(Restrictions.eq("cust_id", 1l)); Customer customer = (Customer) criteria.uniqueResult(); System.out.println(customer); }
結果:
Customer [cust_id=1, cust_name=程序員111]
條件列表:
補充:QBC也能夠傳SQL條件,例如:
@Test // 根據ID查詢單個,條件查詢 public void test2() { // 1 得到session Session session = HibernateUtils.openSession(); // 2.創建criteria進行查詢 Criteria criteria = session.createCriteria(Customer.class); criteria.add(Restrictions.sqlRestriction("cust_id = 1")); criteria.add(Restrictions.sqlRestriction("cust_name = '張三'")); Customer customer = (Customer) criteria.uniqueResult(); System.out.println(customer); }
結果:
user建立對象 Hibernate: select this_.cust_id as cust_id1_0_0_, this_.cust_name as cust_nam2_0_0_, this_.cust_source as cust_sou3_0_0_, this_.cust_industry as cust_ind4_0_0_, this_.isPerson as isPerson5_0_0_, this_.cust_level as cust_lev6_0_0_, this_.cust_linkman as cust_lin7_0_0_, this_.cust_phone as cust_pho8_0_0_, this_.cust_mobile as cust_mob9_0_0_ from cst_customer this_ where cust_id = 1 and cust_name = '張三' Customer [cust_id=1, cust_name=張三]
@Test // 分頁查詢 public void test3() { // 1 得到session Session session = HibernateUtils.openSession(); // 2.創建criteria進行查詢 Criteria criteria = session.createCriteria(Customer.class); /** * 相似於 limit start,pageSize; * 假設頁大小是2 * 頁號 起始值 頁大小 * 1 0 2 * 2 2 2 */ criteria.setFirstResult(2); criteria.setMaxResults(2); List<Customer> list = criteria.list(); System.out.println(list); }
結果:
[Customer [cust_id=3, cust_name=測試名稱222], Customer [cust_id=4, cust_name=測試名稱222]]
@Test // 排序和分組 public void test5() { // 1 得到session Session session = HibernateUtils.openSession(); // 2.排序 Criteria criteria = session.createCriteria(Customer.class); criteria.addOrder(Order.desc("cust_id")); List<Customer> list = criteria.list(); System.out.println(list); System.out.println(); // 2.分組 Criteria criteria1 = session.createCriteria(Customer.class); criteria1.setProjection(Projections.groupProperty("cust_name")); List<Customer> list1 = criteria1.list(); System.out.println(list1); }
結果:
[Customer [cust_id=4, cust_name=測試名稱222], Customer [cust_id=3, cust_name=測試名稱222], Customer [cust_id=2, cust_name=新增數據], Customer [cust_id=1, cust_name=程序員111]]
[新增數據, 測試名稱222, 程序員111]
@Test // 查詢總數(彙集函數的使用) public void test4() { // 1 得到session Session session = HibernateUtils.openSession(); // 2.彙集函數查詢總數 Criteria criteria = session.createCriteria(Customer.class); criteria.setProjection(Projections.rowCount()); Long count = (Long) criteria.uniqueResult(); System.out.println(count); // 3.彙集函數查詢總數第二種方法 Criteria criteria1 = session.createCriteria(Customer.class); criteria1.setProjection(Projections.count("cust_id")); Long count1 = (Long) criteria.uniqueResult(); System.out.println(count1); // 4.彙集函數查詢幾個不一樣的姓名 Criteria criteria2 = session.createCriteria(Customer.class); criteria2.setProjection(Projections.countDistinct("cust_name")); Long count2 = (Long) criteria2.uniqueResult(); System.out.println(count2); }
結果:
4
4
3
@Test // 根據ID查詢單個,條件查詢 public void test22() { // 1 得到session Session session = HibernateUtils.openSession(); // 2.創建criteria進行查詢 Criteria criteria = session.createCriteria(Customer.class); criteria.add(Restrictions.or(Restrictions.eq("cust_name", "李四1"), Restrictions.eq("cust_name", "李四2"))); criteria.add(Restrictions.isNotNull("cust_name")); List list = criteria.list(); System.out.println(list); }
結果: (這種寫法是一個 or ,至關於 where xxx=xx and ( xxx = xxx or xxxx=xxxx or ....) )
user建立對象
Hibernate:
select this_.cust_id as cust_id1_0_0_, this_.cust_name as cust_nam2_0_0_, this_.cust_source as cust_sou3_0_0_, this_.cust_industry as cust_ind4_0_0_, this_.isPerson as isPerson5_0_0_, this_.cust_level as cust_lev6_0_0_, this_.cust_linkman as cust_lin7_0_0_, this_.cust_phone as cust_pho8_0_0_, this_.cust_mobile as cust_mob9_0_0_ from cst_customer this_ where ( this_.cust_name=? or this_.cust_name=? ) and this_.cust_name is not null
[Customer [cust_id=3, cust_name=李四1], Customer [cust_id=4, cust_name=李四2]]
第二種寫法:(建議這種---多個or的時候清晰)
@Test // 根據ID查詢單個,條件查詢 public void test222() { // 1 得到session Session session = HibernateUtils.openSession(); // 2.創建criteria進行查詢 Criteria criteria = session.createCriteria(Customer.class); criteria.add(Restrictions.isNotNull("cust_name")); Disjunction disjunction = Restrictions.disjunction(); disjunction.add(Restrictions.eq("cust_name", "李四1")); disjunction.add(Restrictions.eq("cust_name", "李四2")); criteria.add(disjunction); List list = criteria.list(); System.out.println(list); }
結果:
user建立對象 Hibernate: select this_.cust_id as cust_id1_0_0_, this_.cust_name as cust_nam2_0_0_, this_.cust_source as cust_sou3_0_0_, this_.cust_industry as cust_ind4_0_0_, this_.isPerson as isPerson5_0_0_, this_.cust_level as cust_lev6_0_0_, this_.cust_linkman as cust_lin7_0_0_, this_.cust_phone as cust_pho8_0_0_, this_.cust_mobile as cust_mob9_0_0_ from cst_customer this_ where this_.cust_name is not null and ( this_.cust_name=? or this_.cust_name=? ) [Customer [cust_id=3, cust_name=李四1], Customer [cust_id=4, cust_name=李四2]]
因此當只須要一個or條件時能夠用Restrictions.or();當用到多個or條件時,就須要用到Restrictions.disjunction();
補充:
Spring 的框架提供了getHibernateTemplate().findByCriteria(detachedCriteria) 方法能夠很方便地根據DetachedCriteria來返回查詢結果。
應用場景通常是動態條件查詢,咱們能夠在web層或者service層根據業務建立DetachedCriteria對象並封裝動態條件傳到dao層,dao層根據DetachedCriteria對象獲取在線Criteria查詢數據庫,實現解耦。使用步驟大概以下:
(1)建立DetachedCriteria對象的方式有兩種:
//第一種方法:傳入class DetachedCriteria dc = DetachedCriteria.forClass(Customer.class);//查詢哪一個對象將哪一個對象的字節碼傳進去 //第二種方法:傳入全類名(必須攜帶包名) DetachedCriteria dc = DetachedCriteria.forEntityName("cn.qlq.domain.Customer");//
(2)DetachedCriteria對象封裝查詢條件與Criteria對象同樣:
dc.add(Restrictions.idEq(7l));
(3)經過DetachedCriteria構造在線Criiteria對象方法:(須要Session環境)
// 1 得到session Session session = HibernateUtil.openSession(); // 2.經過DetachedCriteria構造Criteria對象 Criteria criteria = dc.getExecutableCriteria(session);//獲取在線criteria對象
(4)經過Criteria進行查詢:(與原來同樣)
//1.查詢集合 // List<Customer> list = criteria.list(); //2.查詢單個對象 Object uniqueResult = criteria.uniqueResult();
在線Criteria的查詢圖解以下:
離線Criteria圖解以下:
線下Criteria測試代碼:
@Test public void test1() { /*******************S Web層或者service層 **************************/ //0.構造DetachedCriteria對象封裝查詢條件 //第一種方法:傳入class // DetachedCriteria dc = DetachedCriteria.forClass(Customer.class);//查詢哪一個對象將哪一個對象的字節碼傳進去 //第二種方法:傳入全類名(必須攜帶包名) DetachedCriteria dc = DetachedCriteria.forEntityName("cn.qlq.domain.Customer");// dc.add(Restrictions.idEq(7l)); /*******************E Web層或者service層 **************************/ /********************S Dao層************************************/ // 1 得到session Session session = HibernateUtil.openSession(); // 2.經過DetachedCriteria構造Criteria對象 Criteria criteria = dc.getExecutableCriteria(session);//獲取在線criteria對象 List<Customer> list = criteria.list(); System.out.println(list); /********************E Dao層************************************/ }
結果:
Hibernate: select this_.cust_id as cust_id1_0_0_, this_.cust_name as cust_nam2_0_0_, this_.cust_source as cust_sou3_0_0_, this_.cust_industry as cust_ind4_0_0_, this_.cust_level as cust_lev5_0_0_, this_.cust_linkman as cust_lin6_0_0_, this_.cust_phone as cust_pho7_0_0_, this_.cust_mobile as cust_mob8_0_0_ from cst_customer this_ where this_.cust_id = ? [Customer [cust_id=7, cust_name=測試名稱1]]
user建立對象Hibernate: select this_.cust_id as cust_id1_0_0_, this_.cust_name as cust_nam2_0_0_, this_.cust_source as cust_sou3_0_0_, this_.cust_industry as cust_ind4_0_0_, this_.isPerson as isPerson5_0_0_, this_.cust_level as cust_lev6_0_0_, this_.cust_linkman as cust_lin7_0_0_, this_.cust_phone as cust_pho8_0_0_, this_.cust_mobile as cust_mob9_0_0_ from cst_customer this_ where cust_id = 1 and cust_name = '張三'Customer [cust_id=1, cust_name=張三]