6. MyBatis Plus 分页插件
小于 1 分钟
六、分页插件
在MyBatisPlus中集成了分页插件,我们不需要单独的引入,只需要添加对应的配置类
@Configuration
@MapperScan("com.bobo.mpdemo01.mapper")
public class MyBatisPlusConfig {
/**
* 新的分页插件,一缓和二缓遵循mybatis的规则,
* 需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
然后就可以测试操作了
@Test
void queryPage() {
Page<User> page = new Page<>(1,5);
Page<User> userPage = userMapper.selectPage(page, null);
System.out.println("userPage.getCurrent() = " + userPage.getCurrent());
System.out.println("userPage.getSize() = " + userPage.getSize());
System.out.println("userPage.getTotal() = " + userPage.getTotal());
System.out.println("userPage.getPages() = " + userPage.getPages());
System.out.println("userPage.hasPrevious() = " + userPage.hasPrevious());
System.out.println("userPage.hasNext() = " + userPage.hasNext());
}