Hibernate uses HQL to query the database

In order to query the database more conveniently, Hibernate encapsulates the database query language HQL (Hibernate Query Language). The syntax of HQL is similar to standard SQL. The following is a simple record.

From

HQL uses from to indicate the data table to be queried. Unlike SQL, from can be used separately from select. Note that from is followed by the Java class to which the object to be queried belongs, not the table name in the database, for example, StudentEntity corresponds to the students table in the database, but it is not followed by the table name.

        //查询所有学生类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

If you do not specify the field name, from will automatically query all the properties of the object, you can use select to specify the query object properties to save unnecessary waste of resources. Aliases can be used to simplify the name of the class when querying, such as select s.id, s.name from Student Entity as s, where as can be omitted. If the type is not specified, the data returned by the select query defaults to the Object array

        //查询指定字段属性
        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]);
        }

You can specify that the returned data type is List. You only need to modify the query statement to select new list () .... Set the data type to List when receiving:

        //以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));
        }

You can also return data in the form of a map, you can alias the data, and get it through the alias keyword in the 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"));
        }

You can directly return the List of the specified object type through the class construction method. Here, you can use not only StudentEntity, but also any other type of constructor.

    //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 can be followed by some conditions to limit the scope of the returned result

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是否为空

The Query.list () method will return a list of results. If you know there is only one result, you can use 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 to sort the query results, default asc ascending, desc descending

from StudentEntity s order by s.age desc

distinct filters out the same return result value, and returns the different courses selected by the student as follows

String hql = "select distinct s.course from StudentEntity s";

 

Published 124 original articles · Like 65 · Visit 130,000+

Guess you like

Origin blog.csdn.net/theVicTory/article/details/104545755