mybatis插件PageHelper分页效果 使用mybatis插件PageHelper实现分页效果

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

mybatis插件PageHelper分页效果 使用mybatis插件PageHelper实现分页效果

wy__kobe   2021-03-30 我要评论

最近都在忙着写一个网站项目,今天做一个分页功能的时候,遇到了分页效果实现不了的问题,查了好久的资料,后来终于是成功解决啦,记录一下

1.在pom.xml中添加分页插件依赖

<dependency>
  <groupId>com.github.pagehelper</groupId>
  <artifactId>pagehelper</artifactId>
  <version>4.1.5</version>
 </dependency>

2.在mybatis配置文件中配置分页插件

这里需要注意的是,如果你的项目有mybatis的配置文件时,添加下面配置:(配置参数可根据需要添加或删除)

<plugins>
 <plugin interceptor="com.github.pagehelper.PageHelper">
  <property name="dialect" value="mysql"/>
  <property name="offsetAsPageNum" value="false"/>
  <property name="rowBoundsWithCount" value="false"/>
  <property name="pageSizeZero" value="true"/>
  <property name="reasonable" value="false"/>
  <property name="supportMethodsArguments" value="false"/>
  <property name="returnPageInfo" value="none"/>
 </plugin>
</plugins>

但如果你的项目没有单独配置mybatis的配置文件,而是把spring和mybatis的配置结合起来的话,这时候你需要引入如下配置信息:

<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <!-- 自动扫描mapping.xml文件 -->
  <property name="mapperLocations" value="classpath:com/wang/web/mapper/*.xml"></property>
  <!-- 配置分页插件 -->
  <property name="plugins">
   <array>
    <bean class="com.github.pagehelper.PageHelper">
     <property name="properties">
      <value>
       dialect=mysql
       reasonable=true
      </value>
     </property>
    </bean>
   </array>
  </property>
 </bean>

3.controller层

//访问所有视频信息查询页面
 /**
  * 分页查询所有视频信息
  * @param pn 默认从第一页开始 请求参数
  * @return
  */
 @RequestMapping("/ShowMedia")
 public String Show(@RequestParam(required = false,value="pn",defaultValue="1")Integer pn, HttpServletRequest request){

  TbMediaExample example = new TbMediaExample();
  //从第一条开始 每页查询五条数据
  PageHelper.startPage(pn, 5);
  List<TbMedia> mediaList = mediaService.selectByExample(example);
  //将用户信息放入PageInfo对象里
  PageInfo pageInfo = new PageInfo(mediaList,5);
  System.out.println(pageInfo.getPages());
  request.setAttribute("pageInfo", pageInfo);
  return "/media";
 }

4.前台

<div class="result-content">
  <table class="result-tab" width="100%">
  <tr>
  <th class="tc" width="5%"><input class="allChoose" name="" type="checkbox"></th>
  <th>排序</th>
  <th>ID</th>
  <th>视频标题</th>
  <th>视频资源</th>
  <th>视频图片</th>
  <th>视频描述</th>
  <th>上传时间</th>
  <th>操作</th>
  </tr>
  <c:if test="${!empty pageInfo.list }">
  <c:forEach items="${pageInfo.list}" var="media">
  <tr>
   <td class="tc"><input name="id[]" value="59" type="checkbox"></td>
   <td>
   <input name="ids[]" value="59" type="hidden">
   <input class="common-input sort-input" name="ord[]" value="0" type="text">
   </td>
   <td align="center">${media.id }</td>
   <td align="center">${media.title }</td>
   <td align="center">${media.src }</td>
   <td align="center">${media.picture }</td>
   <td align="center">${media.descript }</td>
   <td align="center">${media.uptime }</td>
   <td>
   <a class="link-update" href="<%=basePath%>user/MediaUpdate?id=${media.id }" rel="external nofollow" >修改</a>
   <a class="link-del" href="<%=basePath%>user/MediaList" rel="external nofollow" >进入视频列表</a>
          <a class="link-del" href="javascript:del('${media.id }')" rel="external nofollow" >删除视频</a>
   </td>
  </tr>
  </c:forEach>
  </c:if>
  </table>
  <hr style="height:1px;border:none;border-top:1px solid #ccc;" />
  <!-- 分页导航栏 -->

  <!-- 分页信息 -->
  <div class="row">
  <!-- 分页文字信息,其中分页信息都封装在pageInfo中 -->
  <div class="col-md-6">
  当前第:${pageInfo.pageNum}页,总共:${pageInfo.pages}页,总共:${pageInfo.total}条记录
  </div>

  <!-- 分页条 -->
  <div class="col-md-6">
  <nav aria-label="Page navigation">
  <ul class="pagination">
   <li><a href="<%=basePath%>user/ShowMedia?pn=1" rel="external nofollow" >首页</a></li>
   <c:if test="${pageInfo.hasPreviousPage }">
   <li>
   <a href="<%=basePath%>user/ShowMedia?pn=${pageInfo.pageNum-1}" rel="external nofollow" aria-label="Previous">
   <span aria-hidden="true">«</span>
   </a>
   </li>
   </c:if>

   <c:forEach items="${pageInfo.navigatepageNums }" var="page_Num">
   <c:if test="${page_Num == pageInfo.pageNum }">
   <li class="active"><a href="#" rel="external nofollow" >${ page_Num}</a></li>
   </c:if>
   <c:if test="${page_Num != pageInfo.pageNum }">
   <li><a href="<%=basePath%>user/ShowMedia?pn=${ page_Num}" rel="external nofollow" >${ page_Num}</a></li>
   </c:if>
   </c:forEach>
   <c:if test="${pageInfo.hasNextPage }">
   <li>
   <a href="<%=basePath%>user/ShowMedia?pn=${pageInfo.pageNum+1}" rel="external nofollow" aria-label="Next">
   <span aria-hidden="true">»</span>
   </a>
   </li>
   </c:if>
   <li><a href="<%=basePath%>user/ShowMedia?pn=${pageInfo.pages}" rel="external nofollow" >末页</a></li>
  </ul>
  </nav>
 </div>
</div>

效果实现如下:

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们