信息发布→ 登录 注册 退出

MyBatis-Plus 分页查询的实现示例

发布时间:2026-01-11

点击量:

方法:

使用selectPage()方法,
第一个参数是传入分页方法(传入当前页和当前显示多少条数据),
第二个参数是传入查询条件(如果查询全部的话,可以传null)。

前提:

表中的数据为:

第一种方式:

//分页查询
Page<Employee> employees = employeeMapper.selectPage(new Page<>(3, 2), null);
System.out.println("数据为:"+employees.getRecords());
System.out.println("总数为:"+employees.getTotal()+",总页数为:"+employees.getPages());
System.out.println("当前页为:"+employees.getCurrent()+",每页限制:"+employees.getSize());

结果为:

展示了所有的数据,也没有总数,并没有分页的效果。

第二种方式:

//分页查询
Page<Employee> employees = employeeMapper.selectPage(new Page<>(3, 2), null);
Integer count = employeeMapper.selectCount(null);
employees.setTotal(count);
System.out.println("数据为:"+employees.getRecords());
System.out.println("总数为:"+employees.getTotal()+",总页数为:"+employees.getPages());
System.out.println("当前页为:"+employees.getCurrent()+",每页限制:"+employees.getSize());

结果为:

虽然有了总数和总页数,但依然没有分页的效果。

第三种方式:

//分页查询
Page<Employee> employees = employeeMapper.selectPage(new Page<>(3, 2), null);
System.out.println("数据为:"+employees.getRecords());
System.out.println("总数为:"+employees.getTotal()+",总页数为:"+employees.getPages());
System.out.println("当前页为:"+employees.getCurrent()+",每页限制:"+employees.getSize());

增加Mybatis-Plus插件,

@Configuration
public class MyBatisPlusConfig {
    @Bean
    public PaginationInterceptor paginationInterceptor(){
        PaginationInterceptor page = new PaginationInterceptor();
        return page;
    }
}

结果:

在线客服
服务热线

服务热线

4008888355

微信咨询
二维码
返回顶部
×二维码

截屏,微信识别二维码

打开微信

微信号已复制,请打开微信添加咨询详情!