Hibernate的查询方式:
在Hibernate中提供了很多种的查询的方式。Hibernate共提供了五种查询方式。
一、Hibernate的查询方式:OID查询
OID检索:Hibernate根据对象的OID(主键)进行检索
*get方法:Customer customer = session.get(Customer.class,1l);
*使用load方法:Customer customer = session.load(Customer.class,1l);
Hibernate的查询方式:对象导航检索
二、对象导航检索:Hibernate根据一个已经查询到的对象,获得其关联的对象的一种查询方式。
LinkMan linkMan = session.get(LinkMan.class,1l);
Customer customer = linkMan.getCustomer();
或:
Customer customer = session.get(Customer.class,2l);
Set<LinkMan> linkMans = customer.getLinkMans();
三、Hibernate的查询方式:HQL检索
HQL查询:Hibernate Query Language,Hibernate的查询语言,是一种面向对象的方式的查询语言,语法类似SQL。通过session.createQuery(),用于接收一个HQL进行查询方式。
HQL简单查询:
/**
* HQL简单查询
*/
@Test
public void demo2() {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
Query query = session.createQuery("from Customer");
/*sql中支持*的写法:select * from cst_customer;但是在HQL中不支持*的写法
Query query = session.createQuery("select * from Customer");//报错*/
List<Customer> list = query.list();
for (Customer customer : list) {
System.out.println(customer);
}
tx.commit();
}
HQL别名查询:
/**
* HQL别名查询
*/
@Test
public void demo3() {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
Query query = session.createQuery("from Customer c");
//Query query = session.createQuery("select c from Customer c");
List<Customer> list = query.list();
for (Customer customer : list) {
System.out.println(customer);
}
tx.commit();
}
HQL排序查询:
/**
* HQL排序查询
*/
@Test
public void demo4() {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
//默认情况
// List<Customer> list = session.createQuery("from Customer c order by cust_id").list();
//设置降序排序 升序使用asc 降序使用desc
List<Customer> list = session.createQuery("from Customer c order by cust_id desc").list();
for (Customer customer : list) {
System.out.println(customer);
}
tx.commit();
}
HQL条件查询:
/**
* HQL条件查询
*/
@Test
public void demo5() {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
//条件查询:
//一:按位置绑定:根据参数的位置进行绑定
//一个条件:
/*Query query = session.createQuery("from Customer where cust_name=?");
query.setParameter(0, "lisi");*/
//多个条件:
/*Query query = session.createQuery("from Customer where cust_source=? and cust_name like ?");
query.setParameter(0, "123");
query.setParameter(1, "li%");*/
//二、按名称绑定:
Query query = session.createQuery("from Customer where cust_source = :aaa and cust_name like :bbb ");
query.setParameter("aaa", "123");
query.setParameter("bbb", "li%");
List<Customer> list = query.list();
for (Customer customer : list) {
System.out.println(customer);
}
tx.commit();
}
HQL投影查询:
投影查询:查询对象的某个或某些属性
/**
* HQL投影查询
*/
@Test
public void demo6() {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
//单个属性
// Query query = session.createQuery("select c.cust_name from Customer c");
//多个属性
/*Query query = session.createQuery("select c.cust_name,c.cust_source from Customer c");
List<Object []> list = query.list();
for (Object[] objects : list) {
System.out.println(Arrays.toString(objects));
}*/
//查询多个属性,但封装到对象中:
List<Customer> list = session.createQuery("select new Customer(c.cust_name,c.cust_source) from Customer c").list();
for (Customer customer : list) {
System.out.println(customer);
}
tx.commit();
}
HQL分页查询:
/**
* HQL分页查询
*/
@Test
public void demo7() {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
Query query = session.createQuery("from LinkMan");
query.setFirstResult(20);
query.setMaxResults(10);
List<LinkMan> list = query.list();
for (LinkMan linkMan : list) {
System.out.println(linkMan);
}
tx.commit();
}
HQL分组统计查询:
/**
* HQL分组统计
*/
@Test
public void demo8() {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
//聚合函数的使用:count()、sum()、max()、min()、avg()
Object object = session.createQuery("select count(*) from Customer").uniqueResult();
System.out.println(object);
//分组统计
List<Object[]> list = session.createQuery("select cust_source,count(*) from Customer group by cust_source").list();
for (Object[] objects : list) {
System.out.println(Arrays.toString(objects));
}
tx.commit();
}
HQL多表查询:
/**
* HQL多表查询
*/
@Test
public void demo9() {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
//select * from cst_customer c inner join cst_linkman l where c.cust_id=l.lkm_cust_id;
//普通内连接
/*List<Object []> list = session.createQuery("from Customer c inner join c.linkMans").list();
for (Object[] objects : list) {
System.out.println(Arrays.toString(objects));
}*/
//HQL迫切内连接:其实就是在普通内连接inner join后添加一个关键字fetch,from Customer c inner join fetch c.linkMans
List<Customer> list = session.createQuery("from Customer c inner join fetch c.linkMans").list();//通知Hibernate,将另一个对象的数据封装到该对象中
for (Customer customer : list) {
System.out.println(customer);
}
tx.commit();
}
四、Hibernate的查询方式:QBC检索
QBC:Query By Criteria,条件查询,是一种更加面向对象化的查询方式
简单查询:
/**
* 简单查询
*/
@Test
public void demo1() {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
Criteria criteria = session.createCriteria(Customer.class);
List<Customer> list = criteria.list();
for (Customer customer : list) {
System.out.println(customer);
}
tx.commit();
}
排序查询:
/**
* 排序查询
*/
@Test
public void demo2() {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
Criteria criteria = session.createCriteria(Customer.class);
// criteria.addOrder(Order.asc("cust_id"));//升序
criteria.addOrder(Order.desc("cust_id"));//降序
List<Customer> list = criteria.list();
for (Customer customer : list) {
System.out.println(customer);
}
tx.commit();
}
分页查询:
/**
* 分页查询
*/
@Test
public void demo3() {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
Criteria criteria = session.createCriteria(LinkMan.class);
criteria.setFirstResult(10);
criteria.setMaxResults(10);
List<LinkMan> list = criteria.list();
for (LinkMan linkMan : list) {
System.out.println(linkMan);
}
tx.commit();
}
条件查询:
/**
* 条件查询
*/
@Test
public void demo4() {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
Criteria criteria = session.createCriteria(Customer.class);
//设置条件
/*
* = eq
* > gt
* >= ge
* < lt
* <= le
* <> ne(不等于)
* like
* in
* and
* or
*/
criteria.add(Restrictions.like("cust_name", "li%"));
criteria.add(Restrictions.eq("cust_source", "123"));
List<Customer> list = criteria.list();
for (Customer customer : list) {
System.out.println(customer);
}
tx.commit();
}
统计查询:
/**
* 统计查询
*/
@Test
public void demo5() {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
Criteria criteria = session.createCriteria(Customer.class);
criteria.setProjection(Projections.rowCount());
Long num = (Long)criteria.uniqueResult();
System.out.println(num);
tx.commit();
}
离线条件查询(SSH)-- DetachedCriteria
/**
* 离线条件查询
*/
@Test
public void demo6() {
DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Customer.class);
detachedCriteria.add(Restrictions.like("cust_name", "li%"));
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
Criteria criteria = detachedCriteria.getExecutableCriteria(session);
List<Customer> list = criteria.list();
for (Customer customer : list) {
System.out.println(customer);
}
tx.commit();
}
五、Hibernate的查询方式:SQL检索
SQL查询:通过使用sql语句进行查询
/**
* 简单查询
*/
@Test
public void demo1() {
Session session = HibernateUtils.getCurrentSession();
Transaction tx = session.beginTransaction();
/*SQLQuery sqlQuery = session.createSQLQuery("select * from cst_customer");
List<Object []> list = sqlQuery.list();
for (Object[] objects : list) {
System.out.println(Arrays.toString(objects));
}*/
SQLQuery sqlQuery = session.createSQLQuery("select * from cst_customer");
sqlQuery.addEntity(Customer.class);
List<Customer> list = sqlQuery.list();
for (Customer customer : list) {
System.out.println(customer);
}
tx.commit();
}