在做一个自动推荐系统的时候,碰到了一个问题,我传入想做关联的tag,但是实际返回结果却不大对,一番追查下来,发现竟然是mysql返回结果集的时候,没有按照我传入参数的顺序,而是按照了ID从小到大的排列顺序,导致查询数据的时候,关联度最高的标签没有放在最前面。
查阅资料发现,mysql是允许指定这个返回参数顺序的,只不过要多加一个order by field (id, ....),现在用我的代码给大家演示一下这个问题。
mapper.xml映射文件:
<select id="gets" resultMap="resultMap">
select
<include refid="columns"/>
from tag
where deleted = 0
and id in
<foreach collection="ids" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</select>
mapper.java映射方法:
public Map<Integer, T> gets(Collection<Integer> ids) {
if (CollectionUtils.isEmpty(ids)) {
return null;
}
List<T> entitys = baseDao.gets(new LinkedHashSet<>(ids));