您的位置:首页 > Web前端 > JavaScript

Mybatis的插件 PageHelper分页以及jsp实现代码

2018-05-24 21:58 369 查看
版权声明: https://blog.csdn.net/weixin_41253479/article/details/80441328

一。准备工作

1.导入pom依赖

<!-- 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.0.2</version>

</dependency>

2. mybatis-config.xml  配置插件

    <plugins>  
            <!-- com.github.pagehelper为PageHelper类所在包名 -->  
           <plugin interceptor="com.github.pagehelper.PageInterceptor">  
           <!--<!–分页参数合理化  –>-->  
           <property name="reasonable" value="true"/>  
           </plugin> 
    </plugins>

Mybatis的插件 PageHelper 分页查询使用方法

/**
* 查询全部的房间
*/   
@RequestMapping("/room/list")
public String list(Model model,WdHtRoom wdHtRoom,Integer currentPage,Integer rowsOfpage)
{
//查询全部的项目
   List<WdHtProject> projectList = wdProjectService.getAll(null);
   model.addAttribute("projectList",projectList);
   
    // 引入分页查询,使用PageHelper分页功能
// 在查询之前传入当前页,然后多少记录
   if(currentPage!=null&&rowsOfpage!=null&&currentPage!=0&&rowsOfpage!=0)
   {
    PageHelper.startPage(currentPage, rowsOfpage);
   }
// startPage后紧跟的这个查询就是分页查询
List<WdHtRoom> list1 = WdRoomServiceImpl.selectAll(wdHtRoom);
// 使用PageInfo包装查询结果,只需要将pageInfo交给页面就可以
PageInfo<WdHtRoom> list = new PageInfo<WdHtRoom>(list1);
model.addAttribute("list", list);
   
   return "room_list";

}



jsp:相应代码

<div style="position: absolute;right: 0px; bottom:0px;">
<c:choose>
<c:when test="${list.size>0}">
<div class="feny">
<div class="manu">
<span>显示${list.startRow}到${list.endRow}共${list.total}条</span>
<input type="hidden" id="pageSize" name="pageSize"> 
<c:if test="${list.pageNum==1}">
    <span>首页</span>
</c:if>
<c:if test="${list.pageNum!=1}">
    <a href="javascript:void(0);onclick=search(1)">首页</a>
</c:if>
<c:choose>
<c:when test="${list.hasPreviousPage}">
<a
href="javascript:void(0);onclick=search(${list.pageNum-1})">上一页
</a>
</c:when>
<c:otherwise>
<span>上一页</span>
</c:otherwise>
</c:choose>
<c:forEach var="item" items="${list.navigatepageNums}">
<c:choose>

<c:when test="${list.pageNum==item}">
<span class="current">${list.pageNum}</span>
</c:when>

<c:otherwise> 
<a href="javascript:void(0);onclick=search(${item})">${item}</a>
</c:otherwise>
</c:choose>
</c:forEach>
<c:choose>
<c:when test="${!list.isLastPage}">
<a
href="javascript:void(0);onclick=search(${list.pageNum+1})">下一页
</a>
<a href="javascript:void(0);onclick=search(${list.lastPage})">尾页</a>
</c:when>
<c:otherwise>
<span>下一页</span>
<span>尾页</span>
</c:otherwise>
</c:choose>
</div>
</div>
</c:when>
<c:otherwise>
<div class="feny">
<span class="emptyData">没有数据可以显示</span>
</div>
</c:otherwise>
</c:choose>

</div>


发送请求的js代码

function search(page)
{
window.location.href 237c0 ="/wanda/room/list.do?currentPage="+page+"&rowsOfpage=15";

}


不足之处:   前台不能选择每页显示几条-----模糊查询时数据丢失

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: