• 使用mybatis_plus快速实现分页插件


    1.分页插件

    MyBatis Plus自带分页插件,只要简单的配置即可实现分页功能

    • 添加配置类MyBatisPlusConfig

      @Configuration
      @MapperScan("com.atguigu.mybatisplus.mapper")
      public class MyBatisPlusConfig {
          @Bean
          public MybatisPlusInterceptor mybatisPlusInterceptor(){
              MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
              //添加分页插件
              interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
              return interceptor;
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
    • 编写测试方法

      获取下面这些就足够了,提交到前端,前端会自己生成有关导航页的信息

      如果需要后端,查阅mybatis

      @Test
      public void testPage(){
          //new Page()中的两个参数分别是当前页码,每页显示数量
          Page<User> page = new Page<>(1 , 3);
          //null 表示全部都选
          userMapper.selectPage(page ,  null);
          
      	//结果page最后也会被封装结果
          long current = page.getCurrent(); 		//页码
          long size = page.getSize();      		//页容量
          List<User> records = page.getRecord();	//当前页的数据
          long total = page.getTotal();           //总条数
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
    • 前端接口

      /* 
      需求说明
          查询全部数据页数据
      请求uri
          schedule/{pageSize}/{currentPage}
      请求方式 
          get   
      响应的json
          {
              "code":200,
              "flag":true,
              "data":{
                  //本页数据
                  data:
                  [
                  {id:1,title:'学习java',completed:true},
                  {id:2,title:'学习html',completed:true},
                  {id:3,title:'学习css',completed:true},
                  {id:4,title:'学习js',completed:true},
                  {id:5,title:'学习vue',completed:true}
                  ], 
                  //分页参数
                  pageSize:5, // 每页数据条数 页大小
                  total:100 ,   // 总记录数
                  currentPage:1 // 当前页码
              }
          }
      */
      
      • 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
      • 28

    2.自定义分页

    查询的条件不一样

    • UserMapper接口中定义一个方法

      /**
        * 根据年龄查询用户列表,分页显示 
        * @param page 分页对象,xml中可以从里面进行取值,传递参数 Page 即自动分页,必须放在第一位 
        * @param age 年龄 
        * @return 
        */
      Page<User> selectPageVo(@Param("page") Page<User> page,@Param("age") Integer age);
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
    • UserMapper.xml中编写SQL实现该方法

      <select id="selectPageVo" resultType="User">
          select id,username as name,age,email from t_user where age > #{age}
      select>
      
      • 1
      • 2
      • 3
    • 编写测试方法

      @Test
      public void testPageVo(){
          Page<User> page = new Page<User>(1 ,2);
          userMapper.selectPageVo(page , 20);
       
          
          //结果page最后也会被封装结果
          long current = page.getCurrent(); 		//页码
          long size = page.getSize();      		//页容量
          List<User> records = page.getRecord();	//当前页的数据
          long total = page.getTotal();           //总条数
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
  • 相关阅读:
    表单元素——下拉列表
    C#MessageBox的使用
    .NET7 gRPC JSON转码+OpenAPI
    c++ 中 auto, auto & 和 const auto & 的区别
    DarkGate恶意软件通过消息服务传播
    工业设计|设计就是生活
    shell是什么?ssh 与 git bash linux或cmd与 shell区别
    docker-compose部署mysql5.7主从
    前端面试之 vue 篇
    H264码流中SPS PPS详解
  • 原文地址:https://blog.csdn.net/Lan_lianhua/article/details/132631641