mybatis的in查询排序输出
1.先看MySQL
#不排序的查询
SELECT * from student WHERE id in (5,1,3,2);
#排序的查询,按先后顺序输出
SELECT * from student WHERE id in (5,1,3,2) ORDER BY field(id,5,1,3,2);
2.mybatis
SELECT * FROM student WHERE id in
<if test="list != null and list.size() > 0">
<foreach collection="list" item="item" open="(" separator="," close=")">
#{item}
</foreach>
order by field(id,
<foreach collection="list" item="item" separator=",">
${item}
</foreach>
)
</if>
mybatisplus
拼接
entityWrapper.orderBy("FIELD(bar_code,"+ builder.toString() +")")
注意: 如果上述 list
中元素为 Integer 类型,则 ORDER BY field()
中的 foreach
中需要使用 ${}
取值