项目场景:
项目列表使用MyBatis-Plus分页组件,对列表进行分页、排序、分组查询。
问题描述
列表查询结果发现,每次重新调用列表接口,排序不相同。
原因分析:
最开始以为数据排序问题,把返回数组换成LinkedList后继续观察,发现不是业务代码排序导致,查看数据库查询结果,发现每次相同的SQL,相同的分页条件,数据库查询顺序都是不一样。
解决方案:
将数据库排序条件后边增加表主键,相当于组合排序,来确定数据的唯一顺序。
代码样例(prd_no_main是主键):
<select id="searchSpuGroup" resultType="com.huice.cq.stock.model.dto.SpuGroupDTO">
select prd_no_main
from stock_list stl
<where>
<include refid="stock_list_common_where"/>
</where>
group by prd_no_main
<if test="param.orderColumn != null and param.orderColumn != ''">
<if test="param.orderColumn == 3">
order by amount desc,prd_no_main
</if>
<if test="param.orderColumn == 1">
order by cus_no,prd_no_main
</if>
<if test="param.orderColumn == 2">
order by cus_name,prd_no_main
</if>
</if>
</select>