彻底解决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);}
}