easyui+spring MVC+Mybatis实现分页查询

来源:互联网 发布:淘宝信用度怎么看 编辑:程序博客网 时间:2024/05/17 02:11
参考网上教程,搞了一天,终于实现了分页!
开始的问题出在没有思路,不知道咋搞,后来后台把数据都调出来了,但是前台就是不变,总是请求两次URL
1、easyui界面代码

$("#tt").datagrid({
height:$("#body").height()-$('#search_area').height()-5,
width:$("#body").width(),
title: '查询结果',
idField:'id',
url:"${basePath}problem.action?method=selectAllProblem",
singleSelect:true, //允许选择多行
nowrap: true, //数据不换行
striped: true,//设置为 true,则把行条纹化。(即奇偶行使用不同背景色)
border: true,
collapsible: true, //是否可折叠
fitColumns:true,
pagination: true, //分页控件
rownumbers:true, //行号
showPageList:false,
singleSelect: false, //允许选择多行
selectOnCheck: true,//true勾选会选择行,false勾选不选择行, 1.3以后有此选项。重点在这里
checkOnSelect: false, //true选择行勾选,false选择行不勾选, 1.3以后有此选项
loadMsg: "正在努力为您加载数据", //加载数据时向用户展示的语句
emptyMsg: 'no records found',
pageSize: 10, //读取分页条数,即向后台读取数据时传过去的值

**注意:pagination设为true有分页控件,但是并不能实现分页
              pageNumber默认为1
              pageSize设为10
后面之所以出现两次请求URL问题,是因为多了如下代码:

//获取页面分页对象
/* var p = $("#tt").datagrid("getPager");
if(p){
$(p).pagination({
onSelectPage:function(pageNumber,pageSize){
var codes = "";
codes += pageNumber;
codes += ("," + pageSize);
$.post("${basePath}problem.action?method=selectAllProblem",{codes:codes},function(data,status){
if(data){
$("#tt").datagrid("reload");
return false;
}
});
},
onRefresh:function(pageNumber,pageSize){
alert("onRefresh");
}
});
} */

事实上,easyui默认传递page参数和rows参数,分别表示当前页码和条数,当点击下一页的按钮时会自动更新请求参数,而一开始我多余地处理了这两个参数,导致总会多请求一次URL,即:触发onSelectPage函数,这也是问题的根源!
2、后台接收参数并处理代码:

@RequestMapping(params="method=selectAllProblem")
@ResponseBody
public Map<String, Object> selectAllProblem(HttpServletRequest request,HttpServletResponse response){
String page = request.getParameter("page");
String rows = request.getParameter("rows");
int currentpage = Integer.parseInt(page);//第几页
int number = Integer.parseInt(rows);//每页显示条数
int start = 0;//每页的开始记录 0-9 10-19 20-29
start = (currentpage - 1) * number;
List<Problem> list = service.selectAllProblem(start, number);
Map<String,Object> jsonMap = new HashMap<String,Object>();
jsonMap.put("total", service.selectAllProblemTotals());//求出总记录数
jsonMap.put("rows", list);//请求页面的记录
return jsonMap;
}

**思路:先获取总条数,再把总条数和每页的数据封装在map中,以json格式交给前台解析。
3、Mybatis对应SQL语句

<!-- 查询所有问题条数 -->
<select id="selectAllProblemTotals" resultType="Integer">
select count(*)
from t_pro;
</select>

<!-- 查询所有-->
<select id="selectAllProblem" resultMap="selectAllProblemResultMap">
select p.pro_id,
p.pro_title,
p.pro_content,
p.pro_file,
p.pro_date,
f.field_name,
p.pro_tel,
p.pro_describtion,
p.pro_checked
from t_pro p
left outer join t_field f
on p.field_id = f.field_id
limit #{0},#{1}
</select>

**limit这个多参数问题,参考我另一篇博客**
到此,分页查询就实现了!