• ssm分页实战


    1. 插件

    1. maven

    1. <!-- 分页插件 -->
    2. <dependency>
    3. <groupId>com.github.pagehelper</groupId>
    4. <artifactId>pagehelper</artifactId>
    5. <version>5.2.0</version>
    6. </dependency>

     3. mybaits核心配置文件中或spring配置文件中设置plugins

    mybatis-config.xml

    1. <plugins>
    2. <plugin interceptor="com.github.pagehelper.PageInterceptor">plugin>
    3. plugins>

    springxml

    1. <property name="plugins">
    2. <array>
    3. <bean class="com.github.pagehelper.PageInterceptor">bean>
    4. array>
    5. property>

    2. 后端

    1. service层

     PageHelper.startPage(pageNum, 3);开启分页pageNum当前页 ,3 每页数据数

     List empsList = empMapper.getEmpsList();获取全部list

     PageInfo page = new PageInfo<>(empsList, 3);分页相关数据,3 导航页码数

    1. @Override
    2. public PageInfo getEmpPage(Integer pageNum) {
    3. PageHelper.startPage(pageNum, 3);
    4. List empsList = empMapper.getEmpsList();
    5. //获取分页相关数据
    6. PageInfo page = new PageInfo<>(empsList, 3);
    7. return page;
    8. }

    2. controller层

    路径: @RequestMapping(value = "emplist/page/{pageNum}")pageNum当前页是从前端传过来的。

     PageInfo pageInfo = empService.getEmpPage(pageNum);获取分页相关信息

    包括:分页相关信息以及数据库数据,通过model传到前端

    PageInfo{

            pageNum=1, pageSize=3, size=3, startRow=1, endRow=3, total=18, pages=6,

            list=Page {

                            count=true, pageNum=1, pageSize=3, startRow=0, endRow=3, total=18,                        pages=6, reasonable=false, pageSizeZero=false

                            }

             [

                    Emp(empId=1, empName=张三, age=12,  gender=男, email=1),

                    Emp(empId=2, empName=李四, age=34, gender=男, email=2),        

                    Emp(empId=4, empName=张涛, age=24, gender=男, email=2)],

            prePage=0, nextPage=2, isFirstPage=true, isLastPage=false, hasPreviousPage=false,         hasNextPage=true, navigatePages=3, navigateFirstPage=1, navigateLastPage=3,         navigatepageNums=[1, 2, 3]

    }

    1. /**
    2. *
    3. * 分页查询
    4. */
    5. @RequestMapping(value = "emplist/page/{pageNum}" , method = RequestMethod.GET)
    6. public String getEmpByPageNum(@PathVariable("pageNum")Integer pageNum, Model model){
    7. PageInfo pageInfo = empService.getEmpPage(pageNum);
    8. model.addAttribute("page", pageInfo);
    9. System.out.println(pageInfo);
    10. return "emplist";
    11. }

    3. 前端

    实现效果

    首页pageNum=1

    非首页、末页

    末页

     

    实现代码:
    注意路径拼接和导航页码判断

    1. <a th:if="${page.hasPreviousPage}" th:href="@{/emplist/page/1}">首页a>
    2. <a th:if="${page.hasPreviousPage}" th:href="@{'/emplist/page/'+${page.prePage}}">上一页a>
    3. <span th:each="nn:${page.navigatepageNums}">
    4. <a th:if="${page.pageNum == nn}" style="color: red;" th:href="@{'/emplist/page/'+${nn}}" th:text="'['+${nn}+']'">a>
    5. <a th:if="${page.pageNum != nn}" th:href="@{'/emplist/page/'+${nn}}" th:text="${nn}">a>
    6. span>
    7. <a th:if="${page.hasNextPage}" th:href="@{'/emplist/page/'+${page.nextPage}}">下一页a>
    8. <a th:if="${page.hasNextPage}" th:href="@{'/emplist/page/'+${page.pages}}">末页a>

     

  • 相关阅读:
    基数排序(学习)
    美赞臣EDI 940仓库装运订单详解
    《VS2013+ Qt5.6 创建Qt对话框(*.ui 文件, *.h, *.cpp )》
    RPA机器人维护的3个注意点
    在Ferora35中安装oracle-database-xe-21c
    python+django中小学课外知识在线学习网站pycharm
    PyCharm:2023新版PyCharm无UI工具栏,如何回旧版
    grafana-reporter中文乱码问题
    HTB-Valentine
    MySQL数据库管理
  • 原文地址:https://blog.csdn.net/qq_41950447/article/details/128032182