Hibernate的hql查询语言

hql 是hibernate查询语言,全称Hibernate Query Language。

SQL HQL
1. 结构化查询语言,表与列 1. 面向对象的查询语言,类与属性
2. 不区分大小写 2. 类和属性区分大小写,但关键字不区别大小写
3. 可以通过空格与as给表和列起别名 3. 可以通过空格与as给类和属性起别名
4. 支持?占位符,从顺序1开始 4. 支持?占位符,从下标0开始
5. 支持:命名参数

HQL常见错误

Query接口常用方法

单个参数 一组参数
setXXX(int,XXX); setParameterList(String,Object[]);//窗口中最少要保存一个值
setXXX(String,XXX); setParameterList(String,Collection);
setParameter(int,Object);
setParameter(String,Object);
  • 测试类:
    @Test
    public void test() {
    Session session = HibernateUtils.getSession();
    Transaction beginTransaction = session.beginTransaction();

     	String sql="select count(*) from Book";
     	Query query = session.createQuery(sql);
     	
     	
     	/**
     	 * list() 返回的是多个对象、
     	 * 
     	 * 查询多个的hql语句与查询单个的hql语句返回的结果集都是大同小异的
     	 */
    

    // List queryList = query.list();
    //// queryList.stream().forEach(a -> System.out.println(Arrays.toString((Object[])a)));
    // queryList.stream().forEach(System.out::println);

     	/**
     	 * getSingleResult()返回的是一个对象
     	 */
     	Object obj = query.getSingleResult();
     	System.out.println(obj.getClass().getName());
     	System.out.println(obj);
     	
     	/*	解释:使用hql语句根据id查询一个Book ,返回的是一个Book对象 
     	 *  hql :  from Book where book_id=1	   
     	 */
     	
     	/*	解释:使用hql语句根据id查询一个Book的某个属性  , 返回的是一个String对象
     	 *  hql : select book_name from Book where book_id=1  
     	 */
     	
     	/*
     	 * 	解释: 使用hql语句根据id查询一个Book的多个属性 , 返回的是一个Object的数组
     	 * 	hql : select book_name,price from Book where book_id=1 
     	 */
     	
     	/*
     	 * 	解释: 使用hql语句根据id查询一个Book的多个属性 , 返回的是一个Map集合
     	 * 	
     	 * 	hql : select new Map(book_name,price) from Book where book_id=1
     	 * 	如果不为map括号中的列设置别名,则默认的别人为从0开始的递增数字 : {0=西游记, 1=50.0}
     	 * 
     	 * 	hql : select new Map(book_name as book_name,price as price) from Book where book_id=1
     	 * 	如果指定了别名,则Map中键值对中的健,就是你设置的别名  : {book_name=西游记, price=50.0}
     	 * 
     	 */
     	
     	/*
     	 * 	解释:使用hql语句根据id查询一个Book的多个属性,返回一个Book对象
     	 * 
     	 * 	hql : select new Book(book_name,price) from Book where book_id=1
     	 * 	这种new Book()的写法与Book实体类的构造器中的参数以及构造器中参数属性的顺序都是要一一对应的 ,
     	 */
     	
     	
     	beginTransaction.commit();
     	session.close();
    

    }

  1. hql中使用占位符

    @Test
    public void test(){
    Session session = HibernateUtils.getSession();
    Transaction beginTransaction = session.beginTransaction();

     	String sql="from Book where book_id=:id";
             //    from Book where book_id in :ids
     	Query query = session.createQuery(sql);
                 query.setParameter("id", 1);
                 /*
     	 * hql 中占位符的使用  :
     	 * 	  一:hql中使用 = 号时
     	 * 		1、 在hql语句中定义占位符
     	 * 			from Book where book_id=:id    
     	 * 
     	 * 		2、为结果集中的占位符设置值
     	 * 			query.setParameter("id", 1);
     	 * 		
     	 * 	 二:hql中使用 in 关键字时
     	 * 		1、在hql语句中定义占位符
     	 * 			from Book where book_id in :ids
     	 * 
     	 * 		2、为结果集中的占位符设置值
     	 * 			List<Integer> list=new ArrayList<>();
     	 * 			list.add(1);
     	 * 			list.add(2);
     	 * 			query.setParameterList("ids", list);
     	 * 
     	 */
                 List queryList = query.list();
     	queryList.stream().forEach(System.out::println);
    
                 beginTransaction.commit();
     	session.close();
    

    }

  2. 连接查询

    @Test
    public void test(){
    Session session = HibernateUtils.getSession();
    Transaction beginTransaction = session.beginTransaction();

     	String sql="select new Map(o.order_no as order_no, i.product_id as product_id) from Book as b,Category as c on b.book_id.categorys=c";
     	Query query = session.createQuery(sql);
     	
     	
     	/**
     	 * list() 返回的是多个对象、
     	 * 
     	 * 查询多个的hql语句与查询单个的hql语句返回的结果集都是大同小异的
     	 */
     	List queryList = query.list();
     	queryList.stream().forEach(System.out::println);
     	
     	/**
     	 * 连表查询的hql语句的写法:
     	 * 		select new Map(o.order_no as order_no, i.product_id as product_id) from Book as b,Category as c on b.book_id.categorys=c
     	 * 		
     	 * 		不指定返回类型的语句
     	 * 		from Book as b,Category as c on b.book_id.categorys=c
     	 */
     	beginTransaction.commit();
     	session.close();
    

    }

  3. 聚合函数

    @Test
    public void test(){
    Session session = HibernateUtils.getSession();
    Transaction beginTransaction = session.beginTransaction();

     	String sql="select count(*) from Book";
     	Query query = session.createQuery(sql);
     	/**
     	 * getSingleResult()返回的是一个对象
     	 */
     	Object obj = query.getSingleResult();
     	System.out.println(obj.getClass().getName());
     	System.out.println(obj);
     	
     	/**
     	 * 聚合函数:
     	 * 	sum、avg、max、min、count
     	 * 		select count(*) from Book
     	 */
     	beginTransaction.commit();
     	session.close();
    

    }

  4. hql分页

    @Test
    public void test(){
    Session session = HibernateUtils.getSession();
    Transaction beginTransaction = session.beginTransaction();

     String sql="from Book";
     Query query = session.createQuery(sql);
     int page=2;  //页码
     int row=2;  //每页行数
     //设置分页
     query.setFirstResult((page-1)*row);    // 设置起始记录下标
     query.setMaxResults(row);            // 设置返回的最大结果集  
    
    
         /**
      * list() 返回的是多个对象、
      * 
      * 查询多个的hql语句与查询单个的hql语句返回的结果集都是大同小异的
      */
     List queryList = query.list();
     queryList.stream().forEach(System.out::println);
    
     beginTransaction.commit();
     session.close();
    

    }

猜你喜欢

转载自blog.csdn.net/KidShmily/article/details/84894127
今日推荐