使用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