概述
1、hibernate进行数据库查询,函数代码如下(这里对dwr基本的使用不做多余解说):
函数所需类:PageController、Params、AutoQuery。(此三文件如若需要请留下邮箱或者下载地址:http://download.csdn.net/source/612569)
传入参数为查找的页码和参数map
public List select(String pages, Map fromMap) {
Session hibernateSession = HibernateSession.faqSession(); //因为项目用到多数据库,封装了多数据库的链接方式,这块获得的为hibernate的session
PageController customerCtrl = new PageController();//新建类PageController
/*获得map传递进来的参数信息*/
Article article = new Article();//初始化一个实体类
try {
BeanUtils.copyProperties(article, fromMap);//拷贝dwr传入的一个map,拷贝进实体
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
/** 初始化分页参数 */
Params params = new Params();
List result;
params.setTotal(true);
/***********************这块组建HQL*****************/
if (null != article.getId() && article.getId() != 0 && article.getId() != -1) {
params.addParam("id", article.getId(), AutoQuery.LONG);
}
if (null != article.getTitles() && !article.getTitles().equals("")) {
params.addParam("titles", "%" + article.getTitles() + "%", AutoQuery.STRING, "like");
}
if (null != article.getUserName() && !article.getUserName().equals("")) {
params.addParam("userName", "%" + article.getUserName() + "%", AutoQuery.STRING, "like");
}
if (null != article.getUserID() && article.getUserID() != 0 && article.getUserID() != -1) {
params.addParam("userId.id", article.getUserID(), AutoQuery.LONG);
}
if (null != article.getCategoryName() && !article.getCategoryName().equals("")) {
params.addParam("categoryName", "%" + article.getCategoryName() + "%", AutoQuery.STRING, "like");
}
if (null != article.getCategoryID() && article.getCategoryID() != 0 && article.getCategoryID() != -1) {
params.addParam("categoryId.id", article.getCategoryID(), AutoQuery.LONG);
}
/****************************************/
params.setOrderBy(" order by createDate desc ");
params.setFrom("Article");
params.setPageItemNo(customerCtrl.getPageSize());
if (null == pages) {
if (0 == customerCtrl.getCurrentPage()) {
customerCtrl.setCurrentPage(1);
}
} else {
customerCtrl.setCurrentPage(Integer.parseInt(pages));//设置当前页码
}
params.setPageStartNo(customerCtrl.getCurrentPage());
customerCtrl.setParams(params);
AutoQuery aq = new AutoQuery(params);
aq.setSession(hibernateSession);
result = aq.find();
if (null == result) {
result = new ArrayList();
}
log.debug("结果集大小:" + result.size());
customerCtrl.setTotalSize(aq.getTotalSize());//获得总记录数
customerCtrl.setTotalPages(aq.getTotalPages());//获得总页数
log.debug("总页数大小:" + customerCtrl.getTotalPages());
if (customerCtrl.getCurrentPage() > customerCtrl.getTotalPages()) {
customerCtrl.setCurrentPage(customerCtrl.getTotalPages());//获得当前页码
params.setPageStartNo(customerCtrl.getCurrentPage());
customerCtrl.setParams(params);
AutoQuery aqq = new AutoQuery(params);
aqq.setSession(hibernateSession);
result = aqq.find();
if (null == result) {
result = new ArrayList();
}
customerCtrl.setTotalPages(aqq.getTotalPages());
customerCtrl.setTotalSize(aqq.getTotalSize());
}
ArrayList list = new ArrayList(2);//创建一个数组,传入list[0]为result结果集,list[1]为customerCtrl对象,这里存放了当前条件查找到的记录的条数,页数,当前页数等信息。每页的条数也是在此类配置。
list.add(result);
list.add(customerCtrl);
return list;
}
2、上面的代码实现数据库hibernate查找信息,并且对信息进行分页等,充分利用了hibernage对数据库的良好支持以及灵活性。
下面编写页面的分页处理:
当然页面上的dwr基本使用还是需要遵守的:
A、引入:
<script src='/dwr/interface/ArticleManageDwr.js'></script>
<script src='/dwr/engine.js'></script>
<script src='/dwr/util.js'></script>
B、jsp页面的body体:
<body οnlοad="callArticle()">
<form name="myform">
<input type="hidden" name="id" id="id" value="0"/>
<tr>
<font id="biaoji">添加文章:</font>
<td>Title:</td>
<td><input id="titles" name="titles" type="text"/></td>
<td>Content:</td>
<td><input id="content" name="content" type="text"/></td>
<td>CategoryID:</td>
<td><select id="categoryID">
</select>
</td>
<a href="#" οnclick="verdict()">提交</a>
</tr>
</form>
<font color="red" id="error"></font>
<table width="900" border="1" id="mytable">
<thead>
<tr>
<th>Title</th>
<th>Content</th>
<th>UserName</th>
<th>CategoryName</th>
<th colspan="2">Actions</th>
</tr>
</thead>
<tbody id="tb"></tbody>
<tfoot>
<tr>
<td>
<font id="zongyeshu"></font>
</td>
<td>
<input type="button" οnclick="firstPage(callArticle);" value="首页" />
</td>
<td>
<input type="button" οnclick="superiorPage(callArticle)" value="上一页" />
</td>
<td>
<input type="button" οnclick="nextPage(callArticle)" value="下一页" />
</td>
<td>
<input type="button" οnclick="lastPage(callArticle)" value="末页" />
</td>
<td>
<input id="dangqian" maxlength="2"/>
</td>
</tr>
</tfoot>
</table>
</body>
C、javascript实现分页的控制代码:
var page = 1;
var pageSize;//每页行数
var currentPage;//当前页码
var totalPages;//总页数
var totalSize;//总记录数
/*获得当前查询所返回的信息列表:记录数,页数,每页行数==*/
function getPageController(data) {
pageSize = data.pageSize;
currentPage = data.currentPage;
totalPages = data.totalPages;
totalSize = data.totalSize;
}
/*返回首页*/
function firstPage(info) {
page = 1;
info();
}
/*返回上页*/
function superiorPage(info) {
if (page > 1) {
page = page - 1;
}
info();
}
/*返回下页*/
function nextPage(info) {
if (page < totalPages)
page = page - 1 + 2;
else page = totalPages;
info();
}
function lastPage(info){
page = totalPages;
info();
}
D、javascript的综合查询部分:
var map = DWRUtil.getValues("myform");
/*-------------------------------------------------查询用户列表,并且创建表格方法-------------------------------------------------*/
var getTitles = function(data) {
return data.titles;
};
var getContent = function(data) {
return data.content;
};
var getUser = function(data) {
return data.userName;
};
var getCategory = function(data) {
return data.categoryName;
};
var getEdit = function(data) {
return document.createElement('<input type="button" value="Edit" οnclick="readPerson(' + data.id + ')"/>');
};
var getDelete = function(data) {
return document.createElement('<input type="button" value="Delete" οnclick="del(' + data.id + ')"/>');
};
function callArticle()
{
DWRUtil.useLoadingMessage("login......");
ArticleManageDwr.select(page,map,callreturn); //查询文章信息回调 callreturn函数
}
function callreturn(data)
{
DWRUtil.removeAllRows("tb");
DWRUtil.addRows("tb", data[0], [getTitles,getContent,getUser,getCategory,getEdit,getDelete]);
getPageController(data[1]);
$("dangqian").value = "当前第"+page+"页";
$("zongyeshu").innerHTML = "共计"+totalPages+"页"+totalSize+"条记录";
}
以上测试通过,可以放心使用,此代码完全基于hibernate的分页技术,相信hibernate相信dwr。
最后
以上就是细腻豆芽为你收集整理的dwr+hibernate实现的分页技术的全部内容,希望文章能够帮你解决dwr+hibernate实现的分页技术所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复