数组作为SQL查询参数

数组作为sql的查询条件,其实就是让其符合如下格式

where id in (001,002,003)

若有一参数为

String[] ids = ["001","002","003"]

在mybatis中可以写为

where a.id in
   <foreach collection="array" item="id" separator="," open="(" close=")">
       #{id}
   </foreach>

其中item="id"的id为数组中遍历的每个元素

ex:

int deleteActivityByIds(String[] ids);
<delete id="deleteActivityByIds" parameterType="string">
    delete from tbl_activity where id in
    <foreach collection="array" item="id" separator="," open="(" close=")">
        #{id}
    </foreach>
</delete>
posted @ 2022-07-28 16:06  z-laoyao  阅读(2047)  评论(0编辑  收藏  举报