本文主要是介绍hibernate中的HQL查询,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
HQL(Hibernate Query Language)Hibernate 自定义的面向对象的数据库查询语言:
查询语句中的关键词都是类的属性名和类名而不是数据库中的表名和列名
比较适应不复杂的多表查询
public void test(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
String hql = " from Customer "; //Customer类如果唯一可以简写
Query query = session.createQuery(hql);
List<Customer> list = query.list();
System.out.println(list);
tx.commit();
session.close();
}
1. HQL普通条件查询
//Customer类如果在配置文件唯一可以简写Customer 而不带包名
String hql = " from Customer where cust_id = 1 ";
Query query = session.createQuery(hql);
Customer c = (Customer) query.uniqueResult();
2.HQL问号占位符 条件查询
String hql = " from Customer where cust_id = ? "; // 所有Customer对象
Query query = session.createQuery(hql);
query.setParameter(0, 1l);//query.setLong(0, 1l);第一个参数表位置,第二表ID
Customer c = (Customer) query.uniqueResult();
3.HQL命名占位符 条件查询
String hql = " from Customer where cust_id = :cust_id ";
Query query = session.createQuery(hql);
query.setParameter("cust_id", 1l);//第一个参数在hql语句中定义
Customer c = (Customer) query.uniqueResult();
4.HQL分页查询
String hql = " from Customer ";
Query query = session.createQuery(hql);
query.setFirstResult(4);//从0开始
query.setMaxResults(2);//最大数据条数
List<Customer> list = query.list();
HQL排序查询
String hql1 = " from com.example.domain.Customer order by cust_id asc ";//升序
String hql2 = " from com.example.domain.Customer order by cust_id desc ";//降序
Query query = session.createQuery(hql2);
List list = query.list();
HQL统计查询
String hql1 = " select count(*) from com.example.domain.Customer";
String hql2 = " select sum(cust_id) from com.example.domain.Customer";
String hql3 = " select avg(cust_id) from com.example.domain.Customer";
String hql4 = " select max(cust_id) from com.example.domain.Customer";
String hql5 = " select min(cust_id) from com.example.domain.Customer";
Query query = session.createQuery(hql5);
Number number = (Number) query.uniqueResult();
System.out.println(number);
//HQL统计查询投影查询:投影查询:只查部分字段
String hql1 = " select cust_name from Customer";
String hql2 = " select cust_name,cust_id from Customer";
String hql3 = " select new Customer(cust_id,cust_name) from Customer";
Query query = session.createQuery(hql3);
List list = query.list();
HQL内连接
/* 内连接:取交集,把符合链接的两行链接起来
* 例如A表,B表 内连接
* 1.把符合条件的A表中的一行和B表中的一行链接起来返回
* 2.如果一张表中有多行数据符合条件,且这多行数据只对应另一张表中的一行,
* 则会给这多行数据的每一行都复制对应的一行来链接
* 3.返回的总行数是小于等于两表中行数最多的一行 */
public void fun1(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//sql:SELECT * FROM cst_customer c,cst_linkman l WHERE c.cust_id=l.lkm_cust_id
//:SELECT * FROM cst_customer c JOIN cst_linkman l WHERE c.cust_id=l.lkm_cust_id
//:SELECT * FROM cst_customer c JOIN cst_linkman l ON c.cust_id=l.lkm_cust_id
//:SELECT * FROM cst_customer c CROSS JOIN cst_linkman l WHERE c.cust_id=l.lkm_cust_id
//:SELECT * FROM cst_customer c CROSS JOIN cst_linkman l on c.cust_id=l.lkm_cust_id
String hql = " from Customer c inner join c.linkMens ";
Query query = session.createQuery(hql);
List<Object[]> list = query.list();
for(Object[] arr : list){
System.out.println(Arrays.toString(arr));
}
tx.commit();
session.close();
}
迫切HQL内连接HQL特有:封装成Customer对象
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<Customer> list = query.list();
System.out.println(list);
tx.commit();
session.close();
}
HQL左外链接
/***
*1.以左边表为基表,先把基表数据全部查询出来。
*2.如果链接的表中有符合条件的数据,就把符合条件的这行数据全部链接到基表对应的那一行
*3.如果链接表中有多行数据符合条件,而且多行数据只对应基表的一行,就会给链接表的每行符合条件的数据复制一行基表对应的数据来链接
*所以最后查询的结果行数,大于等于基表行数 */
public void fun3(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//sql:SELECT * FROM cst_customer c LEFT JOIN cst_linkman l ON c.cust_id=l.lkm_cust_id
String hql = " from Customer c left join c.linkMens ";
Query query = session.createQuery(hql);
List<Object[]> list = query.list();
for(Object[] arr : list){
System.out.println(Arrays.toString(arr));
}
tx.commit();
session.close();
}
HQL右外链接
/**
* 右外连接 :和左外链镜像:即基表和链接表互换了位置,其他不变
*1.以右边表为基表,先把基表数据全部查询出来。
*2.如果链接的表中有符合条件的数据,就把符合条件的这行数据全部链接到基表对应的那一行
*3.如果链接表中有多行数据符合条件,而且多行数据只对应基表的一行,就会给链接表的每行符合条件的数据复制一行基表对应的数据来链接
*4.所以最后查询的结果行数,大于等于基表行数 */
public void fun4(){
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();
//该句相当于sql:SELECT * FROM cst_customer c RIGHT JOIN cst_linkman l ON c.cust_id=l.lkm_cust_id
String hql = " from Customer c right join c.linkMens ";
Query query = session.createQuery(hql);
List<Object[]> list = query.list();
for(Object[] arr : list){
System.out.println(Arrays.toString(arr));
}
tx.commit();
session.close();
}
这篇关于hibernate中的HQL查询的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!