The use of mybatisplus paging plug in

Time:2021-4-2

#Mybatisplus version requirements: 3.4 and above

First, create a new accessory file

@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;
    }
}

Using in controller or service layer

Default to the first page, 10 items per page

@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);

    }

#Analysis:

Page class parameters, with parameter constructor

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);
    }

This work adoptsCC agreementReprint must indicate the author and the link of this article