使用MybatisPlus进行分页查询的相关配置
package com.mall.utils;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 配置MP的分页插件
*/
@Configuration
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor()); // 添加分页拦截器
return mybatisPlusInterceptor;
}
}
以上代码就是在使用MybatisPlus进行分页查询时需要添加的配置内容
问题说明:
上面两张图分别是请求参数以及响应参数的说明,在项目开发中相关的分页查询代码如下所示:
/**
*
* 根据品牌名称分页获取品牌列表
* @param pageNum
* @param pageSize
* @param keyword
* @return
*/
@GetMapping("/list")
public AjaxResult getList(Integer pageNum, Integer pageSize, String keyword) {
//分页构造器
Page pageInfo = new Page(pageNum, pageSize);
//条件构造器
LambdaQueryWrapper<PmsBrand> queryWrapper = new LambdaQueryWrapper();
//添加过滤条件
queryWrapper.like(StringUtils.isNotEmpty(keyword), PmsBrand::getName, keyword);
//按照排序字段进行倒序排序
queryWrapper.orderByDesc(PmsBrand::getSort);
pmsBrandService.page(pageInfo, queryWrapper);
//重新封装数据,保证返回值一致
Map<String, Object> map = new HashMap<>();
map.put("list", pageInfo.getRecords());
map.put("pageNum", pageInfo.getCurrent());
map.put("pageSize", pageInfo.getSize());
map.put("total", pageInfo.getTotal());
map.put("totalPage", pageInfo.getPages());
return AjaxResult.success(map);
}
看着写的分页代码没有什么问题,但是显示的时候并没有达到分页的效果,debug之后发现 total和 totalPage的值一直为0,所以没有分页的效果,于是查找了一下之前项目中关于分页的内容,最后才发现配置没有写,只要把上面的配置代码加上去就行了。