• mybatis分页


    一、sql语句分页:

    在mapper.xml文件添加sql语句

    <select id="getEmpByLimit" parameterType="map" resultType="Emp">
        select * from emp order by empno limit #{startIndex},#{pageSize} 
    select>
    
    • 1
    • 2
    • 3

    在接口中添加方法:

    List<Emp> getEmpByLimit(Map<String, Integer> map);
    
    • 1

    在测试类中添加:

            int startIndex= (currentPage - 1)* pageSize; //根据 当前页 计算起始位置
            Map<String, Integer> map = new HashMap<String, Integer>();
            map.put("startIndex",startIndex);//起始位置
            map.put("pageSize",pageSize);//每页条数
    
            List<Emp> empList = mapper.getEmpByLimit(map);
            for (Emp emp : empList) {
                System.out.println(emp);
            }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    二、RowBounds方式:

    在mapper.xml文件添加sql语句

    <select id="getEmpByBounds" resultType="Emp">
        select * from emp order by empno 
    select>
    
    • 1
    • 2
    • 3

    在接口中添加方法:

    List<Emp> getEmpByBounds(RowBounds rowBounds);
    
    • 1

    在测试类中添加:

             int n = 1; //表示从第几页数据开始
             int pageSize  = 5;//连续取出几条数据 
             int start = (n - 1)* pageSize; //n 当前页 
             RowBounds rowBounds = new RowBounds(start, pageSize );
             List<Emp> list = mapper.getEmpByBounds(rowBounds); 
             for (Emp user : list) {
                 System.out.println(user);
             } 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    三、PageHelper

    pom文件中添加依赖

            <dependency>
                <groupId>com.github.pagehelpergroupId>
                <artifactId>pagehelperartifactId>
                <version>4.1.6version>
            dependency>
            <dependency>
                <groupId>com.github.jsqlparsergroupId>
                <artifactId>jsqlparserartifactId>
                <version>0.9.5version>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    mybatis配置文件中添加插件

    
        <plugins>
            <plugin interceptor="com.github.pagehelper.PageHelper">
               
            plugin>
        plugins>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在mapper.xml文件添加sql语句

    <select id="getEmpByPageHelper" resultType="Emp">
        select * from emp order by empno 
    select>
    
    • 1
    • 2
    • 3

    在接口中添加方法:

    List<Emp> getEmpByPageHelper();
    
    • 1

    在测试类中添加:

            Page<Object> startPage = PageHelper.startPage(1, 5);
            List<Emp> empList = mapper.getEmpByPageHelper();
            for (Emp emp : empList) {
                System.out.println(emp);
            }
            System.out.println("总条数"+startPage.getTotal());
            System.out.println("当前页码"+startPage.getPageNum());
            System.out.println("每页的记录数"+startPage.getPageSize());
            System.out.println("总页码"+startPage.getPages());
                     
          //用PageInfo对结果进行包装
            PageInfo<Emp> page = new PageInfo<Emp>(empList);
            //测试PageInfo全部属性
            //PageInfo包含了非常全面的分页属性
            System.out.println("当前页码"+ page.getPageNum()) ; //
            System.out.println("每页的记录数"+ page.getPageSize()) ;  //
            System.out.println("起始行"+ page.getStartRow())  ;  //
            System.out.println("尾行"+ page.getEndRow() ) ;   //
            System.out.println("总条数"+ page.getTotal() ) ;//
            System.out.println("总页码"+ page.getPages() ) ; //
            System.out.println("第一页"+ page.getFirstPage()) ;    //
            System.out.println("最后一页"+ page.getLastPage() ) ;  //
            System.out.println("是第一页"+ page.isIsFirstPage() ) ; //
            System.out.println("是最后一页"+ page.isIsLastPage() ) ;  //
            System.out.println("有上一页"+ page.isHasPreviousPage() ) ; //
            System.out.println("有下一页"+ page.isHasNextPage())  ; //
            
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    四、springboot+mybatis分页

    pom文件中添加依赖

    
    <dependency>
        <groupId>com.github.pagehelpergroupId>
        <artifactId>pagehelper-spring-boot-starterartifactId>
        <version>1.3.1version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在文件中加:

    spring:
       datasource:
         username: root
         password: root
         url: jdbc:mysql://localhost:3306/elm?characterEncoding=utf-8
         driver-class-name: com.mysql.jdbc.Driver
    
    pagehelper:
      helper-dialect: mysql
      reasonable: true
      support-methods-arguments: true
      params: count=countSql
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    在controller中

    PageHelper.startPage(1, 1);
    
    • 1
  • 相关阅读:
    华为云云耀云服务器L实例评测|StackEdit中文版在线Markdown笔记工具
    cpu设计和实现(取指)
    在colab上训练YOLOv5
    c入门第二十四篇: 学生成绩管理系统优化(可执行文件传参)
    WebShell后门检测与WebShell箱子反杀
    java计算机毕业设计幼儿早教系统软件设计与实现MyBatis+系统+LW文档+源码+调试部署
    Tomcat:部署及优化
    SQL 窗口函数
    Leetcode 2850. Minimum Moves to Spread Stones Over Grid
    持续集成Jenkins安装部署
  • 原文地址:https://blog.csdn.net/weixin_45778311/article/details/134561619