1.配置分页拦截器
如果不配置拦截器的话,那么下面的查询页数和总条数
studentsPages.getPages()和studentsPages.getTotal()都为0
@Configuration
public class MpConfig {
@Bean
public MybatisPlusInterceptor MybatisPlusInterceptor(){
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//配置mybatisplus分页拦截器
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return interceptor;
}
}
2.测试类
@SpringBootTest
public class PageTest {
@Resource
private StudentsMapper studentsMapper;
@Test
void testPage(){
//设置当前页和每页条数
Page<Students> studentsPage = new Page<>(2, 5);
Page<Students> studentsPages = studentsMapper.selectPage(studentsPage, null);
System.out.println("studentsPages = " + studentsPages);
System.out.println("studentsPages.getPages() = " + studentsPages.getPages());
System.out.println("studentsPages.getRecords() = " + studentsPages.getRecords());
System.out.println("studentsPages.getCurrent() = " + studentsPages.getCurrent());
System.out.println("studentsPages.getTotal() = " + studentsPages.getTotal());
System.out.println("studentsPages.getSize() = " + studentsPages.getSize());
}
}