1.查询总结
(1)oid查询-get
(2)对象属性导航查询
(3)HQL
(4)Criteria
(5)原生sql
2.查询-HQL语法
(1)基础语法
String hql = "from Customer" ;
String hql1 = "from java.lang.Object" ;
Query query = session. createQuery(hql1);
List list = query. list ();
System. out. println(list );
(2)排序
String hql = "from Customer order by cust_id asc" ;
Query query = session. createQuery(hql);
List < Customer> list = query. list ();
System. out. println(list );
(3)条件
String hql = "from Customer where cust_id =?"
String hql1 = "from Customer where cust_id =:cust_id"
Query query = session.createQuery (hql1)
//query.setParameter (0 , 1 l)
query.setParameter ("cust_id" , 2 l)
Customer cus = (Customer) query.uniqueResult ()
System.out .println (cus)
(4)分页
String hql = "from Customer"
Query query = session.createQuery (hql)
query.setFirstResult (0 )
query.setMaxResults (2 )
List<Customer> list = query.list ()
System.out .println (list)
(5)聚合
String hql = "select count (*) from Customer";
String hql1 = " select sum (cust_id) from Customer";
String hql2 = " select avg (cust_id) from Customer";
String hql3 = " select max (cust_id) from Customer";
String hql4 = " select min (cust_id) from Customer";
Query query = session.createQuery(hql4);
Number count = (Number) query.uniqueResult();
System.out.println(count);
(6)投影
String hql = "select new Customer(cust_id,cust_name) from Customer" ;
Query query = session. createQuery(hql);
List list = query. list ();
System. out. println(list );
(7)多表查询
1.sql
//回顾原生SQL
//交叉连接
//内连接
//隐式内连接 (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; )
2.hql
3.hql语法
(1)内连接
public void fun1 (){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
String hql = "from Customer c inner join c.linkMans " ;
Query query = session.createQuery(hql);
List<Object[]> list = query.list();
for (Object[] obj : list) {
System.out .println(Arrays.toString(obj));
}
tx.commit();
session.close();
}
public void fun2(){
Session session = HibernateUtils. openSession();
Transaction tx = session. beginTransaction();
String hql = "from Customer c inner join fetch c.linkMans " ;
Query query = session. createQuery(hql);
List < Customer> list = query. list ();
System. out. println(list );
tx. commit();
session. close();
}
(2)左外连接
String hql = "from Customer c left join c.linkMans " ;
Query query = session.createQuery(hql);
List <Object[]> list = query.list ();
for (Object[] obj : list ) {
System.out.println(Arrays.toString(obj));
}
(3)右外连接
String hql = "from Customer c right join c.linkMans " ;
Query query = session.createQuery(hql);
List <Object[]> list = query.list ();
for (Object[] obj : list ) {
System.out.println(Arrays.toString(obj));
}
3.查询-Criteria语言
(1)语法
1.基本
Criteria criteria = session.createCriteria (Customer.class )
List<Customer> list = criteria.list ()
System.out .println (list)
2.条件
Criteria criteria = session.createCriteria(Customer.class);
//criteria .add(Restrictions.idEq(2l) );
criteria.add(Restrictions.eq("cust_id", 2l) );
List<Customer> list = criteria.list();
System.out.println(list);
3.分页
Criteria criteria = session.createCriteria (Customer.class )
criteria.setFirstResult (0 )
criteria.setMaxResults (2 )
List<Customer> list = criteria.list ()
System.out .println (list)
4.排序
Criteria criteria = session.createCriteria (Customer.class )
//criteria.addOrder (Order.desc ("cust_id" ))
criteria.addOrder (Order.asc ("cust_id" ))
List<Customer> list = criteria.list ()
System.out .println (list)
5.统计
Criteria criteria = session.createCriteria (Customer.class )
criteria.setProjection (Projections.count ("cust_id" ))
Long count = (Long) criteria.uniqueResult ()
System.out .println (count)
(2)离线查询
1.非离线
2.离线
3.演示
@Test
//离线criteria查询
public void fun1(){
//Service/Web层
DetachedCriteria dc = DetachedCriteria.forClass (Customer.class )
dc.add (Restrictions.idEq (2 l))
//-------------------------------------------
Session session = HibernateUtils.openSession ()
Transaction tx = session.beginTransaction ()
//-----------------------------------------
Criteria criteria = dc.getExecutableCriteria (session)
Customer cus = (Customer) criteria.uniqueResult ()
System.out .println (cus)
//-----------------------------------------
tx.commit ()
session.close ()
}
4.查询优化
(1)类级别查询
get方法:没有任何策略.调用即立即查询数据库加载数据.
@Test
public void fun1 (){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
Customer c = session.get (Customer.class, 1 l);
System.out .println(c);
tx.commit();
session.close();
}
load方法: 应用类级别的加载策略
lazy(默认值):true, 查询类时,会返回代理对象.会在使用属性时,根据关联的session查询数据库.加载数据.
lazy:false. load方法会与get方法没有任何区别.调用时即加载数据.
结论:为了提高效率.建议使用延迟加载(懒加载)
注意:使用懒加载时要确保,调用属性加载数据时,session还是打开的.不然会抛出异常
<class name="Customer" table="cst_customer" lazy ="true" >
(2)关联级别查询
1.集合策略
2.关联属性策略
3.结论:为了提高效率.fetch的选择上应选择select. lazy的取值应选择 true. 全部使用默认值.
4.no-session问题解决: 扩大session的作用范围.
(3)批量抓取
<set name ="linkMans" batch-size ="2" >
<key column ="lkm_cust_id" > </key >
<one-to-many class ="LinkMan" />
</set >