mybatis传入数组并且遍历

来源:互联网 发布:上海云计算公司 编辑:程序博客网 时间:2024/06/05 04:30
 <select id="selectByTagIds" resultMap="BaseResultMap" parameterType="java.lang.String" >    select    <include refid="Base_Column_List" />    from hb_information_tags    where 1=1    <if test="tagIds!=null">      and tag_id in       <foreach collection="array" item="tagId" index="index"               open="(" close=")" separator=",">        '${tagId}'      </foreach>    </if>  </select>

这里传入的array是一个数组。通过foreach封装成一个类似于(1,2,3)的字符串。(注意,传入的数组必须命名为array)

当传入的tagIds是一个类似于”1,2,3”的字符串时,语句写成即可:

 and tag_id in      (${tagIds})