Mybatis-Plus 使用IPage分页失效问题处理
问题
在mybatis-plus中使用IPage作为分页功能实现,请求发现并没有实现分页功能。
部分代码
## controller
@PostMapping("/getCollege")
@ApiOperation("查询学院信息")
public CommonResult<IPage<ZgdCollegeVo>> getCollege(@RequestBody ZgdCollegeBo zgdCollegeBo){
IPage<ZgdCollegeVo> college = zgdCollegeServiceImpl.getCollege(new Page<>(zgdCollegeBo.getPageNum(), zgdCollegeBo.getPageSize()), zgdCollegeBo);
return CommonResult.success(college);
}
## service
@Override
public IPage<ZgdCollegeVo> getCollege(IPage<ZgdCollegeVo> page,ZgdCollegeBo zgdCollegeBo) {
return zgdCollegeMapper.getCollege(page,zgdCollegeBo);
}
解决
配置mybatis-plus拦截器,设置以下属性。
package com.xxkj.zgd.school.common.config;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@MapperScan("com.xxkj") // 指定controller层路径
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
paginationInnerInterceptor.setOptimizeJoin(true);
paginationInnerInterceptor.setDbType(DbType.MYSQL);
paginationInnerInterceptor.setOverflow(true);
interceptor.addInnerInterceptor(paginationInnerInterceptor);
OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor = new OptimisticLockerInnerInterceptor();
interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor);
return interceptor;
}
}