1
public
partial
class
DataAccess
2
{
3
#region
Page Query
4
5
public
IList
<
T
>
Query
<
T
>
(
int
pageIndex,
int
pageSize,
out
int
recordCount)
where
T :
class
6
{
7
return
Query
<
T
>
(
null
,
null
, pageIndex, pageSize,
out
recordCount);
8
}
9
10
public
IList
<
T
>
Query
<
T
>
(ICriterion expression,
int
pageIndex,
int
pageSize,
out
int
recordCount)
where
T :
class
11
{
12
return
Query
<
T
>
(expression,
null
, pageIndex, pageSize,
out
recordCount);
13
}
14
15
public
IList
<
T
>
Query
<
T
>
(ICriterion expression, Order[] order,
16
int
pageIndex,
int
pageSize,
out
int
recordCount)
where
T :
class
17
{
18
IList
<
T
>
list
=
new
List
<
T
>
();
19
recordCount
=
0
;
20
ICriteria query
=
session.CreateCriteria
<
T
>
();
21
if
(expression
!=
null
)
22
{
23
query.Add(expression);
24
}
25
ICriteria queryPage
=
CriteriaTransformer.Clone(query);
26
//
获取记录总数
27
recordCount
=
Convert.ToInt32(query.SetProjection(Projections.RowCount()).UniqueResult());
28
29
//
设置排序
30
if
(order
!=
null
)
31
{
32
foreach
(Order o
in
order)
33
{
34
queryPage.AddOrder(o);
35
}
36
}
37
queryPage.SetFirstResult((pageIndex
-
1
)
*
pageSize);
38
queryPage.SetMaxResults(pageSize);
39
list
=
queryPage.List
<
T
>
();
40
41
return
list;
42
}
43
44
public
IList
<
T
>
Query
<
T
>
(
string
hql,
object
[] values,
int
pageIndex,
int
pageSize)
45
{
46
IQuery query
=
session.CreateQuery(hql);
47
for
(
int
i
=
0
; i
<
values.Length; i
++
)
48
{
49
query.SetParameter(i, values[i]);
50
}
51
return
query.SetFirstResult((pageIndex
-
1
)
*
pageSize).SetMaxResults(pageSize).List
<
T
>
();
52
}
53
54
public
IList
<
T
>
Query
<
T
>
(
string
hql,
string
name,
object
value,
int
pageIndex,
int
pageSize)
55
{
56
return
Query
<
T
>
(hql,
new
string
[] { name },
new
object
[] { value }, pageIndex, pageSize);
57
}
58
59
public
IList
<
T
>
Query
<
T
>
(
string
hql,
string
[] names,
object
[] values,
int
pageIndex,
int
pageSize)
60
{
61
IQuery query
=
session.CreateQuery(hql);
62
for
(
int
i
=
0
; i
<
names.Length; i
++
)
63
{
64
query.SetParameter(names[i], values[i]);
65
}
66
return
query.SetFirstResult((pageIndex
-
1
)
*
pageSize).SetMaxResults(pageSize).List
<
T
>
();
67
}
68
69
public
IList
<
T
>
Query
<
T
>
(
string
hql,
int
pageIndex,
int
pageSize)
70
{
71
IQuery query
=
session.CreateQuery(hql);
72
return
query.SetFirstResult((pageIndex
-
1
)
*
pageSize).SetMaxResults(pageSize).List
<
T
>
();
73
}
74
75
#endregion
76
}