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


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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM