zoukankan      html  css  js  c++  java
  • 使用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);
        }
  • 相关阅读:
    memcached 高级机制(一)
    memcached 简介
    Hibernate
    Linux中的搜索命令
    Linux的常用命令
    Git清除用户名和密码
    关于Git的简单使用
    文件的上传与下载(2)
    关于文件的上传和后台接收
    验证码的制作
  • 原文地址:https://www.cnblogs.com/simeone/p/4030378.html
Copyright © 2011-2022 走看看