MybatisPlus使用分页时records为空集

您所在的位置:网站首页 mybatisplus的ipage MybatisPlus使用分页时records为空集

MybatisPlus使用分页时records为空集

2022-12-21 18:19| 来源: 网络整理| 查看: 265

引言

众所周知,MybatisPlus是一款好用的ORM框架,但是最近在使用MybatisPlus处理分页的时候,遇到了分页不生效的问题,也就是在IPage对象在返回时,其他的内容均正常,但是records没有内容。为了解决这个问题,尝试了三种方式,最后终于生效了,于是记录一下。(针对Spring boot框架) 以下三个点,任何一个点忽略了都会导致records没有内容!!!

一、设置过滤器Inteceptor

有时即使正确定义了接口参数和结果,按照标准传入了IPage,也接收了IPage,但是分页就是不生效,records中并不能看过结果,原因可能是MybatisPlus的翻页插件没有设置,关于mybatisplus的翻页设置,在官网有介绍,可以按照网址查看。 在这里插入图片描述 代码如下:

package com.caicai.mybatisplusdemo.config; import com.baomidou.mybatisplus.annotation.DbType; 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; @Configuration // 这个注解就是告诉springboot在容器启动时依赖注入时给JavaBean的配置 public class MybatisPlusConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor(){ MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); // 添加分页过滤器,如果mybatisplus需要使用分页,boot必须添加这个配置 PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor(DbType.MYSQL); // 溢出总页数后是否进行处理 paginationInnerInterceptor.setOverflow(true); interceptor.addInnerInterceptor(paginationInnerInterceptor); return interceptor; } } 二、返回值类型缺少Getter和Setter方法

比如我的表有10个字段,但是前段只需要5个字段,并且前端的同事跟你打招呼,请你不要传输多余的字段**(可以减少网络IO)**,于是,你写了一个VO,将response的字段封装成五个,在你些接口的时候,也设置了mapper中返回值的类型,例如这样 在这里插入图片描述 但是,如果PlayerInfoVO缺少Getter和Setter方法,查询结果是无法形成包装类的。这种情况也会导致报错

在这里插入图片描述

import io.swagger.annotations.ApiModelProperty; import lombok.Data; import java.math.BigDecimal; /** * 一定需要添加@Data注释,因为@Data注释等于为实体类的所有属性添加了 * get、set、equals、hashCode、toString等方法 * ORM框架将字段值映射到实体类的时候,通过Setter方法为类的属性赋值 * 因此,返回值一定需要@Data注解 */ @Data public class PlayerInfoVO { @ApiModelProperty(value = "主键ID") private String id; @ApiModelProperty(value = "姓名") private String playerName; @ApiModelProperty(value = "国籍") private String nation; @ApiModelProperty(value = "身价") private BigDecimal price; @ApiModelProperty(value = "队伍ID") private String teamId; @ApiModelProperty(value = "号码") private Integer number; @ApiModelProperty(value = "年龄") private Integer age; } 三、源码

感兴趣的可以拉一下,也没啥东西。 https://github.com/caijiahuihui/mybatisplus-demo



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3