MySQL可以通过field()函数自定义排序
遇到一个需求,需要根据前端入参的参数顺序,查询后顺序返回对应的数据;

正常编写sql

在这里插入图片描述

自定义排序函数field()加持sql(得到了想要的结果)

在这里插入图片描述

MyBatis中这个样子

<select id="selServiceItemsByIdList" parameterType="com.xxx.entity.TServiceItems" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from t_service_items
    where s_id in
    <foreach collection="list" index="index" item="item" open="(" separator="," close=")">
      #{item}
    </foreach>
    ORDER BY FIELD
    <foreach collection="list" item="item" open="(s_id," separator="," close=")">
      #{item}
    </foreach>
  </select>
Logo

更多推荐