HQL语句

1、什么是hql?

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

hql和sql区别/异同
注:QuerySyntaxException:book is not mapped

处理返回的结果集
实体类与配置文件就不贴了。
@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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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();

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
连接查询
@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();

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
聚合函数
@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();

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值