彻底解决ruoyi分页后总数错误的问题

问题描述

最近时不时的发现用户列表出来的数据只有24条,但是总记录数却有58条,很奇怪。各种百度查询,都是什么修改查询分页改代码,尝试后发现还是没有效果,经过各种验证发现就是SQL语句错误。

如果非要说是SQL语句没有问题,查询出来的数据是正确的,如果基于这个事实去讲那确实没有错,错的是你建表的字段类型不一致,这是本质错误。举个例子,你的主表user中有一个dept_id的字段,而这个字段是varchar(200),但是你的dept表的id确实bigint的类型,这个时候就会出现你用具体的字段去查询是正确的结果数据,但是用count(*) 去查询就是错误的记录数。

解决方法前提

先把PageUtils.java替换成和我一样的。避免出现其它的幺蛾子。

java 复制代码
package com.ruoyi.common.utils;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.utils.sql.SqlUtil;

import java.util.List;

/**
 * 分页工具类
 *
 * @author ruoyi
 */
public class PageUtils extends PageHelper {

    private static Page page;
    /**
     * 设置请求分页数据
     */
    public static void startPage() {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize)) {
            String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
            Boolean reasonable = pageDomain.getReasonable();
            PageHelper.startPage(pageNum, pageSize, orderBy).setReasonable(reasonable);
        }
    }


    public static TableDataInfo getDataTable(List<?> list) {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(list);
        rspData.setTotal(page.getTotal());
        return rspData;
    }
}

解决方法1

把两张表的字段类型对应成一样的。如果有一个是bigint,有一个是varchar要么都统一修改为varchar要么统一修改为bigint。

解决访问2

字段不用修改,修改SQL语句,把bigint的字段转换一下,如下

java 复制代码
// 错误的写法
select count(0)
        from user u
        left join dept d on d.id=u.dept_id
        where 1=1
        and u.is_deleted = 0

// 正确的写法
select count(0)
        from user u
        left join dept d on concat(d.id,'')=u.dept_id
        where 1=1
        and u.is_deleted = 0

// 正确的写法
select count(0)
        from user u
        left join dept d on cast( d.id AS CHAR )=u.dept_id
        where 1=1
        and u.is_deleted = 0

警告

切记,不要让没有经验的人去设计表结构,我这次就是为了100%兼容V1版本的设计,用ruoyi框架去做的倾斜匹配,导致各种问题。

------------------------------华丽的分割线--------

无用解决方案

以下都是没有用的解决方案,大家不用看了,修改你的SQL语句去吧。下面的都是凑字数的。

分页插件如何手写count查询支持

增加countSuffix count 查询后缀配置参数,该参数是针对PageInterceptor配置的,默认值为_COUNT

分页插件会优先通过当前查询的msId + countSuffix查找手写的分页查询。

如果存在就使用手写的count查询,如果不存在,仍然使用之前的方式自动创建count查询。

例如,如果存在下面两个查询:

<select id="selectLeftjoin" resultType="com.github.pagehelper.model.User">
    select a.id,b.name,a.py from user a
    left join user b on a.id = b.id
    order by a.id
</select>

<select id="selectLeftjoin_COUNT" resultType="Long">
    select count(distinct a.id) from user a
    left join user b on a.id = b.id
</select>

上面的countSuffix使用的默认值_COUNT,分页插件会自动获取到selectLeftjoin_COUNT查询,这个查询需要自己保证结果数正确。

返回值的类型必须是resultType="Long",入参使用的和selectLeftjoin查询相同的参数,所以在SQL中要按照selectLeftjoin的入参来使用。

因为selectLeftjoin_COUNT方法是自动调用的,所以不需要在接口提供相应的方法,如果需要单独调用,也可以提供。

如何处理Long类型精度丢失问题

当字段实体类为Long类型且值超过前端js显示的长度范围时会导致前端回显错误,解决方案如下

1、使用JsonSerialize注解序列化的时候把Long自动转为String(针对单个属性)

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;

@JsonSerialize(using = ToStringSerializer.class)
private Long xxx;

2、添加JacksonConfig配置全局序列化(针对所有属性)

package com.ruoyi.framework.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;

/**
 * Jackson配置
 * 
 * @author ruoyi
 *
 */
@Configuration
public class JacksonConfig
{
    @Bean
    public MappingJackson2HttpMessageConverter jackson2HttpMessageConverter()
    {
        final Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
        builder.serializationInclusion(JsonInclude.Include.NON_NULL);
        final ObjectMapper objectMapper = builder.build();
        SimpleModule simpleModule = new SimpleModule();
        // Long 转为 String 防止 js 丢失精度
        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
        objectMapper.registerModule(simpleModule);
        // 忽略 transient 关键词属性
        objectMapper.configure(MapperFeature.PROPAGATE_TRANSIENT_MARKER, true);
        // 时区设置
        objectMapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        return new MappingJackson2HttpMessageConverter(objectMapper);
    }
}
相关推荐
小比卡丘9 分钟前
C语言进阶版第17课—自定义类型:联合和枚举
android·java·c语言
xmh-sxh-131415 分钟前
java 数据存储方式
java
小徐敲java28 分钟前
通用mybatis-plus查询封装(QueryGenerator)
mybatis
liu_chunhai28 分钟前
设计模式(3)builder
java·开发语言·设计模式
ya888g1 小时前
GESP C++四级样题卷
java·c++·算法
【D'accumulation】1 小时前
令牌主动失效机制范例(利用redis)注释分析
java·spring boot·redis·后端
小叶学C++1 小时前
【C++】类与对象(下)
java·开发语言·c++
2401_854391081 小时前
高效开发:SpringBoot网上租赁系统实现细节
java·spring boot·后端
Cikiss2 小时前
微服务实战——SpringCache 整合 Redis
java·redis·后端·微服务
wxin_VXbishe2 小时前
springboot合肥师范学院实习实训管理系统-计算机毕业设计源码31290
java·spring boot·python·spring·servlet·django·php