一、简述
在使用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 {@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();// 分页插件PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);// 设置数据库为mysqlpaginationInnerInterceptor.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));// 返回前端的VOPageVO<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;// 课程idprivate Integer cid;
}
测试
{"code": 20000,"msg": "成功","data": {"total": 4,"list": [{"id": 2,"name": "www","studentId": "3333"},{"id": 3,"name": "3333","studentId": "1111"}]}
}
sql语句
当然在创建page对象时还有定义其他功能,比如排序规则
最后
如果感觉有收获的话,点个赞 👍🏻 吧。
❤️❤️❤️本人菜鸟修行期,如有错误,欢迎各位大佬评论批评指正!😄😄😄
💘💘💘如果觉得这篇文对你有帮助的话,也请给个点赞、收藏下吧,非常感谢!👍 👍 👍