• Mybatis-plus的分页查询


    Mybatis-plus的分页查询

    1. 简单说明

    嗨,大家好!今天给大家分享的是Mybatis-plus 插件的分页机制,说起分页机制,相信我们程序员都不陌生,今天,我就给大家分享一下Mybatis-plus的分页机制,供大家学习和Copy。

    2. 介绍说明

    如果你想看代码,可以直接跳到代码区域,这里只是一些简单的说明,如果你想学习,建议可以看看这一块的任容

    本章节将介绍 BaseMapper 中的分页查询,BaseMapper 接口提供了如下几个分页查询接口:

    • selectPage:根据 entity 条件,查询全部记录
    • selectMapsPage:根据 Wrapper 条件,查询全部记录

    在使用上面两个方法进行分页查询时,我们需要配置分页插件。这是只是在介绍SpringBoot的使用。
    注意:由于我们使用的 Spring Boot 项目,因此需要通过 @Configuration@Bean 注解来添加配置

    3. 完整配置类代码:

    下边就是完整的配置类,至于为什么比官网上的少一点,因为那个可以说会报错,而且也不需要使用到它,以下就是完整配置类:

    package com.hxstrive.mybatis_plus;
     
    import com.baomidou.mybatisplus.annotation.DbType;
    import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
    import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
     
    @Configuration
    public class MybatisPlusConfig {
     
        /**
         * 分页插件。如果你不配置,分页插件将不生效
         */
        @Bean
        public MybatisPlusInterceptor paginationInterceptor() {
            MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
            // 指定数据库方言为 MYSQL
            interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
            return interceptor;
        }
     
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    注意:如果你没有配置分页插件,则不会进行分页。所以这个一定要配置。

    4. 示例代码

    1. 使用 QueryWrapper 和 Page 作为参数进行分页,例如:

      package com.hxstrive.mybatis_plus.select;

      import com.alibaba.fastjson.JSONObject;
      import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
      import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
      import com.hxstrive.mybatis_plus.mapper.SimpleMapper;
      import com.hxstrive.mybatis_plus.model.UserBean;
      import org.junit.jupiter.api.Test;
      import org.junit.runner.RunWith;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.boot.test.context.SpringBootTest;
      import org.springframework.test.context.junit4.SpringRunner;

      @RunWith(SpringRunner.class)
      @SpringBootTest
      class Select6Test {

      @Autowired
      private SimpleMapper simpleMapper;

      @Test
      void contextLoads() {
      QueryWrapper wrapper = new QueryWrapper<>();
      wrapper.isNotNull(“user_id”);

         // 创建分页对象(1表示第一页;4表示每页大小为4)
         Page page = new Page<>(1, 4);
         Page result = simpleMapper.selectPage(page, wrapper);
         System.out.println("page == result: " + (page == result));
         System.out.println("size: " + result.getSize());
         System.out.println("total: " + result.getTotal());
         for(UserBean userBean : result.getRecords()) {
             System.out.println(userBean);
         }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9

      }

      }

    运行上面代码,你会发现 page 和selectPage 返回的 result1 相等,说明两者是同一个对象。因此,可以忽略掉 selectPage 方法的返回结果,如下:

    Page page = new Page<>(1, 4);
    simpleMapper.selectPage(page, wrapper);
    
    • 1
    • 2
    1. 另外一个分页方法,selectMapsPage 和上面的使用方法一样,仅仅是返回类型不一样。代码如下:

      package com.hxstrive.mybatis_plus.select;

      import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
      import com.hxstrive.mybatis_plus.mapper.SimpleMapper;
      import org.junit.jupiter.api.Test;
      import org.junit.runner.RunWith;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.boot.test.context.SpringBootTest;
      import org.springframework.test.context.junit4.SpringRunner;
      import java.util.Map;

      @RunWith(SpringRunner.class)
      @SpringBootTest
      class Select7Test {

      @Autowired
      private SimpleMapper simpleMapper;
      
      @Test
      void contextLoads() {
          // 返回的结果类型为 Map
          Page> page = new Page<>(1, 4);
          simpleMapper.selectMapsPage(page, null);
          System.out.println("size: " + page.getSize());
          System.out.println("total: " + page.getTotal());
          System.out.println("pages: " + page.getPages());
          for(Map map : page.getRecords()) {
              System.out.println(map);
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15

      }

    注意:这里我们平常会使用以下代码获取page里边的存放的代码。
    page.getRecords():这是用来获取我们分页查出来的数据

    5. 最后总结

    这一小结,我们主要是对mybatis-pluts 插件的分页功能的使用,做了简单介绍。下边我们来梳理以下,使用插件步骤:

    1. 在我们项目的配置文件夹下,一定要添加MybatisPlusConfig
    2. 我们需要在这个配置类中添加paginationInterceptor()方法,进行分页功能的配置,其实就是配置分页功能的拦截器
    3. 使用方法,进来数据的分页
    4. 使用方法,返回分页的数据

    以上就是我们使用分页的步骤了,这里需要注意一些问题,一定要把相应的注解加上去,要不然,是没办法使用的。

  • 相关阅读:
    Adobe 推出 Photoshop Elements 2024 新版
    STM32 CAN/CANFD软件快速配置(HAL库版本)
    cocoscreator播放Spine动画
    五、torchvision
    【原创】指针变量作为函数参数要点注意+main函数中值是否改变
    最新CLion + STM32 + CubeMX 开发环境搭建
    Flink重分区算子解析 - StreamPartitioner
    Ubuntu20.04 部署 k8s
    java项目-第151期ssm文物管理系统_java毕业设计_计算机毕业设计
    人脸识别5.1.2- insightface人脸检测模型RetinaFace-Paddle
  • 原文地址:https://blog.csdn.net/m0_67403076/article/details/126035165