Mybatis-Plus分页

(1)创建配置类

/**
 * 分页插件
 */
@Bean
public PaginationInterceptor paginationInterceptor() {
    return new PaginationInterceptor();
}

(2)测试selectPage分页
测试:最终通过page对象获取相关数据

@Test
public void testSelectPage() {//1.创建page对象,传入两个参数:当前页和每页显示记录数
    Page<User> page = new Page<>(1,5);
    //2.调用方法查询
    userMapper.selectPage(page, null);
​
    page.getRecords().forEach(System.out::println);//每页数据list集合
    System.out.println(page.getCurrent());//当前页
    System.out.println(page.getPages());//总页数
    System.out.println(page.getSize());//每页显示记录数
    System.out.println(page.getTotal());//总记录数
    System.out.println(page.hasNext());//是否有下一页
    System.out.println(page.hasPrevious());//是否有上一页
}

控制台sql语句打印:SELECT id,name,age,email,create_time,update_time FROM user LIMIT 0,5

(3)测试selectMapsPage分页:结果集是Map

@Test
public void testSelectMapsPage() {
​
    Page<User> page = new Page<>(1, 5);
​
    IPage<Map<String, Object>> mapIPage = userMapper.selectMapsPage(page, null);//注意:此行必须使用 mapIPage 获取记录列表,否则会有数据类型转换错误
    mapIPage.getRecords().forEach(System.out::println);
    System.out.println(page.getCurrent());
    System.out.println(page.getPages());
    System.out.println(page.getSize());
    System.out.println(page.getTotal());
    System.out.println(page.hasNext());
    System.out.println(page.hasPrevious());
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值