溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Mybatis Plus怎么使用分頁(yè)

發(fā)布時(shí)間:2022-03-04 16:04:16 來(lái)源:億速云 閱讀:208 作者:iii 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要介紹了Mybatis Plus怎么使用分頁(yè)的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇Mybatis Plus怎么使用分頁(yè)文章都會(huì)有所收獲,下面我們一起來(lái)看看吧。

    Mybatis-Plus(簡(jiǎn)稱MP)是一個(gè) Mybatis 的增強(qiáng)工具,在 Mybatis 的基礎(chǔ)上只做增強(qiáng)不做改變,為簡(jiǎn)化開(kāi)發(fā)、提高效率而生

    1.寫(xiě)個(gè)Mybatis-plus配置類:

    是通過(guò)攔截器實(shí)現(xiàn)分頁(yè)

    @Configuration
    public class MybatisConfig {
        @Bean
        public MybatisPlusInterceptor mybatisPlusInterceptor() {
            MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
            interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
            return interceptor;
        }
    }

    官網(wǎng)復(fù)制即可,只是你需要把數(shù)據(jù)庫(kù)改為你使用的,這里我是使用mysql

    Mybatis Plus怎么使用分頁(yè)

    2.寫(xiě)接口測(cè)試

    很簡(jiǎn)單

    @GetMapping("/test")
        public Response test(){
            Page<Produce> producePage = new Page<>(1,1);
            Page<Produce> page = produceService.page(producePage);
            System.out.println(producePage == page);
            List<Produce> records = page.getRecords();
            for (Produce record : records) {
                System.out.println(record);
            }
            return new Response<>(records, ResultEnum.SUCCESS);
        }

    Mybatis Plus怎么使用分頁(yè)

    默認(rèn)是會(huì)查詢總條數(shù),都有g(shù)et、set方法,可以根據(jù)自己的需求設(shè)置(點(diǎn)開(kāi)Page類看看)

    Mybatis Plus怎么使用分頁(yè)

    3.注意

    我們傳入的page對(duì)象和查詢返回的page對(duì)象是同一個(gè)

    Mybatis Plus怎么使用分頁(yè)

    Mybatis Plus怎么使用分頁(yè)

    4.如果你還有查詢條件

    比如我們只查詢id和price,id小于5的分頁(yè)查詢

    Mybatis Plus怎么使用分頁(yè)

    1.Lambda表達(dá)式

    @GetMapping("/test")
    public Response test(){
        Page<Produce> producePage = new Page<>(1,2);
        Page<Produce> page = new LambdaQueryChainWrapper<>(produceService.getBaseMapper())
                .select(Produce::getPid,Produce::getPrice)
                .lt(Produce::getPid,5)
                .page(producePage);
    
        return new Response<>(page.getRecords(), ResultEnum.SUCCESS);
    }

    Mybatis Plus怎么使用分頁(yè)

    2.普通查詢

    @GetMapping("/test")
    public Response test(){
        Page<Produce> producePage = new Page<>(1,2);
        QueryWrapper<Produce> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("pid","price");
        queryWrapper.lt("pid",5);
        Page<Produce> page = produceService.page(producePage, queryWrapper);
        return new Response<>(page.getRecords(), ResultEnum.SUCCESS);
    }

    Mybatis Plus怎么使用分頁(yè)

    Mybatis Plus怎么使用分頁(yè)

    關(guān)于“Mybatis Plus怎么使用分頁(yè)”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“Mybatis Plus怎么使用分頁(yè)”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

    向AI問(wèn)一下細(xì)節(jié)

    免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

    AI