Hibernate使用HQL查询数据库

为了更方便地查询数据库,Hibernate封装了数据库查询语言HQL(Hibernate Query Language),HQL的语法和标准SQL类似,以下做简单的记录。

From

HQL用from表示要查询的数据表,不同于SQL,from可以脱离select单独使用。注意from后接的是要查询的对象所属的Java类,而不是数据库中的表名,例如StudentEntity在数据库中对应的students表,但后面不是跟表名

        //查询所有学生类StudentEntity
        String hql = "from StudentEntity ";
        Query query=session.createQuery(hql);
        //返回结果List
        List<StudentEntity> students=query.list();
        for (StudentEntity s:students)
            System.out.println(s.getName());

Select

如果不指定字段名,from会自动查询对象的所有属性,可以使用select指定查询的对象属性以节省不必要的资源浪费。在查询时可以使用别名来简化类的名称,例如select s.id,s.name from Student Entity as s,其中as可以省略。如果不指定类型,select查询返回的数据默认是Object数组

        //查询指定字段属性
        String hql = "select s.id,s.name from StudentEntity s";
        Query query=session.createQuery(hql);
        //默认返回Object数组
        List<Object[]> students=query.list();
        for (Object[] o:students) {
            System.out.println("id:" + o[0]);
            System.out.println("name:" + o[1]);
        }

可以指定返回的数据类型为list的List,只需要将查询语句修改为select  new list() ....在接收时设置数据类型为List:

        //以List方式查询
        String hql = "select new list (s.id,s.name) from StudentEntity s";
        Query query=session.createQuery(hql);
        //以List类型接收返回数据
        List<List> students=query.list();
        for (List l:students) {
            System.out.println("id:" + l.get(0));
            System.out.println("name:" + l.get(1));
        }

也可以Map形式返回数据,可以为数据起别名,在map中通过别名关键字获取:

        //以Map形式进行查询
        String hql = "select new map (s.id as sid,s.name as sname) from StudentEntity s";
        Query query=session.createQuery(hql);
        //接收Map形式的返回数据
        List<Map> students=query.list();
        for (Map m:students) {
            System.out.println("id:" + m.get("sid"));
            System.out.println("name:" + m.get("sname"));
        }

可以通过类构造方法直接返回指定对象类型的List,这里不只可以使用StudentEntity,可以使用其他任何类型的构造器传入

    //StudentEntity构造方法
    public StudentEntity(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    //以指定构造器类型进行查询
    String hql = "select new StudentEntity (s.id,s.name,s.age) from StudentEntity s";
    Query query=session.createQuery(hql);
    //返回指定类型的List
    List<StudentEntity> students=query.list();
    for (StudentEntity s:students) {
        System.out.println("id:" + s.getId());
        System.out.println("name:" + s.getName());
    }

where

where后可以跟一些条件来限制返回结果的范围

String hql = "from StudentEntity s where s.age > 20";    //比较运算符
String hql = "from StudentEntity s where s.address is null";    //判空
String hql = "from StudentEntity s where s.age in (22,23,24)";    //in特定值
String hql = "from StudentEntity s where s.age between 10 and 20";    //between范围
String hql = "from StudentEntity s where s.name like '张_'";    //模式匹配
String hql = "from StudentEntity s where s.name like '%明%' and s.age>20";    //逻辑运算
String hql = "from CourseEntity c where c.students is empty";    //判断Set集合students是否为空

Query.list()方法会返回结果list,如果知道只有一个结果,可以使用Query.uniqueResult()

        String hql = "from StudentEntity s where s.name ='李四'";
        Query query=session.createQuery(hql);
        //返回单个查询结果
        StudentEntity student=(StudentEntity)query.uniqueResult();
        System.out.println(student.getId());

order by对查询结果进行排序,默认asc升序,desc降序

from StudentEntity s order by s.age desc

distinct过滤掉相同的返回结果值,如下返回学生所选不同的课程course

String hql = "select distinct s.course from StudentEntity s";
发布了124 篇原创文章 · 获赞 65 · 访问量 13万+

猜你喜欢

转载自blog.csdn.net/theVicTory/article/details/104545755