SSH入门系列10:初窥HQL
SSH入门系列10:初窥HQL
ORZ你好 发表于4个月前
SSH入门系列10:初窥HQL
  • 发表于 4个月前
  • 阅读 1
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 新注册用户 域名抢购1元起>>>   

HQL完整的语句格式如下:

Select/update/delete…… from …… where …… group by …… having …… order by …… asc/des 非常类似于SQL语句。常见的有如下几种

1:实体查询

Configuration configure = new   Configuration().configure();
        SessionFactory  sessionFactory = configure.buildSessionFactory();
        Session session =  sessionFactory.openSession();

        String hql="from Books";
        List list = session.createQuery(hql).list();
        Iterator it = list.iterator();
        while (it.hasNext()) {
            Books books = (Books)it.next();
            System.out.println(books.getAuthor());
        }
        
    }

2:属性查询

    Configuration configure = new   Configuration().configure();
        SessionFactory  sessionFactory = configure.buildSessionFactory();
        Session session =  sessionFactory.openSession();

         //此处as 可删
        String hql="select books.author,books.title from Books as books";
         //无法按照下面的模式写 会报could not resolve property 错误
        //String hql ="select Books.title , Books.author form Books";
        List list = session.createQuery(hql).list();
        Iterator iter = list.iterator();
        while (iter.hasNext()) {
            /*Books books = (Books)iter.next();
            System.out.println(books.getAuthor());
            System.out.println(books.getTitle());
            Ljava.lang.Object; cannot be cast to com.model.Books
            */
            Object[] object = (Object[])iter.next();
            System.out.println(object[0]);
            System.out.println(object[1]);
        }

3按照参数位置进行查询:

       Configuration configure =new Configuration().configure();
         SessionFactory  sessionFactory = configure.buildSessionFactory();
         Session  session = sessionFactory.openSession();
         String hql ="from Books books where books.title like ?" ;
         Query query = session.createQuery(hql);
         query.setString(0, "%C++Primer%");
         List list = query.list();
         Iterator iter = list.iterator();
         while (iter.hasNext()) {
        Books books = (Books)iter.next();
        System.out.println(books.getAuthor());
        }

4按照参数名字进行查询:

Configuration configure =new Configuration().configure();
         SessionFactory  sessionFactory = configure.buildSessionFactory();
         Session  session = sessionFactory.openSession();
        //=后面必须加:  同时setString不能改为books.Title
        //经试验,下面一种方法不可以
         //String hql ="from Books books where books.title=C++Primer ";
         String hql = "from Books books where books.title=:bookTitle";
         Query query = session.createQuery(hql);
         query.setString("bookTitle", "C++Primer");
         List list = query.list();
         Iterator iter = list.iterator();
         while (iter.hasNext()) {
        Books books = (Books)iter.next();
        System.out.println(books.getAuthor());
        }

5连接查询

Configuration configure =new Configuration().configure();
         SessionFactory  sessionFactory = configure.buildSessionFactory();
         Session  session = sessionFactory.openSession();
        String hql = " select b from Books b, Publishers p  where b.publishers=p  and p.name='清华大学出版社'";
    
         //经试验,下面一种方法不可以
        // String hql ="from Books books where books.title=C++Primer ";
         Query query = session.createQuery(hql);
        //query.setString("bookTitle", "C++Primer");
         List   list = query.list();
        Iterator itor = list.iterator();
         while (itor.hasNext()) {
            Books books = (Books)itor.next();
            System.out.println(books.getAuthor()+"     "+books.getTitle());    
                }

6进行升序降序

 Configuration configure =new Configuration().configure();
         SessionFactory  sessionFactory = configure.buildSessionFactory();
         Session  session = sessionFactory.openSession();
        String hql = " from Books b order by b.title asc";
          //经试验,下面一种方法不可以
        // String hql ="from Books books where books.title=C++Primer ";
         Query query = session.createQuery(hql);
        //query.setString("bookTitle", "C++Primer");
        // query.setFirstResult(0);
        // query.setMaxResults(3);
         List   list = query.list();
        Iterator itor = list.iterator();
         while (itor.hasNext()) {
            Books books = (Books)itor.next();
            System.out.println(books.getAuthor()+"     "+books.getTitle());    
                }

7聚集函数:需要加入uniqueResult

 Configuration configure =new Configuration().configure();
         SessionFactory  sessionFactory = configure.buildSessionFactory();
         Session session = sessionFactory.openSession();
         String hql =" select count(b) from  Books b";
         Query query = session.createQuery(hql);
         Long count = (Long)query.uniqueResult();
         System.out.println("图书总数是"+count.toString());
         String hql1 = "select avg(b.unitPrice)  from Books b";
         Query query1 = session.createQuery(hql1);
         Double  price = (Double)query1.uniqueResult();
         System.out.println("平均价格是"+price.toString());
         String hql2 = "select max(b.unitPrice)  from Books  b";
         Query query2 = session.createQuery(hql2);
        Integer MaxPrice = (Integer)query2.uniqueResult();
         System.out.println("最高价格是:"+MaxPrice.toString());
         String hql3 = "select min(b.unitPrice) from Books b";
         Query query3 = session.createQuery(hql3);
         Integer MinPrice = (Integer)query3.uniqueResult();
         System.out.println("最小价格是"+MinPrice.toString());

共有 人打赏支持
粉丝 0
博文 13
码字总数 9911
×
ORZ你好
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: