有如下表table_people
id name
1 dwyane
2 james
3 paul
4 bosh
現在將查詢出的數據按照id 3、4、1、2排序
先把id數據按照一定順序放到一個List中
1 List<Integer> ids = new ArrayList<Integer>(); 2 ids.add(3); 3 ids.add(4); 4 ids.add(1); 5 ids.add(2);
mybits sql如下
<select id="findHistory" resultMap="recentlyBrowse"> select * from( <foreach collection="list" item="id" index="index" open="(" close=")" separator="union all"> select * from table_people where id=#{id,jdbcType=DECIMAL} </foreach> ) </select>
把ids作為參數傳遞給查詢的dao方法
@Autowired SqlSessionTemplate sql; public List<People> findHistory(List<Integer> ids){ return sql.selectList(NAMESPACE + "findHistory", ids); }