Mybatis @Select注解,使用in传入ids数组作为参数

在使用ids集合作为参数时,首先想应该是如下做法:

@Select("select id, name, user_id from label where id in 
<foreach collection=\"ids\" index = \"index\" item = \"id\" open= \"(\" separator=\",\" close=\")\"> 
#{id} 
</foreach>")
List<LabelDTO> getLabelsByIds(@Param("ids") List<Long> ids);

不知道为什么,一直提示获取不到id参数

后来在网上查了一下,最后得出如下正确做法:

    @Select({
            "<script>",
                "select",
                "id, name, user_id",
                "from label",
                "where id in",
                    "<foreach collection='ids' item='id' open='(' separator=',' close=')'>",
                    "#{id}",
                    "</foreach>",
            "</script>"
    })
    List<LabelDTO> getLabelsByIds(@Param("ids") List<Long> ids);

注意:

1,@Select后面的括号包含大括号

2, 使用<script>标签

3,@Select后面大括号中的代码,每行后面使用逗号结束

猜你喜欢

转载自blog.csdn.net/qq_2300688967/article/details/81186420