MyBatis-Plus分頁:刪除最后一頁最后一條數(shù)據(jù)后顯示無數(shù)據(jù)(后端解決方法)

1、問題描述

MyBatis-Plus使用Ipage分頁查詢數(shù)據(jù)時(shí),刪除最后一頁全部數(shù)據(jù)后,再次調(diào)用查詢接口,顯示當(dāng)前頁無數(shù)據(jù)。
例如:共有21條數(shù)據(jù),每頁顯示10條數(shù)據(jù),刪除最后一條數(shù)據(jù)(第3頁的唯一一條數(shù)據(jù))后,頁面跳轉(zhuǎn)至第二頁,但是顯示無數(shù)據(jù)。

分頁查詢代碼如下(Repository層):

//分頁查詢列表
    @Override
    public IPage<SiteType> getAllByOrgIdAndPagination(String orgId, Pagination pagination) {
        //Page從第1頁開始,Pagination從第0頁開始
        IPage<SiteType> page = new Page<>(pagination.getCurrent() + 1, pagination.getPageSize());
        Wrapper<SiteType> queryWrapper = Wrappers.lambdaQuery(SiteType.class)
                .eq(SiteType::getOrgId, orgId)
                .orderByDesc(SiteType::getCreateTime);
        return siteTypeMapper.selectPage(page,queryWrapper);
    }

2、問題原因

查詢接口只查詢當(dāng)前頁的數(shù)據(jù),在最后一頁(第3頁)刪除最后一條數(shù)據(jù)后,前端仍查詢當(dāng)前頁(第3頁)的數(shù)據(jù),結(jié)果為空。
因此需要判斷當(dāng)前頁是否有數(shù)據(jù),若無數(shù)據(jù),需要進(jìn)行處理,如跳轉(zhuǎn)至前一頁或首頁。本文介紹后端處理方法。

3、解決方法

方法一:手動(dòng)判斷當(dāng)前頁是否有數(shù)據(jù)(不推薦)

在Service層判斷當(dāng)前頁是否有數(shù)據(jù),若無數(shù)據(jù),查詢前一頁:

    @Override
    public SiteTypeListDTO getSiteTypeList(SiteTypeListRequest request) {
        User user = ContextUserHolder.get();
        IPage<SiteType> siteTypeIPage = siteTypeRepository.getAllByOrgIdAndPagination(user.getOrgId(), request.getPagination());
        Pagination pagination = new Pagination(siteTypeIPage.getCurrent() - 1, siteTypeIPage.getSize(), siteTypeIPage.getTotal(), null);

        //如果當(dāng)前頁沒有數(shù)據(jù)(如刪除某頁的唯一一條數(shù)據(jù)),重新獲取前一頁的記錄
        //例如查詢第3頁時(shí),current:2,pageSize:10,如果total<=20,則current-=1
        if (pagination.getCurrent() * pagination.getPageSize() >= pagination.getTotal() && pagination.getCurrent() != 0) {
            //request中的current減1
            request.getPagination().setCurrent(request.getPagination().getCurrent() - 1);
            siteTypeIPage = siteTypeRepository.getAllByOrgIdAndPagination(user.getOrgId(), request.getPagination());
            pagination.setCurrent(pagination.getCurrent() - 1);
        }

        List<SiteTypeDTO> siteTypeList = siteTypeIPage.getRecords().stream()
                .map(siteType -> convert2SiteTypeDTO(siteType, user.getLocale())).collect(Collectors.toList());
        return new SiteTypeListDTO(siteTypeList, pagination);
    }

方法二:設(shè)置分頁插件屬性(推薦)

參考mybatis plus分頁插件設(shè)置,配置mybatis plus分頁器,設(shè)置overflow屬性為true(查詢頁面超出范圍時(shí),跳轉(zhuǎn)至首頁):

@Configuration
public class MybatisConfig {    
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();

        //設(shè)置分頁插件屬性:請(qǐng)求頁超出范圍時(shí),返回首頁
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.MYSQL);        
        paginationInnerInterceptor.setOverflow(true);
        interceptor.addInnerInterceptor(paginationInnerInterceptor);

        return interceptor;
    }    
}

設(shè)置Overflow屬性為true后,刪除最后一頁最后一條數(shù)據(jù)后頁面空白的問題就解決了。

修改Overflow處理邏輯:無數(shù)據(jù)時(shí)查詢前一頁數(shù)據(jù)

查看PaginationInnerInterceptor.class源碼發(fā)現(xiàn),頁面超出范圍后的處理是查詢第1頁數(shù)據(jù):

    protected boolean continuePage(IPage<?> page) {
        if (page.getTotal() <= 0L) {
            return false;
        } else {
            if (page.getCurrent() > page.getPages()) {
                if (!this.overflow) {
                    return false;
                }
                this.handlerOverflow(page);
            }
            return true;
        }
    }

    //頁面超出范圍時(shí),返回第1頁    
    protected void handlerOverflow(IPage<?> page) {
        page.setCurrent(1L);
    }

為了保持刪除后的查詢邏輯一致(刪除后返回當(dāng)頁,當(dāng)頁無數(shù)據(jù)返回前一頁,而不是首頁),可以重寫handlerOverflow方法如下:
①新建NewPaginationInnerInterceptor 類,重寫handlerOverflow方法:

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author Tony brings water
 * @date 2021/8/18 14:10
 * 分頁刪除某頁唯一記錄后,自動(dòng)跳轉(zhuǎn)至前一頁(默認(rèn)實(shí)現(xiàn)是跳轉(zhuǎn)至首頁)
 */
@Data
@NoArgsConstructor
public class NewPaginationInnerInterceptor extends PaginationInnerInterceptor {
    private DbType dbType;

    public NewPaginationInnerInterceptor(DbType dbType) {
        this.dbType = dbType;
    }

    @Override
    protected void handlerOverflow(IPage<?> page) {
        //查詢前一頁
        page.setCurrent(page.getCurrent() - 1);
    }
}

具體跳轉(zhuǎn)邏輯可根據(jù)業(yè)務(wù)自定義。
②修改MyBatis Plus配置,使用NewPaginationInnerInterceptor:

@Configuration
public class MybatisConfig {    
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();

        //設(shè)置分頁插件屬性:請(qǐng)求頁超出范圍時(shí),返回前一頁        
        NewPaginationInnerInterceptor paginationInnerInterceptor = new NewPaginationInnerInterceptor(DbType.MYSQL);
        paginationInnerInterceptor.setOverflow(true);
        interceptor.addInnerInterceptor(paginationInnerInterceptor);

        return interceptor;
    }    
}

至此,問題解決,刪除最后一頁最后一條數(shù)據(jù)后,頁面能夠正確顯示結(jié)果。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
禁止轉(zhuǎn)載,如需轉(zhuǎn)載請(qǐng)通過簡(jiǎn)信或評(píng)論聯(lián)系作者。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容