有如下表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);
}