使用MyBatis查询数据,按特定顺序排序

有如下表table_city

id      city
1      beijing
2      shanghai
3      hanghzou
4      shenzhen

现在将查询出的数据按照id 3、4、1、2排序

先把id数据按照一定顺序放到一个List中

List<Integer> ids = new ArrayList<Integer>();
ids.add(3);
ids.add(4);
ids.add(1);
ids.add(2);

MyBatis mapper sql如下

<select id="findHistory" resultMap="recentlyBrowse">
    select * from
    <foreach collection="list" item="id" index="index"
        open="(" close=")" separator="union all">
        select * from table_city where id=#{id,jdbcType=DECIMAL}
    </foreach> as city
</select>

把ids作为参数传递给查询的dao方法

@Autowired
SqlSessionTemplate sql;

public List<People> findHistory(List<Integer> ids) {
    return sql.selectList(NAMESPACE + "findHistory", ids);
}

对于Mapper接口,如下:

public interface TableCityMapper {
    List<City> findHistory(@Param("list") List<Integer> ids);
}
上一篇 Java图片缩略图裁剪水印缩放旋转压缩转格式-Thumbnailator图像处理
下一篇 MyBatis根据List批量查询List结果排序问题
目录
文章列表
1 CSS3 grayscale滤镜图片变黑白
CSS3 grayscale滤镜图片变黑白
2
React Native混合原生开发之集成RN到原生项目与RN首屏白屏优化
React Native混合原生开发之集成RN到原生项目与RN首屏白屏优化
3
Kubernetes修改NodePort的范围
Kubernetes修改NodePort的范围
4
元素唯一的LinkedBlockingQueue阻塞队列
元素唯一的LinkedBlockingQueue阻塞队列
5
使用uni-app开发小程序
使用uni-app开发小程序
最新评论
一位WordPress评论者
一位WordPress评论者
2月12日
您好,这是一条评论。若需要审核、编辑或删除评论,请访问仪表盘的评论界面。评论者头像来自 Gravatar。