Hibernate 提供了五种查询方式
通过对象的OID(主键)进行检索。
session.get(Class c,Serializable id);
session.load(Class c,Serializable id);
通过一个已经查询到的对象,获取其关联的对象进行查询。
LInkMan linkMan = session.get(LinkMan.class,1l); Customer customer = linkMan.getCustomer()
Hibernate Query Language,Hibernate查询语言,一种面向对象的查询语言,语法与SQL类似,通过session.createQuery()用于接收一个HQL进行查询。
Session session = HibernateUtiles.getCurrentSession();
Transaction tx = session.beginTransaction();
Query query = session.createQuery("from Customer");
List<Customer> list = query.list();
for(Customer customer : list){
System.out.println(customer);
}
tx.commit();
注意,需要在Cutomer实体类中创建toString方法,但不能两边都toString,即toString时不能有集合。HQL不支持 * 符号号的使用。
Session session = HibernateUtiles.getCurrentSession();
Transaction tx = session.beginTransaction();
Query quer = session.createQuery("select c from Customer c");
List<Customer> list() = query.list;
for(Customer customer : list){
System.out.println(customer);
}
tx.commit();
Session session = HibernateUtiles.getCurrentSession();
Transaction tx = session.beginTransaction();
//默认情况,升序
//List list() = session.createQuery("from Customer order by cust_id").list();
//降序查询(desc) 升序(asc)
List<Customer> list() = session.createQuery("from Customer order by cust_id desc").list();
for(Customer customer : list){
System.out.println(customer);
}
tx.commit();
Session session = HibernateUtiles.getCurrentSession();
Transaction tx = session.beginTransaction();
//按位置绑定:根据参数的位置进行绑定
//单个条件
//Query query = session.createQuery(“from Customer where cust_name=?");
//query.setParamenter(0,"ZS");
//List list = query.list();
//多个条件
//Query query = session.createQuery(“from Customer where cust_name=? and cust_age=?");
//query.setParameter(0,"ZS");
//query.setParameter(1,"男");
//List list = query.list();
//按名称绑定:将需要参数的位置一个名称。
Query query = session.createQuery("from Customer where cust_name= :ZS and age= :bbb ");
query.setParameter("aaa","ZS");
query.setParameter("bbb","男");
List<Customer> list = query.list();
for(Customer customer : list){
System.out.println(customer);
}
tx.commit();
Session session = HibernateUtiles.getCurrentSession();
Transaction tx = session.beginTransaction();
//单个属性
/*
List
//多个属性
/*
session.createQuery("select c.cust_name ,c.cust_age from Customer c").list();
for(Customer customer : list){
System.out.println(Arrays.toString(objects));
}
*/
//查询多个属性,封装到对象当中,那么首先需要在实体类中添加有参构造方法,方法中的参数为这里需的参数
List<Customer> list = session.createQuery("select new Customer(cust_name,cust_surce) from Customer");
for(Customer customer : list){
System.out.println(customer);
}
tx.commit();
Session session = HibernateUtiles.getCurrentSession();
Transaction tx = session.beginTransaction();
//注意,统计查询返回的是一个long类型,uniquerResult表示唯一结果
/*使用聚合函数,这里只用了count(),还可以用max()、min()、avg()、sum()等
Object object = session.createQuery("select count(*) from LinkMan").uniqueResult();
query.setFirstResult(0);
query.setMaxResults(10);
List list = query.list();
System.out.println(object);
*/
//分组统计
List<Object[]> list = session.createQuery("select cust_soure,count(*) from Customer group by cust_source").uniqueResult();
for(Object[] objects : list){
System.out.println(Arrarys.toString(objects));
}
tx.commit();
Session session = HibernateUtiles.getCurrentSession();
Transaction tx = session.beginTransaction();
Query query = session.createQuery("from LinkMan");
query.setFirstResult(0);
query.setMaxResults(10);
List<linkMan> list = query.list();
for(LinkMan linkMan : list){
System.out.println(linkMan);
}
tx.commit();
Query By Criterla,条件查询。是一种更加面向对象化的查询方式。
Session session = HibernateUtiles.getCurrentSession();
Transaction tx = session.beginTransaction();
//获取Criteria的对象
Criteria criteria = session.createCriteria(Customer.class);
//简单查询
/*
List list = criteria.list();
for(Customer customer : list){
System.out.println(customer);
}
*/
//排序查询
/*
//升序
Criteria.addOrder(Order.asc("cust_id"));
//降序
Criteria.addOrder(Order.desc("cust_id"));
listj list = criteria.list();
for(Customer customer : list){
System.out.println(customer);
}
*/
//分页查询
/*
criteria.setFirstResult(0);
criteria.setMaxResults(10);
List list = criteria.list();
list list = criteria.list();
for(LinkMan linkMan: list){
System.out.println(linkMan);
}
*/
//条件查询
/*
//设置条件, == eq >gt >= ge < lt <= le <> ne like in and or
criteria.add(Restrictions.like("cust_name","Z%"));
criteria.add(Restrictions.eq("cust_age","男"))
list list = criteria.list();
for(Customer customer: list){
System.out.println(customer);
}
*/
//统计查询
/*
criteria.setProjection(Projections.rowCount());
Long num = (Long)criteria.uniqueResult();
System.out.println(num);
*/
tx.commit();
可以脱离session使用
//先在web层创建一个DatachedCriteria对象
DatachedCriteria dc = DatachedCriteria.forClass(Customer.css);
//设置条件
dc.add(Restriotions.eq());
//在service层中向dao传入对象 dao.query(dc);
Dao1{
//因为已经在web层设置了条件,所以在dao中不需要拼SQL
xxx(dc){
//绑定session
Criteria criteria = datachedCriteria.getExecutableCriteria(session);
List<> list = criteria.list();
}
}
SQL多表查询
select * from A,B;
select * from A,B where A.id=B.aid;
select * from A inner join B on A.id=B.aid;
select * from A left outer join B on A.id = B.aid
select * from A reghtouter join B on A.id = B.aid
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Customer.class);
detachedCriteria.add(Restrictions.like("
cust_name","W%:));
Session session = HibernateUtils.getCurrentSession();
Transaction transaction = session.beginTransaction();
延迟加载概述
lazy(懒加载)。执行到该行代码时不会发送语句进行查询,当真正使用这个对象的属性时才发送SQl语句进行查询
延迟加载的分类
session.load(Customer.class,1l);
Customer customer = session.get(Customer.class,1l); customer.getLinkMans();
·
通过一个对象抓取到关联对象需要发送SQL语句,SQL语句如何发送,发送成什么样格式通过策略进行配置。
fetch:抓取策略,控制SQL语句格式
lazy:延迟加载,控制查询关联对象的时候是否采用延迟
fetch:抓取策略,控制SQL语句格式
lazy:延迟加载,控制查询关联对象的时候是否采用延迟