java实现分页功能

1.通过maven添加jar包

<!-- 加入分页 需要的jar包-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.1.2</version>
        </dependency>

2.在myBatis里添加插件

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

3.修改service层
3.1修改接口
把List改成PageInfo,并添加参数

public PageInfo<Tables> selectInformationById(int userId,int pageIndex,int pageSize);

3.2修改实现类

//改成分页查询
    @Override
    public PageInfo<Tables> selectInformationById(int userId,int pageIndex,int pageSize) {
        //设置开始查询的位置 以及查询多少条记录
        PageHelper.startPage(pageIndex,pageSize);

        //查询当前页数据
        List<Tables> tables=tablesMapper.selectInformationById(userId);

        //组装成pageInfo 返回
        PageInfo<Tables> pageInfo=new PageInfo<Tables>(tables);

        return pageInfo;

    }

4.修改控制层

 @RequestMapping("/selectInformationById")
    public ModelAndView selectInformationById(@RequestParam(name = "pageIndex",defaultValue = "1")int pageIndex, HttpServletRequest request){
        HttpSession session = request.getSession();
        if (session.getAttribute("loginUser")!=null){
            Users users=(Users) session.getAttribute("loginUser");
            int userId = users.getUserId();

            PageInfo<Tables> pageInfo = tablesService.selectInformationById(userId,pageIndex,2);

            System.out.println(pageInfo);
            //转发到table页面
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.addObject("pageInfo",pageInfo);
            modelAndView.addObject("userId",userId);
            modelAndView.setViewName("table");
            return modelAndView;
        }else {
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.setViewName("redirect:/users/loginUi");
            return modelAndView;
        }

    }

5.修改前端页面
修改页面显示,变成pageinfo

<tr th:each="table:${pageInfo.getList()}">
                                <td><input type="checkbox" th:text="${table.courseId}"></td>
                                <td th:text="${table.courseName}">1231</td>
                                <td th:text="${table.teacherName}">23123</td>
                                <td th:text="${table.courseTime}">12312</td>
                                <td>
                                    <div class="operate">
                                        <a href="#">查看详情</a>
                                    </div>
                                </td>
                            </tr>

修改页码

<ul>
                    <li class="first"><a href="#">首页</a></li>
                    <li class="pre_page"><a href="#">上一页</a></li>

                    <li th:each="pageIndex:${pageInfo.navigatepageNums}" class=""><a href="#" th:href="@{/tables/selectInformationById(pageIndex=${pageIndex},userId=${userId})}" th:text="${pageIndex}">1</a></li>

                    <li class="pre_page"><a href="#">下一页</a></li>
                    <li class="last"><a href="#">尾页</a></li>
                </ul>

over!!!

更多推荐

java实现分页功能