简体   繁体   中英

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM