使用mybatis分頁插件--PageHelper實現分頁效果

很久沒有寫博客了,最近都在忙着寫一個網站項目,今天做一個分頁功能的時候,遇到了分頁效果實現不了的問題,查了好久的資料,後來終於是成功解決啦,記錄一下~
 
 
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 }">修改</a>
										<a class="link-del" href="<%=basePath%>user/MediaList">進入視頻列表</a>
                                        <a class="link-del" href="javascript:del('${media.id }')">刪除視頻</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">首頁</a></li>
									<c:if test="${pageInfo.hasPreviousPage }">
										<li>
											<a href="<%=basePath%>user/ShowMedia?pn=${pageInfo.pageNum-1}" aria-label="Previous">
												<span aria-hidden="true">&laquo;</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="#">${ page_Num}</a></li>
										</c:if>
										<c:if test="${page_Num != pageInfo.pageNum }">
											<li><a href="<%=basePath%>user/ShowMedia?pn=${ page_Num}">${ page_Num}</a></li>
										</c:if>
									</c:forEach>
									<c:if test="${pageInfo.hasNextPage }">
										<li>
											<a href="<%=basePath%>user/ShowMedia?pn=${pageInfo.pageNum+1}" aria-label="Next">
												<span aria-hidden="true">&raquo;</span>
											</a>
										</li>
									</c:if>
									<li><a href="<%=basePath%>user/ShowMedia?pn=${pageInfo.pages}">末頁</a></li>
								</ul>
							</nav>
						</div>
					</div>

 
 
效果實現如下:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章