用PageHelper实现分页效果

1.在pom中添加如下代码。

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

2.我使用的是SSM框架所以要在配置文件中添加相关配置。

2.1 applicationContext.xml


    <!--分页-->
    <bean id="pageHelper" class="com.github.pagehelper.PageHelper"/>

2.2 mybatis-config.xml


    <!--分页-->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor">
        </plugin>
    </plugins>

3.现在控制类中能引用PageHelper了

3.1先添加个注解


    @Autowired
    PageHelper pageHelper;

3.2 上代码

 @RequestMapping("/examAdminIndex")
    public String examAdminIndex(@RequestParam(value="pn",defaultValue="1") Integer pn, HttpSession session) {
        int pageSize=6;/*每页的有几条数据*/
        pageHelper.startPage(pn, pageSize);
        List<Subject> subjects=examService.findAllSubject();/*查询的数据*/

       
        PageInfo pageInfo=new PageInfo(subjects);
        
        session.setAttribute("PageInfo",pageInfo);
       
        return "adminIndex/examAdminIndex";
    }

4.jsp页面,前后端交互用session

4.1控制分页的跳转

<script type="text/javascript">
    $(function(){//控制分页的跳转
        $("#li"+${sessionScope.PageInfo.pageNum}).addClass("active");
        $("#previous").click(function(){
            if(${sessionScope.PageInfo.isFirstPage}){
                alert("当前为第一页,没有上一页了");
                return false;
            }
        });
        $("#next").click(function(){
            if(${sessionScope.PageInfo.isLastPage}){
                alert("当前为最后一页,没有下一页了");
                return false;
            }
        });
    });
</script>

4.2底部的页面跳转栏

 <div id="div2">总共有${sessionScope.PageInfo.pages}${sessionScope.PageInfo.total}条记录,当前为第${sessionScope.PageInfo.pageNum}</div>

        <div id="div3">
            <nav aria-label="...">
                <ul class="pagination">
                    <li ><a href="/examAdminIndex?pn=${PageInfo.pageNum-1}" aria-label="Previous" id="previous"><span aria-hidden="true">«</span></a></li>
                    <li><a href="/examAdminIndex">首页</a></li>

                    <c:forEach begin="1" end="${PageInfo.pages}" var="id">
                        <li id="li${id}"><a href="/examAdminIndex?pn=${id}">${id}</a></li>
                    </c:forEach>
                    <li><a href="/examAdminIndex?pn=${PageInfo.pages}">尾页</a></li>

                    <li><a href="/examAdminIndex?pn=${PageInfo.pageNum+1}" aria-label="Next"><span aria-hidden="true" id="next">»</span></a></li>
                </ul>
            </nav>
        </div>

    </div>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值