Hibernate框架学习笔记04

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, 1l);
query.setParameter("cust_id", 2l);
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;)
//内连接
    //隐式内连接 (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
//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(2l));
    //-------------------------------------------
    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
//get方法:立即加载,执行方法时立即发送sql语句查询
public void fun1(){
    Session session = HibernateUtils.openSession();
    Transaction tx = session.beginTransaction();
    //-----------------------------------------
    Customer c = session.get(Customer.class, 1l);
    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.集合策略
<!-- 
lazy:决定是否延迟加载
    true:延迟加载(懒加载):true是默认值
    false:立即加载
    extra:及其懒惰
fetch:决定加载策略:使用什么类型的sql语句加载集合数据
    select:是默认值:单表查询加载集合
    join:使用多表查询加载集合
    subselect:使用子查询加载集合 -->
2.关联属性策略
<!--fetch:决定加载的sql语句
        select 使用单表查询
        join  使用多表查询
    lazy:决定加载时机
        false  立即加载
        proxy  由Customer的类级别加载策略决定-->
3.结论:为了提高效率.fetch的选择上应选择select. lazy的取值应选择 true. 全部使用默认值.
4.no-session问题解决: 扩大session的作用范围.

这里写图片描述

(3)批量抓取
<!-- batch-size="3"
抓取集合的数量为3,抓取客户的集合时,一次抓取几个客户的联系人集合 -->
<set name="linkMans" batch-size="2">
<key column="lkm_cust_id"></key>
<one-to-many class="LinkMan"/>
</set>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值