- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章基于hibernate实现的分页技术实例分析由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
本文实例讲述了基于hibernate实现的分页技术。分享给大家供大家参考,具体如下:
先说明一下基于hibernate实现分页的原理,假如从数据库取出100条数据,我们要让每页显示10条,假如从30开始,只需要设置起始位置和最大的返回结果即可 先上代码:注意传进来的参数有 Page这类,后面有介绍 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public
List<Article> queryByPage(
final
String username,
final
Page page) {
return
this
.getHibernateTemplate().executeFind(
new
HibernateCallback() {
public
Object doInHibernate(Session session)
throws
HibernateException, SQLException {
Query query = session.createQuery(
"select art from Article art where art.username = ?"
);
//设置参数
query.setParameter(
0
, username);
//设置每页显示多少个,设置多大结果。
query.setMaxResults(page.getEveryPage());
//设置起点
query.setFirstResult(page.getBeginIndex());
return
query.list();
}
});
|
上面关键代码是 setMaxResults(),和setFirstResult(),即设置最大显示值和起点 。
这里我们需要一个Page工具类,用来操作分页.
Page.java:
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
|
package
com.fenye;
public
class
Page {
// 1.每页显示数量(everyPage)
private
int
everyPage;
// 2.总记录数(totalCount)
private
int
totalCount;
// 3.总页数(totalPage)
private
int
totalPage;
// 4.当前页(currentPage)
private
int
currentPage;
// 5.起始点(beginIndex)
private
int
beginIndex;
// 6.是否有上一页(hasPrePage)
private
boolean
hasPrePage;
// 7.是否有下一页(hasNextPage)
private
boolean
hasNextPage;
public
Page(
int
everyPage,
int
totalCount,
int
totalPage,
int
currentPage,
int
beginIndex,
boolean
hasPrePage,
boolean
hasNextPage) {
this
.everyPage = everyPage;
this
.totalCount = totalCount;
this
.totalPage = totalPage;
this
.currentPage = currentPage;
this
.beginIndex = beginIndex;
this
.hasPrePage = hasPrePage;
this
.hasNextPage = hasNextPage;
}
//构造函数,默认
public
Page(){}
//构造方法,对所有属性进行设置
public
int
getEveryPage() {
return
everyPage;
}
public
void
setEveryPage(
int
everyPage) {
this
.everyPage = everyPage;
}
public
int
getTotalCount() {
return
totalCount;
}
public
void
setTotalCount(
int
totalCount) {
this
.totalCount = totalCount;
}
public
int
getTotalPage() {
return
totalPage;
}
public
void
setTotalPage(
int
totalPage) {
this
.totalPage = totalPage;
}
public
int
getCurrentPage() {
return
currentPage;
}
public
void
setCurrentPage(
int
currentPage) {
this
.currentPage = currentPage;
}
public
int
getBeginIndex() {
return
beginIndex;
}
public
void
setBeginIndex(
int
beginIndex) {
this
.beginIndex = beginIndex;
}
public
boolean
isHasPrePage() {
return
hasPrePage;
}
public
void
setHasPrePage(
boolean
hasPrePage) {
this
.hasPrePage = hasPrePage;
}
public
boolean
isHasNextPage() {
return
hasNextPage;
}
public
void
setHasNextPage(
boolean
hasNextPage) {
this
.hasNextPage = hasNextPage;
}
}
|
Page工具类主要是封装页面信息,一共多少数据啊,一页显示多少啊,起点的序号,总页数,是否有上一页下一页,当前页.
还需要一个操作page的工具类,PageUtil.java 。
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
|
package
com.sanqing.fenye;
/*
* 分页信息辅助类
*/
public
class
PageUtil {
public
static
Page createPage(
int
everyPage,
int
totalCount,
int
currentPage) {
everyPage = getEveryPage(everyPage);
currentPage = getCurrentPage(currentPage);
int
totalPage = getTotalPage(everyPage, totalCount);
int
beginIndex = getBeginIndex(everyPage, currentPage);
boolean
hasPrePage = getHasPrePage(currentPage);
boolean
hasNextPage = getHasNextPage(totalPage, currentPage);
return
new
Page(everyPage, totalCount, totalPage, currentPage,
beginIndex, hasPrePage, hasNextPage);
}
public
static
Page createPage(Page page,
int
totalCount) {
int
everyPage = getEveryPage(page.getEveryPage());
int
currentPage = getCurrentPage(page.getCurrentPage());
int
totalPage = getTotalPage(everyPage, totalCount);
int
beginIndex = getBeginIndex(everyPage, currentPage);
boolean
hasPrePage = getHasPrePage(currentPage);
boolean
hasNextPage = getHasNextPage(totalPage, currentPage);
return
new
Page(everyPage, totalCount, totalPage, currentPage,
beginIndex, hasPrePage, hasNextPage);
}
//设置每页显示记录数
public
static
int
getEveryPage(
int
everyPage) {
return
everyPage ==
0
?
10
: everyPage;
}
//设置当前页
public
static
int
getCurrentPage(
int
currentPage) {
return
currentPage ==
0
?
1
: currentPage;
}
//设置总页数,需要总记录数,每页显示多少
public
static
int
getTotalPage(
int
everyPage,
int
totalCount) {
int
totalPage =
0
;
if
(totalCount % everyPage ==
0
) {
totalPage = totalCount / everyPage;
}
else
{
totalPage = totalCount / everyPage +
1
;
}
return
totalPage;
}
//设置起始点,需要每页显示多少,当前页
public
static
int
getBeginIndex(
int
everyPage,
int
currentPage) {
return
(currentPage -
1
) * everyPage;
}
//设置是否有上一页,需要当前页
public
static
boolean
getHasPrePage(
int
currentPage) {
return
currentPage ==
1
?
false
:
true
;
}
//设置是否有下一个,需要总页数和当前页
public
static
boolean
getHasNextPage(
int
totalPage,
int
currentPage) {
return
currentPage == totalPage || totalPage ==
0
?
false
:
true
;
}
}
|
创建Page只需要3个参数,每页显示多少数据,当前页,总共多少数据,其他的4个参数都可以通过这三个计算出来 。
所以后面要创建Page,只需要调用这工具方法PageUtil.createPage(3个参数),就返回一Page. 。
返回的Page就是前面参数的Page,即要显示的分页 。
这样就算完成了分页的功能.
希望本文所述对大家基于Hibernate框架的Java程序设计有所帮助.
最后此篇关于基于hibernate实现的分页技术实例分析的文章就讲到这里了,如果你想了解更多关于基于hibernate实现的分页技术实例分析的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我刚刚继承了一个旧的 PostgreSQL 安装,需要进行一些诊断以找出该数据库运行缓慢的原因。在 MS SQL 上,您可以使用 Profiler 等工具来查看正在运行的查询,然后查看它们的执行计划。
将目标从Analytics(分析)导入到AdWords中,然后在Analytics(分析)中更改目标条件时,是否可以通过更改将目标“重新导入”到AdWords,还是可以自动选择? 最佳答案 更改目标值
我正在使用google analytics api来获取数据。我正在获取数据,但我想验证两个参数,它们在特定日期范围内始终为0。我正在获取['ga:transactions']和['ga:goalCo
我使用Google API从Google Analytics(分析)获取数据,但指标与Google Analytics(分析)的网络界面不同。 即:我在2015年3月1日获得数据-它返回综合浏览量79
我在我的Web应用程序中使用sammy.js进行剔除。我正在尝试向其中添加Google Analytics(分析)。我很快找到了following plugin来实现页面跟踪。 我按照步骤操作,页面如
当使用 Xcode 分析 (product>analyze) 时,有没有办法忽略给定文件中的任何错误? 例如编译指示之类的? 我们只想忽略第三方代码的任何警告,这样当我们的代码出现问题时,它对我们
目录 EFK 1. 日志系统 2. 部署ElasticSearch 2.1 创建handless服务 2.2 创建s
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve thi
GCC/G++ 是否有可用于输出分析的选项? 能够比较以前的代码与新代码之间的差异(大小、类/结构的大小)将很有用。然后可以将它们与之前的输出进行比较以进行比较,这对于许多目的都是有用的。 如果没有此
我正在浏览 LYAH,并一直在研究处理列表时列表理解与映射/过滤器的使用。我已经分析了以下两个函数,并包含了教授的输出。如果我正确地阅读了教授的内容,我会说 FiltB 的运行速度比 FiltA 慢很
在 MySQL 中可以使用 SET profiling = 1; 设置分析 查询 SHOW PROFILES; 显示每个查询所用的时间。我想知道这个时间是只包括服务器的执行时间还是还包括将结果发送到前
我用 Python 编写了几个用于生成阶乘的模块,我想测试运行时间。我找到了一个分析示例 here我使用该模板来分析我的模块: import profile #fact def main():
前几天读了下mysqld_safe脚本,个人感觉还是收获蛮大的,其中细致的交代了MySQL数据库的启动流程,包括查找MySQL相关目录,解析配置文件以及最后如何调用mysqld程序来启动实例等,有着
1 内网基础 内网/局域网(Local Area Network,LAN),是指在某一区域内有多台计算机互联而成的计算机组,组网范围通常在数千米以内。在局域网中,可以实现文件管理、应用软件共享、打印机
1 内网基础 内网/局域网(Local Area Network,LAN),是指在某一区域内有多台计算机互联而成的计算机组,组网范围通常在数千米以内。在局域网中,可以实现文件管理、应用软件共享、打印机
我有四列形式的数据。前三列代表时间,value1,value 2。第四列是二进制,全为 0 或 1。当第四列中对应的二进制值为0时,有没有办法告诉excel删除时间、值1和值2?我知道这在 C++ 或
我正在运行一个进行长时间计算的 Haskell 程序。经过一些分析和跟踪后,我注意到以下内容: $ /usr/bin/time -v ./hl test.hl 9000045000050000 Com
我有一个缓慢的 asp.net 程序正在运行。我想分析生产服务器以查看发生了什么,但我不想显着降低生产服务器的速度。 一般而言,配置生产盒或仅本地开发盒是标准做法吗?另外,您建议使用哪些程序来实现这一
我目前正在尝试分析 Haskell 服务器。服务器永远运行,所以我只想要一个固定时间的分析报告。我尝试只运行该程序 3 分钟,然后礼貌地要求它终止,但不知何故,haskell 分析器不遵守术语信号,并
是否有工具可以分析 Maven 构建过程本身,以便我可以看到构建花费最多时间的地方? 我们在工作中遇到了关于 Maven 3.0.3 和 3.0b1 的问题。与 3.0.3 (9m00s) 相比,我们
我是一名优秀的程序员,十分优秀!