HQL是Hibernate Query Language的缩写,提供更加丰富灵活、更为强大的查询能力;HQL更接近SQL语句查询语法。
package test;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import bean.Customer;
import utils.HibernateUtils;
public class Hql {
public static void main(String[] args) {
//普通的oid查询
//test1();
//HQL查询
//1.查询表中所有数据
//test2();
//2.条件查
//test3();
//3.分页查
//test4();
//4.单列查
//test5();
//5.多列查
//test6();
//6.投影查询
//test7();
//7.排序查
//test8();
//8.聚合查
test9();
}
//普通的oid查询
private static void test1() {
Session session = HibernateUtils.openSession();
Transaction tr = session.beginTransaction();
Customer customer = session.get(Customer.class, 9L);
System.out.println(customer.getCust_name());
tr.commit();
session.close();
}
// 全查 类似sql语句的表达式 from 持久化类
private static void test2() {
Session session = HibernateUtils.openSession();
Transaction tr = session.beginTransaction();
Query qr = session.createQuery("from Customer");
List<Customer> list = qr.list();
for (Customer customer : list) {
//返回的是对象
System.out.println(customer.getCust_id()+":"+customer.getCust_name());
}
tr.commit();
session.close();
}
//条件查,类似sql语句的表达式 from 持久化类 where 属性=?
private static void test3() {
Session session = HibernateUtils.openSession();
Transaction tr = session.beginTransaction();
Query qr = session.createQuery("from Customer where cust_name like ?");
qr.setParameter(0, "a%");
List<Customer> list = qr.list();
for (Customer cu : list) {
System.out.println(cu.getCust_name());
}
tr.commit();
session.close();
}
// 分页查 类似sql语句的表达式 from 持久化类 where 属性=?
private static void test4() {
Session session = HibernateUtils.openSession();
Transaction tr = session.beginTransaction();
Query qr = session.createQuery("from Customer");
//mysql: from Customer limit 0,3;
//sqlserver: from Customer top 0,3;
//开始位置
qr.setFirstResult(0);
//结束位置
qr.setMaxResults(3);
List<Customer> list = qr.list();
for (Customer customer : list) {
System.out.println(customer.getCust_id()+":"+customer.getCust_name());
}
tr.commit();
session.close();
}
// 单列查 返回是Object
private static void test5() {
Session session = HibernateUtils.openSession();
Transaction tr = session.beginTransaction();
Query qr = session.createQuery("select cust_id from Customer");
List<Object> list = qr.list();
for (Object object : list) {
System.out.println(object);
}
tr.commit();
session.close();
}
// 多列查 返回的是Object数组
private static void test6() {
Session session = HibernateUtils.openSession();
Transaction tr = session.beginTransaction();
Query qr = session.createQuery("select cust_id,cust_name,cust_phone from Customer");
List<Object[]> list = qr.list();
for (Object[] objects : list) {
System.out.println(objects[0]+"--"+objects[1]+"--"+objects[2]);
}
tr.commit();
session.close();
}
//投影查询
//步骤:
// 1 查多少字段 就需要在持久化类中写多少个构造参数
// 2 语法的书写: select new 持久化类(要查的值1,要查的值2) from 持久化类;
//返回的是对象
private static void test7() {
Session session = HibernateUtils.openSession();
Transaction tr = session.beginTransaction();
Query qr = session.createQuery("select new Customer(cust_id,cust_name,cust_phone) from Customer");
List<Customer> list = qr.list();
for (Customer customer : list) {
System.out.println(customer.getCust_id()+"--"+customer.getCust_name()+"--"+customer.getCust_phone());
}
tr.commit();
session.close();
}
// 排序查
//升序:asc
//降序:desc
private static void test8() {
Session session = HibernateUtils.openSession();
Transaction tr = session.beginTransaction();
Query qr = session.createQuery("from Customer order by cust_id asc");
List<Customer> list = qr.list();
for (Customer customer : list) {
System.out.println(customer.getCust_id()+"--"+customer.getCust_name()+"--"+customer.getCust_phone());
}
tr.commit();
session.close();
}
//聚合查
private static void test9() {
Session session = HibernateUtils.openSession();
Transaction tr = session.beginTransaction();
Query qr = session.createQuery("select count(*) from Customer");
Object object = qr.uniqueResult();
System.out.println(object);
tr.commit();
session.close();
}
}
between and查询
//10.between and查询
private static void test11() {
Session session = HibernateUtils.openSession();
Transaction tr = session.beginTransaction();
Query qr = session.createQuery("select cust_name from Customer where (cust_id between ? and ?)");
qr.setParameter(0, 5L);
qr.setParameter(1, 10L);
List<Object> list = qr.list();
for (Object object : list) {
System.out.println(object);
}
tr.commit();
session.close();
}
多表查询
//连表查询根据linkman的name 查找到customer的信息
@Test
private static void test10() {
Session session = HibernateUtils.openSession();
Transaction tr = session.beginTransaction();
Query qr = session.createQuery("from Customer c,Linkman l where c.cust_id = l.custmoer.cust_id and l.lkm_name = ?");
qr.setParameter(0, "小米21");
//得到的list是object[],数组中的元素是customer对象和linkman对象;
List<Object[]> list = qr.list();
for (int i = 0; i < list.size(); i++) {
Object[] object = (Object[]) list.get(i);
//查询到的是两个持久化类的对象。把object强转为customer对象就可以使用了
Customer customer=(Customer) object[0];
System.out.println(customer.getCust_name());
}
tr.commit();
session.close();
}
多表查询具体信息
//根据linkman的name查询customer中的name和address
private static void test10() {
Session session = HibernateUtils.openSession();
Transaction tr = session.beginTransaction();
Query qr = session.createQuery("select c.cust_address,c.cust_name from Customer c,Linkman l where c.cust_id = l.custmoer.cust_id and l.lkm_name = ?");
qr.setParameter(0, "小米21");
//得到的list是object[],数组中的元素是customer对象和linkman对象;
List<Object[]> list = qr.list();
for (int i = 0; i < list.size(); i++) {
Object[] object = list.get(i);
System.out.println("地址:"+object[0]+"\t姓名:"+object[1]);
}
tr.commit();
session.close();
}