1. 分页逻辑的步骤

public PageResponseResult pageList(WmMaterialDto dto){
        // 1.生成page
        IPage<WmMaterial> rawPage = new Page<>(dto.getPage(), dto.getSize());

        // 2.封装wrapper 
        Integer userId = RequestContextUtil.get("id");
        LambdaQueryWrapper<WmMaterial> wrapper = Wrappers.<WmMaterial>lambdaQuery()
                .eq(dto.getIsCollection()!=null,
                        WmMaterial::getIsCollection,
                        dto.getIsCollection())
                .eq(WmMaterial::getUserId,userId);
        // 3.分页查询
        IPage<WmMaterial> resultPage = wmMaterialMapper.selectPage(rawPage, wrapper);

        // 4.封装结果对象
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) resultPage.getTotal());
        pageResponseResult.setData(resultPage.getRecords());

        // 5.返回结果
        return pageResponseResult;
    }

如果不配置MybatisPlusInterceptor,resultPage里会有全部的records,并且total为0

2. 配置分页插件

@Bean
    public MybatisPlusInterceptor paginationInnerInterceptor(){
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        paginationInnerInterceptor.setDbType(DbType.MYSQL);

        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        mybatisPlusInterceptor.addInnerInterceptor(paginationInnerInterceptor);
        return mybatisPlusInterceptor;
    }

3. models实体类

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;

@Data
@TableName("wm_material")
public class WmMaterial implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 主键
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    /**
     * 自媒体用户ID
     */
    @TableField("user_id")
    private Integer userId;

    /**
     * 图片地址
     */
    @TableField("url")
    private String url;

    /**
     * 素材类型
     0 图片
     1 视频
     */
    @TableField("type")
    private Short type;

    /**
     * 是否收藏
     0表示全部
     1表示收藏
     */
    @TableField("is_collection")
    private Short isCollection;

    /**
     * 创建时间
     */
    @TableField("created_time")
    private Date createdTime;

}

4. spring配置

mybatis-plus:
  mapper-locations: classpath*:mapper/*.xml
  # 设置别名包扫描路径,通过该属性可以给包中的类注册别名
  type-aliases-package: com.xxx.xxx.xxx.pojos
04-29 10:07