@Testjava public void demo01(){mysql //1 查詢全部web Session session = factory.openSession();算法 session.beginTransaction();sql
//1 使用簡單類名 , 存在自動導包數組 // * Customer.hbm.xml <hibernate-mapping auto-import="true">session // Query query = session.createQuery("from Customer");app //2 使用全限定類名ide Query query = session.createQuery("from com.hibernate.a_init.Customer");函數
List<Customer> allCustomer = query.list(); for (Customer customer : allCustomer) { System.out.println(customer); }
session.getTransaction().commit(); session.close(); } |
1.3 選擇查詢
@Test public void demo02(){ //2 簡單條件查詢 Session session = factory.openSession(); session.beginTransaction();
//1 指定數據,cid OID名稱 // Query query = session.createQuery("from Customer where cid = 1"); //2 若是使用id,也能夠(瞭解) // Query query = session.createQuery("from Customer where id = 1"); //3 對象別名 ,格式: 類 [as] 別名 // Query query = session.createQuery("from Customer as c where c.cid = 1"); //4 查詢全部項,mysql--> select * from... Query query = session.createQuery("select c from Customer as c where c.cid = 1");
Customer customer = (Customer) query.uniqueResult(); System.out.println(customer);
session.getTransaction().commit(); session.close(); } |
@Test public void demo04(){ //4 投影 Session session = factory.openSession(); session.beginTransaction();
//1 默認 //若是單列 ,select c.cname from,須要List<Object> //若是多列,select c.cid,c.cname from ,須要List<Object[]> ,list存放每行,Object[]多列 // Query query = session.createQuery("select c.cid,c.cname from Customer c"); //2 將查詢部分數據,設置Customer對象中 // * 格式:new Customer(c.cid,c.cname) // * 注意:Customer必須提供相應的構造方法。 // * 若是投影使用oid,結果脫管態對象。 Query query = session.createQuery("select new Customer(c.cid,c.cname) from Customer c");
List<Customer> allCustomer = query.list(); for (Customer customer : allCustomer) { System.out.println(customer.getCid() + " : " + customer.getOrderSet().size()); }
session.getTransaction().commit(); session.close(); } |
@Test public void demo03(){ //3排序 ,mysql--> select... order by 字段 [asc]|desc ,.... Session session = factory.openSession(); session.beginTransaction();
Query query = session.createQuery("from Customer order by cid desc");
List<Customer> allCustomer = query.list(); for (Customer customer : allCustomer) { System.out.println(customer.getCid()); }
session.getTransaction().commit(); session.close(); } |
@Test public void demo05(){ //分頁 Session session = factory.openSession(); session.beginTransaction();
Query query = session.createQuery("from Customer"); // * 開始索引 , startIndex 算法: startIndex = (pageNum - 1) * pageSize; // *** pageNum 當前頁(以前的 pageCode) query.setFirstResult(0); // * 每頁顯示個數 , pageSize query.setMaxResults(2);
List<Customer> allCustomer = query.list(); for (Customer customer : allCustomer) { System.out.println(customer.getCid()); }
session.getTransaction().commit(); session.close(); } |
@Test public void demo06(){ /* 6 綁定參數 * 方式1:佔位符,使用? 在hql語句替換具體參數 * 設置參數 query.setXxx(int , object) * 參數1:?位置,從0開始。 * 參數2:實際參數 * 例如:String --> query.setString(int,String) * 方式2:別名 , 格式 「屬性= :別名 」 * 設置參數 query.setXxx(String,object) * 參數1:別名 * 參數2:實際參數 * 例如:Integer --> query.setInteger(String,Integer) * 提供公共設置方法 * setParameter(int|string , Object) */ Session session = factory.openSession(); session.beginTransaction();
Integer cid = 1;
//方式1 // Query query = session.createQuery("from Customer where cid = ?"); // query.setInteger(0, cid); //方式2 Query query = session.createQuery("from Customer where cid = :xxx"); // query.setInteger("xxx", cid); query.setParameter("xxx", cid);
Customer customer = (Customer) query.uniqueResult(); System.out.println(customer);
session.getTransaction().commit(); session.close(); } |
@Test public void demo07(){ /* 7 聚合函數 */ Session session = factory.openSession(); session.beginTransaction();
//1 // Query query = session.createQuery("select count(*) from Customer"); //2 別名 // Query query = session.createQuery("select count(c) from Customer c"); //3 oid Query query = session.createQuery("select count(cid) from Customer");
Long numLong = (Long) query.uniqueResult(); int num = numLong.intValue();
System.out.println(num);
session.getTransaction().commit(); session.close(); } |
1.交叉鏈接 ,等效 sql 笛卡爾積
2.隱式內鏈接,等效 sql 隱式內鏈接
3.內鏈接,等效sql內鏈接
4.迫切內鏈接,hibernate底層使用 內鏈接。
5.左外鏈接,等效sql左外鏈接
6.迫切左外鏈接,hibernate底層使用 左外鏈接
7.右外鏈接,等效sql右外鏈接
內鏈接和迫切內鏈接?
左外鏈接和迫切左外鏈接?
@Test public void demo08(){ /* 8 連接查詢 : 左外鏈接和迫切左外鏈接? * * 左外鏈接 , left outer join * 底層使用sql的左外鏈接,hibernate進行數據自動封裝,將一條記錄,封裝給兩個對象(Customer,Order) * 將兩個對象添加到一個對象數組中Object[Customer,Order] * * 迫切左外連接 left outer join fetch * 底層使用sql的左外鏈接,hibernate將一條記錄封裝給Customer,講order數據封裝Order,並將order關聯到Customer * customer.getOrderSet().add(order) * 默認查詢的數據重複 */ Session session = factory.openSession(); session.beginTransaction();
//左外鏈接 // List list = session.createQuery("from Customer c left outer join c.orderSet ").list(); //迫切左外連接 (默認數據重複) // List list = session.createQuery("from Customer c left outer join fetch c.orderSet ").list(); //迫切左外連接 (去重複) List list = session.createQuery("select distinct c from Customer c left outer join fetch c.orderSet ").list();
session.getTransaction().commit(); session.close(); } |
l 思想:將HQL從java源碼中,提取到配置文件中。
l 分類:全局、佈局
l 配置
全局:*.hbm.xml <class></class><query name="名稱">HQL語句
局部: <class name="" table=""><id><property> <query name="">HQL</class>
l 得到
全局:
session.getNamedQuery("queryName")
局部:
session.getNamedQuery("className.queryName") 須要使用類的全限定名稱
@Test public void demo09(){ /* 9 命名查詢 */ Session session = factory.openSession(); session.beginTransaction();
//全局 //List list = session.getNamedQuery("findAll").list(); //局部 List list = session.getNamedQuery("com.hibernate.a_init.Customer.findAll").list();
System.out.println(list.size());
session.getTransaction().commit(); session.close(); } |
QBC:Query By Criteria條件查詢.面向對象的查詢的方式.
// 簡單查詢:
List<Customer> list = session.createCriteria(Customer.class).list();
for (Customer customer : list) {
System.out.println(customer);
}
Criteria criteria = session.createCriteria(Order.class);
criteria.setFirstResult(10);
criteria.setMaxResults(10);
List<Order> list = criteria.list();
Criteria criteria = session.createCriteria(Customer.class);
// criteria.addOrder(org.hibernate.criterion.Order.asc("age"));
criteria.addOrder(org.hibernate.criterion.Order.desc("age"));
List<Customer> list = criteria.list();
// 按名稱查詢:
/*Criteria criteria = session.createCriteria(Customer.class);
criteria.add(Restrictions.eq("cname", "tom"));
List<Customer> list = criteria.list();*/
// 模糊查詢;
/*Criteria criteria = session.createCriteria(Customer.class);
criteria.add(Restrictions.like("cname", "t%"));
List<Customer> list = criteria.list();*/
// 條件並列查詢
Criteria criteria = session.createCriteria(Customer.class);
criteria.add(Restrictions.like("cname", "t%"));
criteria.add(Restrictions.ge("age", 35));
List<Customer> list = criteria.list();
l DetachedCriteria 離線查詢對象,不須要使用Session就能夠拼湊查詢條件。通常使用在web層或service層拼湊。將此對象傳遞給dao層,此時將與session進行綁定執行查詢。
l 離線查詢條件與QBC同樣的。
@Test public void demo10(){ /* 10 離線查詢 */
//web & service DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Customer.class); detachedCriteria.add(Restrictions.eq("cid", 1));
//---------------dao
Session session = factory.openSession(); session.beginTransaction();
// 離線Criteria 與session綁定 Criteria criteria = detachedCriteria.getExecutableCriteria(session); List<Customer> allCustomer = criteria.list(); System.out.println(allCustomer.size());
session.getTransaction().commit(); session.close(); } |
l 整合c3p0
步驟一:導入c3p0 jar包
步驟二:hibernate.cfg.xml 配置
hibernate.connection.provider_class org.hibernate.connection.C3P0ConnectionProvider
l c3p0具體配置參數
###########################
### C3P0 Connection Pool###
###########################
#hibernate.c3p0.max_size 2
#hibernate.c3p0.min_size 2
#hibernate.c3p0.timeout 5000
#hibernate.c3p0.max_statements 100
#hibernate.c3p0.idle_test_period 3000
#hibernate.c3p0.acquire_increment 2
#hibernate.c3p0.validate false