對于我們學習的hql,我大概理解為就是一種查詢的語言,它沒有增加、刪除、修改的作用,而對我們用來查詢的操作,感覺用起來就是很簡便,代碼很少,很好理解一些。
下面是查詢操作的簡單實例
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
package com.lc.view; import java.util.iterator; import java.util.list; import org.hibernate.session; import org.hibernate.transaction; import com.lc.domain.student; import com.lc.utils.hibernateutil; public class selectstudent { public static void main(string[] args) { selectsomestudents(); } /** * 1.檢索所有的學生 **/ public static void selectallstudents(){ session session = null ; transaction ts = null ; try { session = hibernateutil.getcurrentsession(); ts = session.begintransaction(); list<student> list = session.createquery( "from student" ).list(); //取出數據1.for循環增強 for (student stu:list){ system.out.println(stu.getsid()+ " " + stu.getsname()+ " " +stu.getsdept()); } //取出數據2.迭代器 system.out.println( "------------------------------" ); iterator iterator = list.iterator(); while (iterator.hasnext()){ student s = (student) iterator.next(); system.out.println(s.getsid()+ " " + s.getsname()+ " " +s.getsdept()); } ts.commit(); } catch (exception e) { if (ts != null ) { ts.rollback(); } throw new runtimeexception(e.getmessage()); } finally { if (session != null && session.isopen()) { session.close(); } } } /** * 2.檢索部分的學生 **/ public static void selectsomestudents(){ session session = null ; transaction ts = null ; try { session = hibernateutil.getcurrentsession(); ts = session.begintransaction(); /** *不可以這樣去除數據了 因為只有student對象的兩個屬性值 不是一個對象 list<student> list = session.createquery("select sname,sdept from student").list(); for(student stu:list){ system.out.println(stu.getsname()+" "+stu.getsdept()); }**/ list list = session.createquery( "select sname,sdept from student" ).list(); for ( int i= 0 ;i<list.size();i++){ object[] obj = (object[]) list.get(i); system.out.println(obj[ 0 ].tostring()+ " " +obj[ 1 ].tostring()); } ts.commit(); } catch (exception e) { if (ts != null ) { ts.rollback(); } throw new runtimeexception(e.getmessage()); } finally { if (session != null && session.isopen()) { session.close(); } } } } |
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對服務器之家的支持。如果你想了解更多相關內容請查看下面相關鏈接
原文鏈接:https://blog.csdn.net/xlgen157387/article/details/39804385