hibernate hql语句

1.什么是hql
HQL是Hibernate Query Language的缩写,提供更加丰富灵活、更为强大的查询能力;HQL更接近SQL语句查询语法。

  1. hql和sql区别/异同
    HQL SQL
    操作的是类名/属性 操作的是表名/列名
    语句区分大小写,关键字不区分大小写 语句不区分大小写
    ?,从下标0开始计算位置(hibernate5之后不支持) ?,从顺序1开始计算位置
    :命名参数 不支持:命名参数
    面向对象的查询语言 面向结构查询语言
  2. 处理返回的结果集
    3.1返回单个对象
    /**
    • hql语句结果集处理情况1
    • 返回单个对象
      */
      public static void test1(){
      Session session = SessionFactoryUtils.openSession();
      Transaction transaction = session.beginTransaction();
      String hql=“from Student where sid=1”;
      Query query = session.createQuery(hql);
      Object obj = query.getSingleResult();
      System.out.println(obj.getClass().getName());
      System.out.println(obj);
      transaction.commit();
      SessionFactoryUtils.closeSession();
      }
      结果:

3.1.2返回多个对象
/**
* hql语句结果集处理情况1
* 返回多个对象 注意数据库字段不能为null
*/
public static void test2(){
Session session = SessionFactoryUtils.openSession();
Transaction transaction = session.beginTransaction();
String hql=“from Student”;
Query query = session.createQuery(hql);
List list=query.list();
for (Object o : list) {
System.out.println(o);
}
transaction.commit();
SessionFactoryUtils.closeSession();
}
结果:

3.2返回字符串

/**
* hql语句结果集处理情况2
* 返回字符串
*/
public static void test3(){
Session session = SessionFactoryUtils.openSession();
Transaction transaction = session.beginTransaction();
String hql=“select sname from Student where sid=1”;
Query query = session.createQuery(hql);
Object obj=query.getSingleResult();
System.out.println(obj);
transaction.commit();
SessionFactoryUtils.closeSession();
}
结果:

3.3返回数组
/**
* hql语句结果集处理情况3
* 返回数组
*/
public static void test4(){
Session session = SessionFactoryUtils.openSession();
Transaction transaction = session.beginTransaction();
String hql=“select sid,sname,version from Student where sid=1”;
Query query = session.createQuery(hql);
Object obj=query.getSingleResult();
System.out.println(Arrays.toString((Object[])obj));
transaction.commit();
SessionFactoryUtils.closeSession();
}
结果:

3.4返回Map
/**
* hql语句结果集处理情况4
* 返回Map
*/
public static void test5(){
Session session = SessionFactoryUtils.openSession();
Transaction transaction = session.beginTransaction();
String hql=“select new map(sid as sid,sname as sname,version as version) from Student where sid=1”;
Query query = session.createQuery(hql);
Object obj=query.getSingleResult();
System.out.println(obj);
transaction.commit();
SessionFactoryUtils.closeSession();
}
结果:

3.5查询多列返回对象(注意对象的构造方法)

/**
* hql语句结果集处理情况5
* 查询多列返回对象
*/
public static void test6(){
Session session = SessionFactoryUtils.openSession();
Transaction transaction = session.beginTransaction();
String hql=“select new Student (sid,sname)from Student where sid=1”;
Query query = session.createQuery(hql);
Object obj=query.getSingleResult();
System.out.println(obj.getClass().getName());
System.out.println(obj);
transaction.commit();
SessionFactoryUtils.closeSession();
}
结果:

  1. hql中使用占位符
    4.1 ?占位符
    从下标0开始计算位置hibernate5之后不再支持?占位符所以不为大家介绍了

4.2 :命名参数
/**
* hql命名参数
*/
public static void test7(){
Session session = SessionFactoryUtils.openSession();
Transaction transaction = session.beginTransaction();
String hql=“from Student where sid in :ids”;
Query query = session.createQuery(hql);
//这里使用 query.setParameter(0,1)报错
// query.setParameter(“id”,1);//这个是传单个参数
// 如果使用in的话
List ids=new ArrayList();
ids.add(1);
ids.add(2);
query.setParameterList(“ids”,ids);
Object obj = query.getSingleResult();
System.out.println(obj.getClass().getName());
System.out.println(obj);
transaction.commit();
SessionFactoryUtils.closeSession();
}
5.连接查询
public static void test8(){
Session session = SessionFactoryUtils.openSession();
Transaction transaction = session.beginTransaction();
String hql="select new map(oi.order_item_id,o.order_id) from Order o,OrderItem oi where o.order_id=oi.oid ";
Query query = session.createQuery(hql);
List list=query.list();
for (Object o : list) {
System.out.println(o);
}
transaction.commit();
SessionFactoryUtils.closeSession();
}
结果:

  1. 聚合函数
    这里就给大家使用一个count()函数其余的sum,avg,min,max用法都一样

/**
* 聚合函数
*/
public static void test9(){
Session session = SessionFactoryUtils.openSession();
Transaction transaction = session.beginTransaction();
String hql="select count(sid)from Student ";
Query query = session.createQuery(hql);
Object singleResult = query.getSingleResult();
System.out.println(singleResult);
transaction.commit();
SessionFactoryUtils.closeSession();
}
7.hql分页

/**
* hql分页
* 查询第二页每页三条
*/
public static void test10() {
Session session = SessionFactoryUtils.openSession();
Transaction transaction = session.beginTransaction();
String hql = "from Book ";
Query query = session.createQuery(hql);
int page = 2;
int offeset = 3;
query.setFirstResult(page * offeset);
query.setMaxResults((page - 1) * offeset);
List list = query.list();
for (Object o : list) {
System.out.println(o);
}
transaction.commit();
SessionFactoryUtils.closeSession();
}


作者:yjt520557
来源:CSDN
原文:https://blog.csdn.net/yjt520557/article/details/84571348
版权声明:本文为博主原创文章,转载请附上博文链接!1.什么是hql
HQL是Hibernate Query Language的缩写,提供更加丰富灵活、更为强大的查询能力;HQL更接近SQL语句查询语法。

  1. hql和sql区别/异同
    HQL SQL
    操作的是类名/属性 操作的是表名/列名
    语句区分大小写,关键字不区分大小写 语句不区分大小写
    ?,从下标0开始计算位置(hibernate5之后不支持) ?,从顺序1开始计算位置
    :命名参数 不支持:命名参数
    面向对象的查询语言 面向结构查询语言
  2. 处理返回的结果集
    3.1返回单个对象
    /**
    • hql语句结果集处理情况1
    • 返回单个对象
      */
      public static void test1(){
      Session session = SessionFactoryUtils.openSession();
      Transaction transaction = session.beginTransaction();
      String hql=“from Student where sid=1”;
      Query query = session.createQuery(hql);
      Object obj = query.getSingleResult();
      System.out.println(obj.getClass().getName());
      System.out.println(obj);
      transaction.commit();
      SessionFactoryUtils.closeSession();
      }
      结果:

3.1.2返回多个对象
/**
* hql语句结果集处理情况1
* 返回多个对象 注意数据库字段不能为null
*/
public static void test2(){
Session session = SessionFactoryUtils.openSession();
Transaction transaction = session.beginTransaction();
String hql=“from Student”;
Query query = session.createQuery(hql);
List list=query.list();
for (Object o : list) {
System.out.println(o);
}
transaction.commit();
SessionFactoryUtils.closeSession();
}
结果:

3.2返回字符串

/**
* hql语句结果集处理情况2
* 返回字符串
*/
public static void test3(){
Session session = SessionFactoryUtils.openSession();
Transaction transaction = session.beginTransaction();
String hql=“select sname from Student where sid=1”;
Query query = session.createQuery(hql);
Object obj=query.getSingleResult();
System.out.println(obj);
transaction.commit();
SessionFactoryUtils.closeSession();
}
结果:

3.3返回数组
/**
* hql语句结果集处理情况3
* 返回数组
*/
public static void test4(){
Session session = SessionFactoryUtils.openSession();
Transaction transaction = session.beginTransaction();
String hql=“select sid,sname,version from Student where sid=1”;
Query query = session.createQuery(hql);
Object obj=query.getSingleResult();
System.out.println(Arrays.toString((Object[])obj));
transaction.commit();
SessionFactoryUtils.closeSession();
}
结果:

3.4返回Map
/**
* hql语句结果集处理情况4
* 返回Map
*/
public static void test5(){
Session session = SessionFactoryUtils.openSession();
Transaction transaction = session.beginTransaction();
String hql=“select new map(sid as sid,sname as sname,version as version) from Student where sid=1”;
Query query = session.createQuery(hql);
Object obj=query.getSingleResult();
System.out.println(obj);
transaction.commit();
SessionFactoryUtils.closeSession();
}
结果:

3.5查询多列返回对象(注意对象的构造方法)

/**
* hql语句结果集处理情况5
* 查询多列返回对象
*/
public static void test6(){
Session session = SessionFactoryUtils.openSession();
Transaction transaction = session.beginTransaction();
String hql=“select new Student (sid,sname)from Student where sid=1”;
Query query = session.createQuery(hql);
Object obj=query.getSingleResult();
System.out.println(obj.getClass().getName());
System.out.println(obj);
transaction.commit();
SessionFactoryUtils.closeSession();
}
结果:

  1. hql中使用占位符
    4.1 ?占位符
    从下标0开始计算位置hibernate5之后不再支持?占位符所以不为大家介绍了

4.2 :命名参数
/**
* hql命名参数
*/
public static void test7(){
Session session = SessionFactoryUtils.openSession();
Transaction transaction = session.beginTransaction();
String hql=“from Student where sid in :ids”;
Query query = session.createQuery(hql);
//这里使用 query.setParameter(0,1)报错
// query.setParameter(“id”,1);//这个是传单个参数
// 如果使用in的话
List ids=new ArrayList();
ids.add(1);
ids.add(2);
query.setParameterList(“ids”,ids);
Object obj = query.getSingleResult();
System.out.println(obj.getClass().getName());
System.out.println(obj);
transaction.commit();
SessionFactoryUtils.closeSession();
}
5.连接查询
public static void test8(){
Session session = SessionFactoryUtils.openSession();
Transaction transaction = session.beginTransaction();
String hql="select new map(oi.order_item_id,o.order_id) from Order o,OrderItem oi where o.order_id=oi.oid ";
Query query = session.createQuery(hql);
List list=query.list();
for (Object o : list) {
System.out.println(o);
}
transaction.commit();
SessionFactoryUtils.closeSession();
}
结果:

  1. 聚合函数
    这里就给大家使用一个count()函数其余的sum,avg,min,max用法都一样

/**
* 聚合函数
*/
public static void test9(){
Session session = SessionFactoryUtils.openSession();
Transaction transaction = session.beginTransaction();
String hql="select count(sid)from Student ";
Query query = session.createQuery(hql);
Object singleResult = query.getSingleResult();
System.out.println(singleResult);
transaction.commit();
SessionFactoryUtils.closeSession();
}
7.hql分页

/**
* hql分页
* 查询第二页每页三条
*/
public static void test10() {
Session session = SessionFactoryUtils.openSession();
Transaction transaction = session.beginTransaction();
String hql = "from Book ";
Query query = session.createQuery(hql);
int page = 2;
int offeset = 3;
query.setFirstResult(page * offeset);
query.setMaxResults((page - 1) * offeset);
List list = query.list();
for (Object o : list) {
System.out.println(o);
}
transaction.commit();
SessionFactoryUtils.closeSession();
}

发布了12 篇原创文章 · 获赞 14 · 访问量 2万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览