Hibernate 框架 -HQL 语法

  HQL ( Hibernate Query Language ) 查询语言是面向对象的查询语言,也是在 Hibernate 中最常见的。其语法和 SQL 语法有一些相似,功能十分强大,几乎支持除特殊 SQL 扩展外的所有查询功能。此种查询方式为 Hibernate 官方推荐的标准查询方式。

  以下我直接使用上一篇文章配置好的持久化类和工具类来写。

  这篇文章我多以代码的形式来说明 HQL 语法的使用。

1、基本语法查询

// 基本语法

    @Test
    public void fun1() {
        Session session = HibernateUtils.openSession();
        Transaction tx = session.beginTransaction();
        // -----------------------------------------------------
        String hql = "from Customer";
        Query query = session.createQuery(hql);
        List<Customer> list = query.list();
        System.out.println(list);

        // -----------------------------------------------------
        tx.commit();
        session.close();
    }

2、条件查询

// 条件查询

    @Test
    public void fun2() {
        Session session = HibernateUtils.openSession();
        Transaction tx = session.beginTransaction();
        // -----------------------------------------------------
        String hql = "from Customer where cust_id = ?";
        String hql1 = "from Customer where cust_id = :id";//另外一种查询语句写法
        Query query = session.createQuery(hql1);
        // query.setParameter(0, 2l);
        query.setParameter("id", 2l);
        List<Customer> list = query.list();
        System.out.println(list);

        // -----------------------------------------------------
        tx.commit();
        session.close();
    }

3、排序查询

// 排序

    @Test
    public void fun3() {
        Session session = HibernateUtils.openSession();
        Transaction tx = session.beginTransaction();
        // -----------------------------------------------------
        String hql = "from Customer order by cust_id asc";// asc:升序 desc:降序
        Query query = session.createQuery(hql);
        List<Customer> list = query.list();
        System.out.println(list);

        // -----------------------------------------------------
        tx.commit();
        session.close();
    }

4、分页查询

// 分页查询

    @Test
    public void fun4() {
        Session session = HibernateUtils.openSession();
        Transaction tx = session.beginTransaction();
        // -----------------------------------------------------
        String hql = "from Customer";
        Query query = session.createQuery(hql);
        // 开始的下标=(当前页数-1)*每页条数
        query.setFirstResult(0);
        query.setMaxResults(3);
        List<Customer> list = query.list();
        System.out.println(list);

        // -----------------------------------------------------
        tx.commit();
        session.close();
    }

5、聚合函数查询

// 统计查询
    // count
    // sum
    // avg
    // max
    // min

    @Test
    public void fun5() {
        Session session = HibernateUtils.openSession();
        Transaction tx = session.beginTransaction();
        // -----------------------------------------------------
        String hql1 = "select count(*) from Customer"; // 统计条数
        String hql2 = "select sum(cust_id) from Customer"; // 求和
        String hql3 = "select avg(cust_id) from Customer"; // 求平均值
        String hql4 = "select max(cust_id) from Customer"; // 求最大值
        String hql5 = "select min(cust_id) from Customer"; // 求最小值
        Query query = session.createQuery(hql3);
        Number result = (Number) query.uniqueResult();
        System.out.println(result);

        // -----------------------------------------------------
        tx.commit();
        session.close();
    }

6、投影查询

// 投影查询

    @Test
    public void fun6() {
        Session session = HibernateUtils.openSession();
        Transaction tx = session.beginTransaction();
        // -----------------------------------------------------

        String hql1 = "select new Customer(cust_id,cust_name) from Customer";
        Query query = session.createQuery(hql1);
        List<Customer> list = query.list();
        System.out.println(list);

        // -----------------------------------------------------
        tx.commit();
        session.close();
    }

  

  以上查询都是单表查询,没有涉及多表查询,多表查询等我们熟悉好这几个查询,才继续学习多表查询。多表查询比单表查询复杂一点点。

posted on 2019-08-09 10:28  HHH燕  阅读(1204)  评论(0编辑  收藏  举报

导航