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é)果。