在springboot中使用mybatis-plus的分页功能

在springboot中使用mybatis-plus的分页功能

一、测试分页功能:

编写测试代码:

@Test
    public void testPage(){
        //设置分页参数,页码为1,大小为5
        Page<Employee> page = new Page<>(1, 5);
        //传入page对象,条件构造为null
        employeeMapper.selectPage(page, null);
        //获取分页数据
        List<Employee> list = page.getRecords();
        //输出分页数据
        list.forEach(System.out::println);
        System.out.println("当前页:"+page.getCurrent());
        System.out.println("每页显示的条数:"+page.getSize());
        System.out.println("总记录数:"+page.getTotal());
        System.out.println("总页数:"+page.getPages());
        System.out.println("是否有上一页:"+page.hasPrevious());
        System.out.println("是否有下一页:"+page.hasNext());
    }

可以看到,很轻松的实现了分页功能。

image-20221104212815602

二、实际使用场景:

1、编写一个mybatis-plus配置类:

@Configuration
public class MybatisPlusConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        // MyBatis Plus分页插件
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}

2、编写mapper接口:

注:(实体类代码不再给出,请自己添加)

@Mapper
public interface EmployeeMapper extends BaseMapper<Employee> {

}

3、编写Controller层后端接口:

@RestController
@RequestMapping("/emp")
public class EmployeeController {

    @Autowired
    private EmployeeMapper employeeMapper;

    @GetMapping("/getEmpByPage/{page}/{size}")
    public Page<Employee> getPage(@PathVariable("page") Integer page,@PathVariable("size") Integer size){
        //设置分页参数
        Page<Employee> pageable = new Page<>(page, size);
        Page<Employee> employeePage = employeeMapper.selectPage(pageable, null);
        return employeePage;
    }

}

4、测试接口:

访问:http://localhost:8000/emp/getByPage/1/3

这里我选择访问的是第1页的3条记录:

image-20221104213948331

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LIAO哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值