hibernate_4

hibernate_day04

02-查询总结&HQL基础语法详解_

hibernate的检索方式:导航对象图检索方式、OID检索方式、HQL检索方式、QBC检索方式、SQL检索方式

导航对象图检索方式

导航对象图检索方式是根据已经加载的对象,导航到它关联的对象。利用类与类之间的关系来检索。譬如要查找一个联系人对应的客户,就可以由联系人对象自动导航找到联系人所属的客户对象:

@Test

public void test1() {

Session session = HibernateUtils.openSession();

Transaction tx = session.beginTransaction();

//----------------------------------------------------

LinkMan linkMan = session.get(LinkMan.class, 5l);

Customer customer = linkMan.getCustomer();

System.out.println(customer.getCust_name());

//----------------------------------------------------

tx.commit();

session.close();

}

OID检索方式

OID检索方式主要利用session对象的get()load()方法加载某条记录对应的对象,有下面两种方式:

 

@Test

public void test2() {

Session session = HibernateUtils.openSession();

Transaction tx = session.beginTransaction();

//----------------------------------------------------

//LinkMan linkMan = session.get(LinkMan.class, 5l);

LinkMan linkMan = session.load(LinkMan.class, 5l);

System.out.println(linkMan.getLkm_name());

//----------------------------------------------------

tx.commit();

session.close();

}

HQL检索

HQL检索方式基本了解

hibernate query language是面向对象的检索语言,使用的是类、对象、属性的概念,而没有表和字段的概念,具有如下功能:

在查询语句中设定各种查询条件

支持投影查询,即仅检索初对象的部分属性

支持分页查询

支持分组查询,允许使用group by,having关键字

提供内置聚集函数,如sum() min() max()

能够调用用户自定SQL函数。

支持子查询,即嵌套查询

支持动态参数绑定

 

基本用法:

//基本语法

@Test

public void fun1(){

Session session = HibernateUtils.openSession();

Transaction tx = session.beginTransaction();

//----------------------------------------------------

String hql = " from  cn.itcast.domain.Customer ";//完整写法

String hql2 = " from  Customer "; //简单写法

//String hql3 = " from java.lang.Object ";

Query query = session.createQuery(hql);

List list = query.list();

System.out.println(list);

//----------------------------------------------------

tx.commit();

session.close();

}

@Test

//排序

public void fun2(){

Session session = HibernateUtils.openSession();

Transaction tx = session.beginTransaction();

//----------------------------------------------------

String hql1 = " from  cn.itcast.domain.Customer order by cust_id asc ";//完整写法

String hql2 = " from  cn.itcast.domain.Customer order by cust_id desc ";//完整写法

Query query = session.createQuery(hql2);

List list = query.list();

System.out.println(list);

//----------------------------------------------------

tx.commit();

session.close();

}

 

@Test

//条件查询

public void fun3(){

Session session = HibernateUtils.openSession();

Transaction tx = session.beginTransaction();

//----------------------------------------------------

String hql1 = " from  cn.itcast.domain.Customer where cust_id =? ";//完整写法

String hql2 = " from  cn.itcast.domain.Customer where cust_id = :id ";//完整写法

Query query = session.createQuery(hql2);

//query.setParameter(0, 2l);

query.setParameter("id", 2l);

List list = query.list();

System.out.println(list);

//----------------------------------------------------

tx.commit();

session.close();

}

@Test

//分页查询

public void fun4(){

Session session = HibernateUtils.openSession();

Transaction tx = session.beginTransaction();

//----------------------------------------------------

String hql1 = " from  cn.itcast.domain.Customer  ";//完整写法

Query query = session.createQuery(hql1);

//limit ?,?

// (当前页数-1)*每页条数

query.setFirstResult(2);

query.setMaxResults(2);

List list = query.list();

System.out.println(list);

//----------------------------------------------------

tx.commit();

session.close();

}

@Test

//统计查询

//count计数

//sum 求和

//avg平均数

//max

//min

public void fun5(){

Session session = HibernateUtils.openSession();

Transaction tx = session.beginTransaction();

//----------------------------------------------------

String hql1 = " select count(*) from  cn.itcast.domain.Customer  ";//完整写法

String hql2 = " select sum(cust_id) from  cn.itcast.domain.Customer  ";//完整写法

String hql3 = " select avg(cust_id) from  cn.itcast.domain.Customer  ";//完整写法

String hql4 = " select max(cust_id) from  cn.itcast.domain.Customer  ";//完整写法

String hql5 = " select min(cust_id) from  cn.itcast.domain.Customer  ";//完整写法

Query query = session.createQuery(hql5);

Number number  = (Number) query.uniqueResult();

System.out.println(number);

//----------------------------------------------------

tx.commit();

session.close();

}

@Test

//投影查询

public void fun6(){

Session session = HibernateUtils.openSession();

Transaction tx = session.beginTransaction();

//----------------------------------------------------

String hql1 = " select cust_name from  cn.itcast.domain.Customer  ";

String hql2 = " select cust_name,cust_id from  cn.itcast.domain.Customer  ";

String hql3 = " select new Customer(cust_id,cust_name) from  cn.itcast.domain.Customer  ";

Query query = session.createQuery(hql3);

List list = query.list();

System.out.println(list);

//----------------------------------------------------

tx.commit();

session.close();

}

03-HQL多表语法详解_

package cn.itcast.a_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.itcast.domain.Customer;

import cn.itcast.utils.HibernateUtils;

 

//学习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 fun1(){

Session session = HibernateUtils.openSession();

Transaction tx = session.beginTransaction();

//----------------------------------------------------

String hql = " from Customer c inner join c.linkMens ";

Query query = session.createQuery(hql);

List list = query.list();

for(Object[] arr : list){

System.out.println(Arrays.toString(arr));

}

//----------------------------------------------------

tx.commit();

session.close();

}

@Test

//HQL 迫切内连接 =>帮我们进行封装.返回值就是一个对象

public void fun2(){

Session session = HibernateUtils.openSession();

Transaction tx = session.beginTransaction();

//----------------------------------------------------

String hql = " from Customer c inner join fetch c.linkMens ";

Query query = session.createQuery(hql);

List list = query.list();

System.out.println(list);

//----------------------------------------------------

tx.commit();

session.close();

}

@Test

//HQL 左外连接 =>将连接的两端对象分别返回.放到数组中.

public void fun3(){

Session session = HibernateUtils.openSession();

Transaction tx = session.beginTransaction();

//----------------------------------------------------

String hql = " from Customer c left join c.linkMens ";

Query query = session.createQuery(hql);

List list = query.list();

for(Object[] arr : list){

System.out.println(Arrays.toString(arr));

}

//----------------------------------------------------

tx.commit();

session.close();

}

@Test

//HQL 右外连接 =>将连接的两端对象分别返回.放到数组中.

public void fun4(){

Session session = HibernateUtils.openSession();

Transaction tx = session.beginTransaction();

//----------------------------------------------------

String hql = " from Customer c right join c.linkMens ";

Query query = session.createQuery(hql);

List list = query.list();

for(Object[] arr : list){

System.out.println(Arrays.toString(arr));

}

//----------------------------------------------------

tx.commit();

session.close();

}

}

 

04-Criteria语法介绍_

基本查询

package cn.itcast.b_criteria;

 

import java.util.List;

 

import org.hibernate.Criteria;

import org.hibernate.Session;

import org.hibernate.Transaction;

import org.hibernate.criterion.Order;

import org.hibernate.criterion.Projections;

import org.hibernate.criterion.Restrictions;

import org.junit.Test;

 

import cn.itcast.domain.Customer;

import cn.itcast.utils.HibernateUtils;

 

//学习Criteria语法

public class Demo {

@Test

//基本语法

public void fun1(){

Session session = HibernateUtils.openSession();

Transaction tx = session.beginTransaction();

//----------------------------------------------------

Criteria c = session.createCriteria(Customer.class);

List list = c.list();

System.out.println(list);

//----------------------------------------------------

tx.commit();

session.close();

}

@Test

//条件语法

public void fun2(){

Session session = HibernateUtils.openSession();

Transaction tx = session.beginTransaction();

//----------------------------------------------------

Criteria c = session.createCriteria(Customer.class);

// c.add(Restrictions.idEq(2l));

c.add(Restrictions.eq("cust_id",3l));

List list = c.list();

System.out.println(list);

//----------------------------------------------------

tx.commit();

session.close();

}

@Test

//分页语法 -HQL一样

public void fun3(){

Session session = HibernateUtils.openSession();

Transaction tx = session.beginTransaction();

//----------------------------------------------------

Criteria c = session.createCriteria(Customer.class);

//limit ?,?

c.setFirstResult(0);

c.setMaxResults(2);

List list = c.list();

System.out.println(list);

//----------------------------------------------------

tx.commit();

session.close();

}

@Test

//排序语法

public void fun4(){

Session session = HibernateUtils.openSession();

Transaction tx = session.beginTransaction();

//----------------------------------------------------

Criteria c = session.createCriteria(Customer.class);

//c.addOrder(Order.asc("cust_id"));

c.addOrder(Order.desc("cust_id"));

List list = c.list();

System.out.println(list);

//----------------------------------------------------

tx.commit();

session.close();

}

@Test

//统计语法

public void fun5(){

Session session = HibernateUtils.openSession();

Transaction tx = session.beginTransaction();

//----------------------------------------------------

Criteria c = session.createCriteria(Customer.class);

//设置查询目标

c.setProjection(Projections.rowCount());

List list = c.list();

System.out.println(list);

//----------------------------------------------------

tx.commit();

session.close();

}

}

 

05-离线查询对象_

 

package cn.itcast.b_criteria;

 

import java.util.List;

 

import org.hibernate.Criteria;

import org.hibernate.Session;

import org.hibernate.Transaction;

import org.hibernate.criterion.DetachedCriteria;

import org.hibernate.criterion.Order;

import org.hibernate.criterion.Projections;

import org.hibernate.criterion.Restrictions;

import org.junit.Test;

 

import cn.itcast.domain.Customer;

import cn.itcast.utils.HibernateUtils;

 

//学习离线Criteria

public class Demo2 {

@Test

public void fun1(){

//Service/web

DetachedCriteria dc  = DetachedCriteria.forClass(Customer.class);

dc.add(Restrictions.idEq(3l));//拼装条件(全部与普通Criteria一致)

//----------------------------------------------------

Session session = HibernateUtils.openSession();

Transaction tx = session.beginTransaction();

//----------------------------------------------------

Criteria c = dc.getExecutableCriteria(session);

List list = c.list();

System.out.println(list);

//----------------------------------------------------

tx.commit();

session.close();

}

}

 

06-类级别加载策略_

package cn.itcast.c_lazy;

 

import java.util.List;

 

import org.hibernate.Criteria;

import org.hibernate.Session;

import org.hibernate.Transaction;

import org.hibernate.criterion.Order;

import org.hibernate.criterion.Projections;

import org.hibernate.criterion.Restrictions;

import org.junit.Test;

 

import cn.itcast.domain.Customer;

import cn.itcast.utils.HibernateUtils;

 

//懒加载|延迟加载

public class Demo {

@Test

// get方法 :调用立即查询。没有加载策略。立即加载.执行方法时立即发送sql语句查询结果

public void fun1(){

Session session = HibernateUtils.openSession();

Transaction tx = session.beginTransaction();

//----------------------------------------------------

Customer c = session.get(Customer.class, 2l);

System.out.println(c);

//----------------------------------------------------

tx.commit();

session.close();

}

@Test

// load方法(默认):是在执行时,不发送任何sql语句.返回一个对象.使用该对象时,才执行查询.

// 延迟加载:仅仅获得没有使用.不会查询.在使用时才进行查询.

// 是否对类进行延迟加载:可以通过在class元素上配置lazy属性来控制.

//lazy:true  加载时,不查询.使用时才查询b,查询类时会返回代理对象,会在使用属性时,根据关联的session查询数据库,加载数据。

//lazy:false 调用加载时立即查询.使用懒加载确保属性加载数据时,session还是打开的。

public void fun2(){

Session session = HibernateUtils.openSession();

Transaction tx = session.beginTransaction();

//----------------------------------------------------

Customer c = session.load(Customer.class, 3l);

System.out.println(c);

//----------------------------------------------------

tx.commit();

session.close();

}

}

为了提高效率,建议使用延迟加载(懒加载)

07-关联级别策略介绍_

Customer.hbm.xml

xml version="1.0" encoding="UTF-8"?>

DOCTYPE hibernate-mapping PUBLIC 

    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="cn.itcast.domain" >

<class name="Customer" table="cst_customer" lazy="true" >

<id name="cust_id"  >

<generator class="native">generator>

id>

<property name="cust_name" column="cust_name" >property>

<property name="cust_source" column="cust_source" >property>

<property name="cust_industry" column="cust_industry" >property>

<property name="cust_level" column="cust_level" >property>

<property name="cust_linkman" column="cust_linkman" >property>

<property name="cust_phone" column="cust_phone" >property>

<property name="cust_mobile" column="cust_mobile" >property>

 

 

<set name="linkMens" batch-size="3"  >

<key column="lkm_cust_id" >key>

<one-to-many class="LinkMan" />

set>

class>

hibernate-mapping>

你可能感兴趣的:(hibernate_4)