javamybatissqlmap

Why doesn't mybatis3 support Set foreach?


If parameterType is java.util.Set, or its implementation, doing foreach with this parameter throws java.lang.UnsupportedOperationException.

<select id="selectList" parameterType="java.util.HashSet" resultMap="someMap">
    SELECT key FROM tb_my_table
    WHERE value IN (
        <foreach collection="set" item="item" separator=",">
        #{item}
        </foreach>
    )
</select>

The cause inside mybatis3 is CollectionWrapper doesn't implement get method and just throws an exception.
I want to know whether it is intended design and the reason.


Solution

  • The documentation states that using Sets is possible:

    You can pass any Iterable object (for example List, Set, etc.), as well as any Map or Array object to foreach as collection parameter.

    I could reproduce exception with Mybatis 3.2.7.

    But it 's working fine with Mybatis 3.3.0.

    Note that Expected name for single parameter is collection:

    <select id="selectList" parameterType="java.util.HashSet" resultMap="someMap">
        SELECT key FROM tb_my_table
        WHERE value IN (
            <foreach collection="collection" item="item" separator=",">
            #{item}
            </foreach>
        )
    </select>