【Java】PageHelper 分页 数据重复问题
·
问题复现:
PageHelper.startPage(questionQueryDTO.getPageNum(), questionQueryDTO.getPageSize());
return questionMapper.selectQuestionList(questionQueryDTO);
<select id="selectQuestionList" resultType="com.bite.system.domain.vo.QuestionVO">
SELECT
tq.question_id,
tq.title,
tq.difficulty,
ts.nick_name as create_name,
tq.create_time
FROM
tb_question tq
left join
tb_sys_user ts
on
tq.create_by = ts.user_id
ORDER BY
create_time DESC
</select>

当 selectQuestionList xml进行排序时,排序的字段不唯一,我这里是 创建时间作为排序,然后一共有四个时间,当分页大小为 每页 5条数据时,就会出现如下bug(分页数据重复):

解决:
ORDER BY
create_time DESC,question_id ASC
使用多条件排序,如果要排序的字段的值不是唯一的,那么必须加上具备唯一性的主键id(或其他唯一性字段)作为辅助排序,这样就能避免查询结果重复。
ORDER BY
question_id ASC
或者 只使用唯一性的主键进行排序。
更多推荐



所有评论(0)