mybatisPlus分頁外掛的使用

juan發表於2021-03-18

#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 協議》,轉載必須註明作者和本文連結

相關文章