主要分为两步:
- 配置分页拦截器
首先要定义一个配置类(需要注解@Configuration),在其中再写对应的Mp拦截器(需要注解@Bean),在Mp拦截其中添加具体的分页拦截器即可。
@Configuration
public class MpConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
//定义Mp拦截器
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//添加具体的分页拦截器
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return interceptor;
}
}
- 调用selectPage()方法
@Test
void testGetPages(){
Page page = new Page(2,5);
bookDao.selectPage(page,null);
System.out.println(page.getPages());
System.out.println(page.getCurrent());
System.out.println(page.getSize());
System.out.println(page.getTotal());
System.out.println(page.getRecords());
}