1.web.xml导入配置信息
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.0</version>
</dependency>
<!-- spring boot可以直接用这个
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency> -->
2.SQL用最简单的查询
<select id="getDemandList" parameterType="com.zte.saltportal.model.DemandQueryCriteria" resultType="com.zte.saltportal.model.DemandQueryCriteria">
SELECT * from progress_info where progresstype = 2 and relid = #{id}
</select>
3.dao层直接查询数据
List<DemandListBean> getDemandList(DemandQueryCriteria getDemandList);
4.service中的接口getDemandList 条件getDemandList 页数pageNum 每页条数显示pageSize
public PageInfo<Students> getDemandList(DemandQueryCriteria getDemandList,Integer pageNum,Integer pageSize);
5.在service的实现类中加上 PageHelper.startPage(pageNum,pageSize);这句代码,然后把查询出来的集合放进 PageInfo 中并返回
.public PageInfo<Students> getDemandList(DemandQueryCriteria getDemandList,Integer pageNum,Integer pageSize) {
PageHelper.startPage(pageNum,pageSize);
List<Students> list = XDao.getDemandList(getDemandList);
return new PageInfo<Students>(list);
}
6.controller层接收返回结果,在这里也需要添加上 pageNum,pageSize 这两个参数,但是在这里可以通过 @RequestParam() 注解设置这两个参数不是必须的并且修改默认值,在函数里面把从service层接收的 PageInfo 集合存到request作用域中:
@ResponseBody
@RequestMapping(value="/demand/queryDemandList", method = RequestMethod.POST)
public PageInfo<DemandListBean> queryList(DemandQueryCriteria demandQueryCriteria,Model model)
{
//根据查询条件查询客户列表
PageInfo<DemandListBean> page=null;
try{
//条数
Integer pageSize=0;
//页数
Integer currentPage=0;
if(demandQueryCriteria.getPageSize()==null||demandQueryCriteria.getPageSize()<0){
pageSize=10;
}else{
pageSize=demandQueryCriteria.getPageSize();
}
if(demandQueryCriteria.getCurrentPage()==null||demandQueryCriteria.getCurrentPage()<0){
currentPage=1;
}else{
currentPage=demandQueryCriteria.getCurrentPage();
}
page= demandService.getPageDemand(demandQueryCriteria,currentPage,pageSize);
System.out.println("page:"+page.getPageSize());
//model.addAttribute("page", page);
} catch (Exception e) {
e.printStackTrace();
}
return page;
}
7.页面接收
由于我用的是js,所ajax返回data后
function queryDemand(){
//把条件参听过json传输格式传给后台,后台用对象接
var pageSize = $("#pageSize").val();
var currentPage = $("#currentPage").val();
$.ajax({
type: 'POST',
async: false,
data : {
pageSize : pageSize,
currentPage:currentPage
},
url: '/saltportal/demand/queryDemandList',
dataType: "json",
success: function(data){
var var pageinfo =data;//去接返回值
if (pageinfo.pageNum == 1) {
htm+='<li class="paginate_button previous disabled"> <a href="#10000">首页</a> </li>';
htm+='<li class="paginate_button previous disabled"> <a href="#10000">上一页</a></li>';
}else if(pageinfo.pageNum >1){
htm+='<li class="paginate_button previous"> <a href="#" οnclick="_go(1);">首页</a></li>';
htm+='<li class="paginate_button previous"> <a href="#" οnclick="_go('+(pageinfo.pageNum-1)+');">上一页</a></li>'
}
if (pageinfo.pageNum == pageinfo.pages) {
htm+='<li class="paginate_button next disabled"> <a href="#10000">下一页</a></li>';
htm+='<li class="paginate_button next disabled"> <a href="#10000">尾页</a></li>';
}else if(pageinfo.pageNum < pageinfo.pages){
htm+='<li class="paginate_button next" id="example2_next"><a href="#" οnclick="_go('+(pageinfo.pageNum+1)+');">下一页</a></li>';
htm+='<li class="paginate_button next" id="example2_next"><a href="#" οnclick="_go('+pageinfo.pages+');">尾页</a></li>';
}
},error:function(){
alert("返回失败!");
}
*8.在前端页面接收传过来的集合遍历输出,这里要注意的一点是,如果是遍历pageInfo类型的集合,要使用它里面的自带的 .list 这个属性来代表集合然后遍历输出
<table>
<div style="width:100%; text-align:center;">
<span style="width:100%;color:red;font-sie:12px;text-align:center;">没有查询到符合条件的数据</span>
</div>
<div>
<div class="col-sm-1" style="padding:8px 0px 0px 8px">
<select name="numperpage" id="numperpage" class="form-control input-sm" style="height:26px" οnchange="_go(1)">
<option value="10">10</option>
<option value="25">25</option>
<option value="50">50</option>
<option value="100">100</option>
</select>
</div>
<div class="col-sm-4" style="padding:0px 0px 0px 10px">
<div style="word-wrap: break-word; overflow: auto; padding:8px 0px 8px 0px">
<span>共</span><span>5</span><span>条;</span>
<span>到</span><span><input type=text name="pageindex" id="pageindex" style="width:35px" th:value="2"></span><span>页</span>
<span><a οnclick="doGoPage();" style="cursor:pointer;">GO</a></span>
<span>共</span><span>5</span><span>页;</span>
</div>
</div>
<div class="col-sm-7" style="padding:0px 10px 0px 0px">
<div class="dataTables_paginate paging_simple_numbers">
<ul class="pagination">
<li class="paginate_button previous"> <a href="#10000">首页</a> </li>
<li class="paginate_button previous"> <a href="#10000">上一页</a></li>
<li class="paginate_button next"> <a href="#10000">下一页</a></li>
<li class="paginate_button next"> <a href="#10000">尾页</a></li>
</ul>
</div>
</div>
</div>
</table>
对于上一页和下一页
<ul class="pagination">
<li class="paginate_button previous disabled"> <a href="#10000">首页</a> </li>
<li class="paginate_button previous disabled"> <a href="#10000">上一页</a></li>
<li class="paginate_button previous"> <a href="#" οnclick="_go(1);">首页</a></li>
<li class="paginate_button previous"> <a οnclick="_go('+pageinfo.pageNum-1+');"> 上一页</a> </li>
<li class="paginate_button next disabled"> <a href="#10000">下一页</a></li>
<li class="paginate_button next disabled"> <a href="#10000">尾页</a></li>
<li class="paginate_button next" id="example2_next"><a href="#" οnclick="_go('+(pageinfo.pageNum+1)+');">下一页</a></li>
<li class="paginate_button next" id="example2_next"><a href="#" οnclick="_go('+pageinfo.pages+');">尾页</a></li>
</ul>