MyBatis-Plus分页插件使用详解

一、简述

在使用mybatis开发项目的时候我们通常使用pagehelper来进行分页操作,
但是我们在使用MyBatis-Plus 开发时,MyBatis-Plus内置已经有分页功能了,其实不需要在额外引入pagehelper依赖了,而且两者同时引入有时候还会导致分页功能失效,所以我们在使用MyBatis-Plus 开发时,就可以直接使用MyBatis-Plus自带的分页插件来实现分页功能。

二、使用步骤

2.1 导入MyBatis-Plus 依赖
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.5.7</version>
</dependency>
2.2 在项目中添加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 mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 分页插件
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);// 设置数据库为mysql
        paginationInnerInterceptor.setMaxLimit(1000L); // 设置最大查询数量,防止一次查询太多数据,给数据库压力过大
        // 添加分页插件
        interceptor.addInnerInterceptor(paginationInnerInterceptor);
        return interceptor;
    }
}

配置好之后就可以在业务层实现分页功能了

2.3 使用

在mapper接口中有一个selectPage()方法

IPage<T> selectPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);

该方法需要传入一个IPage类型的参数,和一个queryWrapper
这里我们需要知道什么时IPage。
在这里插入图片描述
官方文档的实例

// 假设要进行分页查询,每页显示10条记录,查询第1页,查询条件为 age > 25
IPage<User> page = new Page<>(1, 10);
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.gt("age", 25);
IPage<User> userPage = userMapper.selectPage(page, queryWrapper); // 调用 selectPage 方法
List<User> userList = userPage.getRecords();
long total = userPage.getTotal();
System.out.println("Total users (age > 25): " + total);
for (User user : userList) {
    System.out.println("User: " + user);
}

下面我们就开始带入实际开发中

2.4 实际使用

controller层

    @GetMapping("/findStudentInfo")
    public R findStudentInfo(PageParam param) {
        return R.ok(studentCourseService.findStudentInfo(param));
    }

serviceImpl类

    public PageVO<Student> findStudentInfo(PageParam param) {
    	// 根据课程id查询选课学生id集合
        List<Integer> sidList = studentCourseMapper.getSidListById(param.getCid());
        // 创建page对象,根据前端传来的页面大小size,页码num,这里我封装了成一个param实体类了
        Page<Student> page = Page.of(param.getPageNum(), param.getPageSize());
        // 调用selectPage方法,构造一个条件查询器,查询在sidList 集合的学生信息,返回一个page对象
        Page<Student> studentPage = studentMapper.selectPage(page, new LambdaQueryWrapper<Student>().in(Student::getId, sidList));
        // 返回前端的VO
        PageVO<Student> pageVO = new PageVO<>();
        // 取出page中的记录,即根据分页信息查询到的学生信息
        pageVO.setList(studentPage.getRecords());
        // 总条数
        pageVO.setTotal(studentPage.getTotal());
        return pageVO;
    }

PageParam类

@Data
@ToString
public class PageParam {
    // 页码
    private Integer pageNum;
    // 页面大小
    private Integer pageSize;
    // 模糊查询关键字
    private String keyword;
    // 课程id
    private Integer cid;
}
测试
{
  "code": 20000,
  "msg": "成功",
  "data": {
    "total": 4,
    "list": [
      {
        "id": 2,
        "name": "www",
        "studentId": "3333"
      },
      {
        "id": 3,
        "name": "3333",
        "studentId": "1111"
      }
    ]
  }
}

sql语句
在这里插入图片描述
当然在创建page对象时还有定义其他功能,比如排序规则

最后

如果感觉有收获的话,点个赞 👍🏻 吧。
❤️❤️❤️本人菜鸟修行期,如有错误,欢迎各位大佬评论批评指正!😄😄😄

💘💘💘如果觉得这篇文对你有帮助的话,也请给个点赞、收藏下吧,非常感谢!👍 👍 👍在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值