ssm+maven项目使用PageHelper分页

2020-05-11  本文已影响0人  目光下的暮光

1 导入PageHelper依赖

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

2 在mybatis配置文件中添加下列代码

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

3 编写相关代码
service

 /**
     * 分页显示
     *
     * @param pageIndex 第几页开始
     * @param pageSize  一页显示多少
     * @param notice    查询条件
     * @return 文章列表
     */
    PageInfo<CptNotice> pageNotice(Integer pageIndex, Integer pageSize, CptNotice notice);

serviceImpl

 @Override
    public PageInfo<CptNotice> pageNotice(Integer pageIndex, Integer pageSize, CptNotice notice) {
        PageHelper.startPage(pageIndex, pageSize);
        List<CptNotice> cptNoticeList = cptNoticeDao.selectAll(notice);
        return new PageInfo<>(cptNoticeList);
    }

4 在controller中调用分页代码

 /**
     * 
     * @param pageIndex 页码
     * @param pageSize 显示几条数据
     * @param modelMap
     * @param cptNotice 查询条件
     * @return
     */
    @RequestMapping("/more")
    public String moreMsg(@RequestParam(required = false, defaultValue = "1") Integer pageIndex,
                          @RequestParam(required = false, defaultValue = "8") Integer pageSize,ModelMap modelMap,CptNotice cptNotice) {
        cptNotice.setStatus("2");
        PageInfo<CptNotice>  cptNoticeList= noticeService.pageNotice(pageIndex, pageSize, cptNotice);
        modelMap.addAttribute("pageInfo",cptNoticeList);
        modelMap.addAttribute("category",cptNotice.getCategory());
        return "home/list";
    }

5 jsp中分页代码

 <%--分页--%>
            <ul class="pagination">
                <li class="page-item"><a class="page-link" href="/more?pageIndex=1&category=${category}">首页</a></li>
                <c:if test="${pageInfo.hasPreviousPage}">
                    <li class="page-item"><a class="page-link"
                                             href="/more?pageIndex=${pageInfo.pageNum-1}&category=${category}">上一页</a></li>
                </c:if>
                <c:forEach items="${pageInfo.navigatepageNums}" var="page">
                    <c:if test="${page==pageInfo.pageNum}">
                        <li class="page-item active"><a class="page-link" href="#">${page}</a></li>
                    </c:if>
                    <c:if test="${page!=pageInfo.pageNum}">
                        <li class="page-item"><a class="page-link"
                                                 href="/more?pageIndex=${page}&category=${category}">${page}</a></li>
                    </c:if>
                </c:forEach>
                <c:if test="${pageInfo.hasNextPage}">
                    <li class="page-item"><a class="page-link"
                                             href="/more?pageIndex=${pageInfo.pageNum+1}&category=${category}">下一页</a></li>
                </c:if>
                <li class="page-item"><a class="page-link" href="/more?pageIndex=${pageInfo.pages}&category=${category}">末页</a>
                </li>
            </ul>

大功告成 如图


image.png
上一篇下一篇

猜你喜欢

热点阅读