HQL语句集合大全

HQL 查询(简单多表查询 不复杂的查询)
查询所有对象

@Test
    public void fun() {
        String hql = "from User ";
        Session session = HibernateUtils.getSession();
        Transaction transaction = session.beginTransaction();
        Query query = session.createQuery(hql);
        //*************************************     
        List<User> list = query.list();
        System.out.println(list);
        //**************************************

        transaction.commit();
        session.close();
    }

问号占位符

@Test
    public void fun2() {
        Session session = HibernateUtils.getSession();
        Transaction transaction = session.beginTransaction();
        //*************************************
        String hql = "from User where id=?";
        Query query = session.createQuery(hql);
        //给语句中问号赋值  索引从0开始
        query.setParameter(0, 1);
        //query.setParameter(1, "wl");
        System.out.println(query.uniqueResult());
        //**************************************    
        transaction.commit();
        session.close();
    }

冒号占位符

@Test
    public void fun3() {
        Session session = HibernateUtils.getSession();
        Transaction transaction = session.beginTransaction();
        //*************************************
        //冒号后面相当于 给这个冒号占位符 起了个别名
        //注意:冒号后面不要加空格
        String hql = "from User where id=:ww";
        Query query = session.createQuery(hql);
        //直接使用别名 给 占位符 赋值
        query.setParameter("ww", 1);
        System.out.println(query.uniqueResult());
        //**************************************    
        transaction.commit();
        session.close();
    }

分页查询

@Test
    public void fun4() {
        Session session = HibernateUtils.getSession();
        Transaction transaction = session.beginTransaction();
        //*************************************
        String hql = "from User";
        Query query = session.createQuery(hql);
        //设置起始和最大显示
        query.setFirstResult(2);
        query.setMaxResults(2);
        //接收结果
        List list = query.list();
        System.out.println(list);
        //**************************************    
        transaction.commit();
        session.close();
    }

无语句查询

@Test
    public void fun5() {
        Session session = HibernateUtils.getSession();
        Transaction transaction = session.beginTransaction();
        //*************************************
        Criteria criteria = session.createCriteria(User.class);
        List list = criteria.list();
        System.out.println(list);
        //**************************************    
        transaction.commit();
        session.close();
    }

添加查询条件查询

@Test
    public void fun6() {
        Session session = HibernateUtils.getSession();
        Transaction transaction = session.beginTransaction();
        //*************************************
        Criteria criteria = session.createCriteria(User.class);
        //添加一个查询条件
        criteria.add(Restrictions.eq("id", 2));
        User user = (User)criteria.uniqueResult();
        System.out.println(user);
        //**************************************    
        transaction.commit();
        session.close();
    }
  HQL运算符                   QBC运算符                         含义
      =                    Restrictions.eq()                  等于
      >                    Restrictions.gt()                  大于
      >=                   Restrictions.ge()                  大于等于
      <                    Restrictions.lt()                  小于
      <=                   Restrictions.le()                  小于等于
      is null              Restrictions.isnull()              等于空值
      is not null          Restrictions.isNotNull()           非空值
      like                 Restrictions.like()                字符串模式匹配
      and                  Restrictions.and()                 逻辑与
      or                   Restrictions.or()                  逻辑或
      not                  Restrictions.not()                 逻辑非
      in(列表)              Restrictions.in()                  等于列表中的某一个值
      between x and y      Restrictions.between()             闭区间xy中的任意值

原生查询

@Test
    public void fun9() {
        Session session = HibernateUtils.getSession();
        Transaction transaction = session.beginTransaction();
        //*************************************
        String sql = "select * from user where id = ?";
        Query query = session.createSQLQuery(sql);
        query.setParameter(0, 3);
        Object[] objects = (Object[])query.uniqueResult();
        for (Object object : objects) {
            System.out.println(object);
        }

        //**************************************    
        transaction.commit();
        session.close();
    }

你可能感兴趣的:(hibernate)