溫馨提示×

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

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

MyBatis-Plus分頁(yè)查詢的方法有哪些

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

本文小編為大家詳細(xì)介紹“MyBatis-Plus分頁(yè)查詢的方法有哪些”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“MyBatis-Plus分頁(yè)查詢的方法有哪些”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來(lái)學(xué)習(xí)新知識(shí)吧。

方法:

使用selectPage()方法,
第一個(gè)參數(shù)是傳入分頁(yè)方法(傳入當(dāng)前頁(yè)和當(dāng)前顯示多少條數(shù)據(jù)),
第二個(gè)參數(shù)是傳入查詢條件(如果查詢?nèi)康脑?,可以傳null)。

MyBatis-Plus分頁(yè)查詢的方法有哪些

前提:

表中的數(shù)據(jù)為:

MyBatis-Plus分頁(yè)查詢的方法有哪些

第一種方式:

//分頁(yè)查詢
Page<Employee> employees = employeeMapper.selectPage(new Page<>(3, 2), null);
System.out.println("數(shù)據(jù)為:"+employees.getRecords());
System.out.println("總數(shù)為:"+employees.getTotal()+",總頁(yè)數(shù)為:"+employees.getPages());
System.out.println("當(dāng)前頁(yè)為:"+employees.getCurrent()+",每頁(yè)限制:"+employees.getSize());

結(jié)果為:

MyBatis-Plus分頁(yè)查詢的方法有哪些

展示了所有的數(shù)據(jù),也沒(méi)有總數(shù),并沒(méi)有分頁(yè)的效果。

第二種方式:

//分頁(yè)查詢
Page<Employee> employees = employeeMapper.selectPage(new Page<>(3, 2), null);
Integer count = employeeMapper.selectCount(null);
employees.setTotal(count);
System.out.println("數(shù)據(jù)為:"+employees.getRecords());
System.out.println("總數(shù)為:"+employees.getTotal()+",總頁(yè)數(shù)為:"+employees.getPages());
System.out.println("當(dāng)前頁(yè)為:"+employees.getCurrent()+",每頁(yè)限制:"+employees.getSize());

結(jié)果為:

MyBatis-Plus分頁(yè)查詢的方法有哪些

雖然有了總數(shù)和總頁(yè)數(shù),但依然沒(méi)有分頁(yè)的效果。

第三種方式:

//分頁(yè)查詢
Page<Employee> employees = employeeMapper.selectPage(new Page<>(3, 2), null);
System.out.println("數(shù)據(jù)為:"+employees.getRecords());
System.out.println("總數(shù)為:"+employees.getTotal()+",總頁(yè)數(shù)為:"+employees.getPages());
System.out.println("當(dāng)前頁(yè)為:"+employees.getCurrent()+",每頁(yè)限制:"+employees.getSize());

增加Mybatis-Plus插件,

@Configuration
public class MyBatisPlusConfig {
    @Bean
    public PaginationInterceptor paginationInterceptor(){
        PaginationInterceptor page = new PaginationInterceptor();
        return page;
    }
}

結(jié)果:

MyBatis-Plus分頁(yè)查詢的方法有哪些

讀到這里,這篇“MyBatis-Plus分頁(yè)查詢的方法有哪些”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過(guò)才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(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