list()查询
1
2
3
4
5
6
7
8
9
10
11
|
//一次性把数据对象取出来
@Test
public
void
findTestList(){
Session s=sessionFactory.getCurrentSession();
s.beginTransaction();
List<Person> persons=s.createQuery(
"from Person"
).list();
for
(Person person:persons){
System.out.println(person.getName()+
"----"
+person.getId());
}
s.getTransaction().commit();
}
|
sql语句如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
16:00:43,118 DEBUG SQL:111 -
select
person0_.id
as
id1_,
person0_.p_age
as
p2_1_,
person0_.group_id
as
group4_1_,
person0_.p_name
as
p3_1_
from
p_person person0_
张三0
----1
张三1
----2
张三2
----3
张三3
----4
张三4
----5
张三5
----6
张三6
----7
张三7
----8
张三8
----9
张三9
----10
|
iterate()查询
1
2
3
4
5
6
7
8
9
10
11
12
13
|
//一次性取出来的是所有对象的主键值,并且会把这些主键值放在Session缓冲中去
//下次需要的话,直接在Session中获取,不需要再次访问数据库
//缺点是将查询出来的主键值,分别再以主键进行取值对象,造成数据库压力巨大
@Test
public
void
findTestIterate(){
Session s=sessionFactory.getCurrentSession();
s.beginTransaction();
Iterator<Person> persons=s.createQuery(
"from Person"
).iterate();
while
(persons.hasNext()){
System.out.println(persons.next().getName());
}
s.getTransaction().commit();
}
|
sql语句
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
16:03:55,776 DEBUG SQL:111 -
select
person0_.id
as
col_0_0_
from
p_person person0_
16:03:55,806 DEBUG SQL:111 -
select
person0_.id
as
id1_0_,
person0_.p_age
as
p2_1_0_,
person0_.group_id
as
group4_1_0_,
person0_.p_name
as
p3_1_0_
from
p_person person0_
where
person0_.id=?
张三0
16:03:55,823 DEBUG SQL:111 -
select
person0_.id
as
id1_0_,
person0_.p_age
as
p2_1_0_,
person0_.group_id
as
group4_1_0_,
person0_.p_name
as
p3_1_0_
from
p_person person0_
where
person0_.id=?
张三1
16:03:55,826 DEBUG SQL:111 -
select
person0_.id
as
id1_0_,
person0_.p_age
as
p2_1_0_,
person0_.group_id
as
group4_1_0_,
person0_.p_name
as
p3_1_0_
from
p_person person0_
where
person0_.id=?
张三2
16:03:55,832 DEBUG SQL:111 -
select
person0_.id
as
id1_0_,
person0_.p_age
as
p2_1_0_,
person0_.group_id
as
group4_1_0_,
person0_.p_name
as
p3_1_0_
from
p_person person0_
where
person0_.id=?
张三3
16:03:55,836 DEBUG SQL:111 -
select
person0_.id
as
id1_0_,
person0_.p_age
as
p2_1_0_,
person0_.group_id
as
group4_1_0_,
person0_.p_name
as
p3_1_0_
from
p_person person0_
where
person0_.id=?
张三4
16:03:55,838 DEBUG SQL:111 -
select
person0_.id
as
id1_0_,
person0_.p_age
as
p2_1_0_,
person0_.group_id
as
group4_1_0_,
person0_.p_name
as
p3_1_0_
from
p_person person0_
where
person0_.id=?
张三5
16:03:55,841 DEBUG SQL:111 -
select
person0_.id
as
id1_0_,
person0_.p_age
as
p2_1_0_,
person0_.group_id
as
group4_1_0_,
person0_.p_name
as
p3_1_0_
from
p_person person0_
where
person0_.id=?
张三6
16:03:55,844 DEBUG SQL:111 -
select
person0_.id
as
id1_0_,
person0_.p_age
as
p2_1_0_,
person0_.group_id
as
group4_1_0_,
person0_.p_name
as
p3_1_0_
from
p_person person0_
where
person0_.id=?
张三7
16:03:55,846 DEBUG SQL:111 -
select
person0_.id
as
id1_0_,
person0_.p_age
as
p2_1_0_,
person0_.group_id
as
group4_1_0_,
person0_.p_name
as
p3_1_0_
from
p_person person0_
where
person0_.id=?
张三8
16:03:55,875 DEBUG SQL:111 -
select
person0_.id
as
id1_0_,
person0_.p_age
as
p2_1_0_,
person0_.group_id
as
group4_1_0_,
person0_.p_name
as
p3_1_0_
from
p_person person0_
where
person0_.id=?
张三9
|
get()查询
1
2
3
4
5
6
7
8
9
|
//立刻发送sql语句,不管我们是否需要,比如person.getName();
@Test
public
void
findTestget(){
Session s=sessionFactory.getCurrentSession();
s.beginTransaction();
Person person=(Person)s.get(Person.
class
,
1
);
// System.out.println(person.getName());
s.getTransaction().commit();
}
|
load()查询
1
2
3
4
5
6
7
8
9
|
//不会立刻发送sql语句,只有当我们需要的时候才会发送sql语句,比如person.getName();
@Test
public
void
findTestload(){
Session s=sessionFactory.getCurrentSession();
s.beginTransaction();
Person person=(Person)s.load(Person.
class
,
1
);
// System.out.println(person.getName());
s.getTransaction().commit();
}
|
本文转自 小夜的传说 51CTO博客,原文链接:http://blog.51cto.com/1936625305/1576128,如需转载请自行联系原作者