Hibernate查询语言HQL

HQL:hibernate query language 即hibernate查询语言
hql是完全面向对象的查询语言,可以理解继承,多态,关联等概念
hql区分大小写。但是对应sql关键字(select,from where)不区分


一:基本查询

(1)单属性查询

//查询所有书名
        //创建Query对象
        Session session = HibernateUtil.getSession();
        Transaction tx = session.beginTransaction();

        //Book 大写 表示的是  cn.siggy.pojo.Book类
        //name表示的 Book类中的属性名
        String hql="select name from Book";
        Query query = session.createQuery(hql);
        //list()方法返回查询结果
        //返回结果的类型 是根据查询的列决定的
        List<String> list = query.list();
        for(String bookname:list){
            System.out.println(bookname);
        }
        tx.commit();

(2)多属性查询

//查询所有书 的名称和价格
        //创建Query对象
        Session session = HibernateUtil.getSession();
        Transaction tx = session.beginTransaction();

        //查询多个列时  返回结果是数组集合 数组中元素的类型 是由查询列来决定
        List list = session.createQuery("select name,price from Book").list();
        for(Object[] objs:list){
            System.out.println(objs[0]+"--"+objs[1]);
        }
        tx.commit();

(3)将多个查询封装为对象
利用了构造方法

//查询多个列时 将查询结果封装为对象集合
    @Test
    public void testQuery3(){
        //查询所有书 的名称和价格
        //创建Query对象
        Session session = HibernateUtil.getSession();
        Transaction tx = session.beginTransaction();

        List list = session.createQuery("select new Book(name,price) from Book").list();
        for(Book b:list){
            System.out.println(b);
        }
        tx.commit();
        HibernateUtil.closeSession();
    }

(4)别名的使用

Session session = HibernateUtil.getSession();
        Transaction tx = session.beginTransaction();

        List list = session.createQuery("select new Book(b.name,b.price) from Book as b").list();
        for(Book b:list){
            System.out.println(b);
        }
        tx.commit();

(5)查询所有列 不用select

Session session = HibernateUtil.getSession();
        Transaction tx = session.beginTransaction();

        List list = session.createQuery("from Book").list();
        for(Book b:list){
            System.out.println(b);
        }
        tx.commit();

(6)查询所有列2 使用select 需要使用别名

Session session = HibernateUtil.getSession();
        Transaction tx = session.beginTransaction();
        List list = session.createQuery("select b from Book b").list();
        for(Book b:list){
            System.out.println(b);
        }
        tx.commit();

二:条件查询

(1)占位符? 从0开始 使用setParameter(不用理会参数类型)

Session session = HibernateUtil.getSession();
        Transaction tx = session.beginTransaction();

        List list = session.createQuery("from Book b where id)
                        .setParameter(0, 4)
                        .list();
        for(Book b:list){
            System.out.println(b);
        }
        tx.commit();

(2)命名查询

Session session = HibernateUtil.getSession();
        Transaction tx = session.beginTransaction();

        /*指定名称*/
        List list = session.createQuery("from Book b where id<:id")
                        .setParameter("id", 4)
                        .list();
        for(Book b:list){
            System.out.println(b);
        }
        tx.commit();

(3)分页查询

Session session = HibernateUtil.getSession();
        Transaction tx = session.beginTransaction();
        /*开始显示的记录下标 (从下标为3的记录开始查询,即id=4)*/
        /*设置每页记录数pageSize*/
        List list = session.createQuery("from Book b")
                        .setFirstResult(3)
                        .setMaxResults(3)
                        .list();
        for(Book b:list){
            System.out.println(b);
        }
        tx.commit();

(4)统计查询

//查询图书总数
//返回结果唯一,所以不用list,使用uniqueResult
            Session session = HibernateUtil.getSession();
            Transaction tx = session.beginTransaction();
            //类型可能在变化,可能是int也可能是long,大的时候就是long
            //count无参数就是返回行数,有参数就是返回该列非空的数值个数
            Number count = (Number)session.createQuery("select count(b.name)  from Book b")
                            .uniqueResult();

            System.out.println("总数:"+count.intValue());
            tx.commit();
            Session session = HibernateUtil.getSession();
            Transaction tx = session.beginTransaction();

            Object count = (Number)session.createQuery("select count(b.price)  from Book b")
                            .uniqueResult();

            System.out.println("总数:"+count);
            tx.commit();

(5)分组查询

//查询图书总数
        Session session = HibernateUtil.getSession();
        Transaction tx = session.beginTransaction();
        //int,long
        List list = session.createQuery("select b.category.name,count(b.id) from Book b group by b.category.name")
                        .list();
        for(Object[] objs:list){
            System.out.println(objs[0]+"--"+objs[1]);
        }
        tx.commit();

(6)排序

Session session = HibernateUtil.getSession();
        Transaction tx = session.beginTransaction();
    //排序
        List list = session.createQuery("from Book order by price desc")
                        .list();
        for(Book b:list){
            System.out.println(b);
        }
        tx.commit();

(7)对象导航 连接

    //查询 "仙侠"的书籍信息
        Session session = HibernateUtil.getSession();
        Transaction tx = session.beginTransaction();
        //三个hql其实都是一样的
        String hql="from Book b where b.category.name=:name";
        hql="select b from Book b join b.category c where c.name=:name";
        hql="select b from Book b inner join b.category c where c.name=:name";
        List list = session.createQuery(hql)
                        .setString("name", "仙侠")
                        .list();
        for(Book b:list){
            System.out.println(b); 
        }

(8)左外连接

Session session = HibernateUtil.getSession();
        Transaction tx = session.beginTransaction();

        String hql="select c.name,b.name from Category c left outer join c.books b";
        List list = session.createQuery(hql)
                        .list();
        for(Object[] objs:list){
            System.out.println(objs[0]+"----"+objs[1]);
        }

你可能感兴趣的:(hibernate,hql,hibernate4)