mybatisPlus分页插件的使用
#myBatisPlus版本要求:3.4及以上
首先新建一个配件文件
@Configuration
@MapperScan("cn.gitku.dao.*")
public class MybatisConfig implements MetaObjectHandler{
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
return interceptor;
}
}
在controller 或 service 层的使用
默认第一页,每页10条
@PostMapping("list")
public RestResult list(@RequestParam(defaultValue = "1") int current,@RequestParam(defaultValue = "10") int size){
log.info("list");
long userId = BaseController.getUserId();
QueryWrapper<Charge> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("userId",userId);
IPage<Charge> iPage = new Page<>(current,size);
IPage<Charge> list = chargeService.page(iPage,queryWrapper);
return RestResult.success(list);
}
#分析:
page类的参数,带参构造器
public Page() {
this.records = Collections.emptyList();
this.total = 0L;
this.size = 10L;
this.current = 1L;
this.orders = new ArrayList();
this.optimizeCountSql = true;
this.isSearchCount = true;
this.hitCount = false;
}
public Page(long current, long size) {
this(current, size, 0L);
}
本作品采用《CC 协议》,转载必须注明作者和本文链接