jpa中无法使用mysql的field()函数解决办法
使用 sort() 自定义排序
/**
* 通过ID列表查询
* @param ids ID列表
* @return 查询结果
*/
// @Query(value = "select l from LabelDO l where l.id in ?1 order by field(l.id, ?1)")
List<LabelDO> findByIdIn(List<Long> ids);
/**
* 根据给定的id列表进行排序
* @param ids id列表
* @return 排序结果
*/
default List<LabelDO> findByIdInAndSort(List<Long> ids) {
List<LabelDO> temp = this.findByIdIn(ids);
sortList(ids, temp);
return temp;
}
private void sortList(List<Long> ids, List<LabelDO> labels) {
try {
labels.sort((o1,o2)->{
int io1 = ids.indexOf(o1.getId());
int io2 = ids.indexOf(o2.getId());
return io1 - io2;
});
} catch (Exception e) {
e.printStackTrace();
}
}