当前位置: 首页 > news >正文

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

问题描述

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

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

解决方法前提

先把PageUtils.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的字段转换一下,如下

// 错误的写法
select count(0)from user uleft join dept d on d.id=u.dept_idwhere 1=1and u.is_deleted = 0// 正确的写法
select count(0)from user uleft join dept d on concat(d.id,'')=u.dept_idwhere 1=1and u.is_deleted = 0// 正确的写法
select count(0)from user uleft join dept d on cast( d.id AS CHAR )=u.dept_idwhere 1=1and 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 aleft join user b on a.id = b.idorder by a.id
</select><select id="selectLeftjoin_COUNT" resultType="Long">select count(distinct a.id) from user aleft 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
{@Beanpublic 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);}
}
http://www.lryc.cn/news/169224.html

相关文章:

  • SpringMVC学习笔记——1
  • 20230908_python练习_selenium模块爬取网页小说练习
  • Python:安装Flask web框架hello world示例
  • 深度解析NLP文本摘要技术:定义、应用与PyTorch实战
  • zabbix监控多实例redis
  • win11将visual studio 2022的调试控制台改为windows terminal
  • 社区问答精选——长安链开发知多少?(8月)
  • 神经网络-Unet网络
  • Java | 多线程综合练习
  • Kotlin变量与控制条件的基本用法
  • 第18章_瑞萨MCU零基础入门系列教程之GPT
  • openssl websockets
  • Vue 组件的单元测试
  • 海底两万里的思维导图,轻松了解整体的内容
  • ZABBIX 6.4官方安装文档
  • 本地MQTT服务器搭建(EMQX)
  • Docker启动pandora并指定ACCESS TOKEN
  • Python + Jmeter 实现自动化性能压测
  • 【Linux进行时】进程状态
  • HarmonyOS开发环境搭建
  • 友思特新闻|友思特与IDS深化战略合作伙伴关系
  • ARM Linux DIY(十三)Qt5 移植
  • 二,手机硬件参数介绍和校验算法
  • ubunutu20/18/22 编译android 5相关的问题汇总-千里马framework开源代码平板编译过程
  • tauri vue vite
  • 名词解析与经验分享(前端)
  • 【前端】js下载url文件
  • 什么是 BSD 协议?
  • 【网络教程】揭秘Windows SSH服务端免密登录:告别繁琐,享受安全连接
  • 使用键盘控制Franka机械臂运动