SSM整合的时候使用分页助手 PageHelper

第一步:
加载文件

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

第二步:配置文件里面配置东西
在Dao层的Spring 文件里面 ApplicationContext.xml
在在各分配置文件的工厂里面加载东西

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 传入PageHelper的插件 -->
        <property name="plugins">
            <array>
                 <!--传入插件的对象-->
                <bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <props>
                            <prop key="helperDialect">mysql</prop>
                            <prop key="reasonable">true</prop>
                        </props>
                    </property>
                </bean>
            </array>
        </property>
    </bean>

第三步:在Service层 使用以及在Controller调用

Dao层:


    //    写一个查询所有的评论
    @Select("select * from carcomment")
    List<CarComment> findAll();

Service层:

    //    写一个查询所有的评论 分页
    List<CarComment> findAll(int page,int size);



ServiceImpl层:
    @Autowired
    CarCommentDao carCommentDao;

    /**
     * 查询出所有用户的评论的列表
     * @return
     */
    public List<CarComment> findAll(int page,int size) {
        //添加分页
        PageHelper.startPage(page, size);
        return carCommentDao.findAll();
    }

Contril层:


    //写一个使用分页
    @RequestMapping("/findAll")
    public ModelAndView findCarComment(@RequestParam(name = "page",required = true,defaultValue = "1") int page,@RequestParam(name = "size",required = true,defaultValue = "5") int size) {

        List<CarComment> commentList = carCommentService.findAll(page,size);
        PageInfo pageInfo = new PageInfo(commentList);
//        System.out.println(commentList);
        mv.addObject("pageInfo", pageInfo);
        mv.setViewName("Evaluation-list");
        return mv;
    }

页面实现:

实现遍历里面的内容

<c:forEach items="${pageInfo.list}" var="comment" varStatus="counts">

										<tr>
											<td><input name="ids" type="checkbox"></td>
											<td>${counts.count }</td>
											<td>${comment.username }</td>
											<td>${comment.date }</td>
											<td>${comment.userEvaluation }</td>
											<td>${comment.user_Headportrait}</td>
											<td class="text-center">
												<button type="button" class="btn bg-olive btn-xs">订单</button>
												<button type="button" class="btn bg-olive btn-xs">详情</button>
												<button type="button" class="btn bg-olive btn-xs">编辑</button>
											</td>
										</tr>
									</c:forEach>

实现页面的总页数 以及编列页数

<div class="box-tools pull-right">
							<ul class="pagination">
								<li><a href="${pageContext.request.contextPath}/carComment/findAll.do?page=1&size=${pageInfo.pageSize}" aria-label="Previous">首页</a></li>
								<li><a href="${pageContext.request.contextPath}/carComment/findAll.do?page=${pageInfo.prePage}&size=${pageInfo.pageSize}">上一页</a></li>
								<c:forEach begin="1" end="${pageInfo.pages}" var="i">
								<li><a href="${pageContext.request.contextPath}/carComment/findAll.do?page=${i}&size=${pageInfo.pageSize}">${i}</a></li>
								</c:forEach>

								<li><a href="${pageContext.request.contextPath}/carComment/findAll.do?page=${pageInfo.nextPage}&size=${pageInfo.pageSize}">下一页</a></li>
								<li><a href="${pageContext.request.contextPath}/carComment/findAll.do?page=${pageInfo.pages}&size=${pageInfo.pageSize}" aria-label="Next">尾页</a></li>
							</ul>
						</div>

实现页面自选每页的页数:

					<div class="box-footer">
						<div class="pull-left">
							<div class="form-group form-inline">
								总共${pageInfo.pages}页,共${pageInfo.total}条数据。 每页 <select class="form-control" id="changePageSize" onchange="changePageSize()">
									<option>1</option>
									<option>2</option>
									<option>3</option>
									<option>4</option>
									<option>5</option>
								</select> 条
							</div>
						</div>


	<script>

         function changePageSize() {
             var pagesize = $("#changePageSize").val();
             location.href = "${pageContext.request.contextPath}/carComment/findAll.do?page=${pageInfo.pages}&size=" +pagesize;
         }
         </script>

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值