MybatisPlus使用分页时records为空集

引言

众所周知,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

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Mybatis-Plus默认分页插件返回的是IPage<T>对象,其中T为查询结果的实体类类型。如果您需要返回Map类型的分页结果,可以通过自定义分页拦截器来实现。以下是实现步骤: 1. 定义自定义分页拦截器MybatisMapPageInterceptor,继承PaginationInterceptor类,并重写intercept方法。在该方法中,将IPage<T>对象中的records转换为Map类型,并返回。具体实现如下: ``` public class MybatisMapPageInterceptor extends PaginationInterceptor { @Override public Object intercept(Invocation invocation) throws Throwable { Object result = invocation.proceed(); if (result instanceof IPage) { IPage page = (IPage) result; List<Map<String, Object>> mapList = new ArrayList<>(); for (Object record : page.getRecords()) { Map<String, Object> map = BeanUtil.beanToMap(record); mapList.add(map); } page.setRecords(mapList); return page; } return result; } } ``` 2. 在Mybatis配置文件中,添加自定义分页拦截器,如下: ``` <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath*:mapper/*.xml"/> <property name="plugins"> <array> <bean class="com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor"/> <bean class="com.example.demo.interceptor.MybatisMapPageInterceptor"/> </array> </property> </bean> ``` 这样,您就可以在使用Mybatis-Plus的分页插件,返回Map类型的分页结果了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值