Hibernate的批量查询
?Hibernate的查詢大致分為以下三種場景,
1. HQL查詢-hibernate Query Language(多表查詢,但不復雜時使用)
? ? ?2. Criteria查詢(單表條件查詢)
3. 原生SQL查詢(復雜的業務查詢)
?
接下來解釋三種使用方法:?
?1.HQL查詢-hibernate Query Language(多表查詢,但不復雜時使用) Hibernate獨家查詢語言,屬于面向對象的查詢語言,HQL語言中不會出現SQL中的表和列,HQL使用的都是JavaBean的類名和屬性名。
1.HQL基本查詢
(1)查詢所有的基本語句
@Test// HQL查詢所有數據public void fun1() {// 1 獲得sessionSession session = HibernateUtil.openSession();// 2.書寫HQL語句String hql = "from cn.qlq.domain.Customer";// from 類名全路徑// 3.根據hql創建查詢對象Query query = session.createQuery(hql);// 4.根據查詢對象獲取查詢結果List<Customer> customers = query.list();System.out.println(customers);}?
結果:
Hibernate: selectcustomer0_.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_ fromcst_customer customer0_ [Customer [cust_id=1, cust_name=XXXXXXXXXX], Customer [cust_id=2, cust_name=聯想]]?
?
改進:如果整個項目中只有一個類的名字可以省略包路徑,也就是可以只寫類名:
@Test// HQL查詢所有數據public void fun1() {// 1 獲得sessionSession session = HibernateUtil.openSession();// 2.書寫HQL語句 // String hql = "from cn.qlq.domain.Customer";// from 類名全路徑String hql = "from Customer";// 如果整個項目中只有這一個類名可以直接寫名字// 3.根據hql創建查詢對象Query query = session.createQuery(hql);// 4.根據查詢對象獲取查詢結果List<Customer> customers = query.list();System.out.println(customers);}?
?
(2)根據主鍵查詢單個
@Test// HQL查詢單個數據public void fun2() {// 1 獲得sessionSession session = HibernateUtil.openSession();// 2.書寫HQL語句 // String hql = "from cn.qlq.domain.Customer";// from 類名全路徑String hql = "from Customer where cust_id = 1";// where后面是Customer的屬性名稱而不是列名// 3.根據hql創建查詢對象Query query = session.createQuery(hql);// 4.根據查詢對象獲取查詢結果Customer customer = (Customer) query.uniqueResult();System.out.println(customer);}?
?
?2.HQL條件查詢:
?(1)?占位符查詢
類似于JDBC的占位符,只是hibernate的?下標從0開始,而JDBC的下標從1開始,基本上所有的編程索引都從0開始,唯獨JDBC從1開始。。。。
@Test// HQL的?占位符查詢public void fun3() {// 1 獲得sessionSession 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 獲得sessionSession 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);}?
?
?
3.HQL排序與分頁查詢
? 分頁查詢類似于mysql的limit關鍵字,limit start,pageSize。。。。。。
@Test// HQL分頁查詢public void fun5() {// 1 獲得sessionSession session = HibernateUtils.openSession();// 2.書寫HQL語句 // String hql = "from cn.qlq.domain.Customer";// from 類名全路徑String hql = "from Customer order by cust_id desc";// :cust_id的名字隨便起,只不過習慣性的起做一樣// 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);}?
?
結果:
Hibernate: selectcustomer0_.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_ fromcst_customer customer0_ limit ?,?[Customer [cust_id=2, cust_name=新增數據], Customer [cust_id=1, cust_name=程序員111]]
?
?
?
2.Criteria查詢(單表條件查詢)
criteria查詢相比于上面的HQL查詢要簡單的多。
?1.基本查詢---查詢所有
@Test// 查詢所有public void test1() {// 1 獲得sessionSession session = HibernateUtils.openSession();// 2.創建criteria進行查詢Criteria criteria = session.createCriteria(Customer.class);List<Customer> list = criteria.list();System.out.println(list);}?
結果:
[Customer [cust_id=1, cust_name=程序員111], Customer [cust_id=2, cust_name=新增數據], Customer [cust_id=3, cust_name=測試名稱222], Customer [cust_id=4, cust_name=測試名稱222]]?
?
2. 條件查詢單個:
@Test// 根據ID查詢單個,條件查詢public void test2() {// 1 獲得sessionSession 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]
?
條件列表:
?
?3.分頁查詢:
@Test// 分頁查詢public void test3() {// 1 獲得sessionSession 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]]
?
4.排序分組使用
?
@Test// 排序和分組public void test5() {// 1 獲得sessionSession 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]
?
5.聚集函數查詢總數
@Test// 查詢總數(聚集函數的使用)public void test4() {// 1 獲得sessionSession 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
?
3.Hibernate種的原生SQL查詢(用于處理一些復雜的業務邏輯)
1.基本查詢
1.查詢結構是數組集合
@Test// 基本查詢--返回數組listpublic void test1() {// 1.獲取與線程綁定的sessionSession session = HibernateUtils.getCurrentSession();// 2.開啟事務Transaction tx = session.beginTransaction();String sql = "select * from cst_customer";// 3.構造Query對象進行查詢SQLQuery sqlQuery = session.createSQLQuery(sql);List<Object[]> list = sqlQuery.list();for (Object[] o : list) {System.out.println(Arrays.toString(o));}// 提交事務 tx.commit();}?
結果
[1, 程序員111, null, null, null, null, null, null]
[2, 新增數據, null, null, null, null, null, null]
[3, 測試名稱222, null, null, null, null, null, null]
[4, 測試名稱222, null, null, null, null, null, null]
?
2.查詢結果是對象集合(重要)
@Test// 基本查詢--返回對象listpublic void test2() {// 1.獲取與線程綁定的sessionSession session = HibernateUtils.getCurrentSession();// 2.開啟事務Transaction tx = session.beginTransaction();String sql = "select * from cst_customer";// 3.構造Query對象進行查詢SQLQuery sqlQuery = session.createSQLQuery(sql);sqlQuery.addEntity(Customer.class);List<Customer> list = sqlQuery.list();System.out.println(list);// 提交事務 tx.commit();}?
結果
[Customer [cust_id=1, cust_name=程序員111], Customer [cust_id=2, cust_name=新增數據], Customer [cust_id=3, cust_name=測試名稱222], Customer [cust_id=4, cust_name=測試名稱222]]
?
2.條件查詢
@Test// 基本查詢--返回對象listpublic void test3() {// 1.獲取與線程綁定的sessionSession session = HibernateUtils.getCurrentSession();// 2.開啟事務Transaction tx = session.beginTransaction();String sql = "select * from cst_customer where cust_id=?";// 3.構造Query對象進行查詢SQLQuery sqlQuery = session.createSQLQuery(sql);sqlQuery.addEntity(Customer.class);sqlQuery.setParameter(0, 1l);Customer cus = (Customer) sqlQuery.uniqueResult();System.out.println(cus);// 提交事務 tx.commit();}?
結果
Customer [cust_id=1, cust_name=程序員111]
?
3.聚集函數查詢總數
@Test// 基本查詢--查詢總數public void test4() {// 1.獲取與線程綁定的sessionSession session = HibernateUtils.getCurrentSession();// 2.開啟事務Transaction tx = session.beginTransaction();String sql = "select count(*) from cst_customer";// 3.構造Query對象進行查詢SQLQuery sqlQuery = session.createSQLQuery(sql);BigInteger bg = (BigInteger) sqlQuery.uniqueResult();System.out.println(bg);// 提交事務 tx.commit();}?
結果
4
?
4.排序與分頁綜合查詢
@Test// 基本查詢--分頁查詢public void test5() {// 1.獲取與線程綁定的sessionSession session = HibernateUtils.getCurrentSession();// 2.開啟事務Transaction tx = session.beginTransaction();//按cust_id降序排序String sql = "select * from cst_customer order by cust_id desc limit ?,?";// 3.構造Query對象進行查詢SQLQuery sqlQuery = session.createSQLQuery(sql);sqlQuery.addEntity(Customer.class);/*** 分頁查詢 取第二頁,頁大小是二*/sqlQuery.setParameter(0, 2);sqlQuery.setParameter(1, 2);List<Customer> list = sqlQuery.list();System.out.println(list);// 提交事務 tx.commit();}?
結果
[Customer [cust_id=2, cust_name=新增數據], Customer [cust_id=1, cust_name=程序員111]]
?
4.原生SQL映射為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();}?
結果:
[{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}]
?
5.hibernate原生SQL進行插入操作
@Test// 原生SQL執行插入操作public void test6() {// 1.獲取與線程綁定的sessionSession session = HibernateUtils.getCurrentSession();// 2.開啟事務Transaction tx = session.beginTransaction();String sql = "insert into user values('xxxx','李四',23)";;// 3.構造Query對象進行插入SQLQuery sqlQuery = session.createSQLQuery(sql);sqlQuery.executeUpdate();// 提交事務 tx.commit();}?
轉載于:https://www.cnblogs.com/qlqwjy/p/9357369.html
總結
以上是生活随笔為你收集整理的Hibernate的批量查询的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 借呗到期了还能分期吗
- 下一篇: 城市交通