[Solved] Error getting generated key or setting result to parameter object. UnsupportedOperationException

Mybatis batch inserts and returns the primary key to the original list

report errors

Error getting generated key or setting result to parameter object. UnsupportedOperationException

reason

The version before mybatis 3.3.1 does not support the function of batch adding and returning the primary key ID; Some versions after mybatis # 3.3.1 support batch insert return ID, but do not support multiple parameters of Dao layer batch insert function, @ param annotation; (that is, table splitting is not supported) mybatis versions after 3.5.1 support batch insertion, return ID, multiple parameters, and @ param annotation

Solution:

Upgrade mybatis 3.5.1 (and ensure that all mybatis versions that Maven depends on are greater than 3.5.1) Dao

void batchInsert(@Param("idx") Integer idx, @Param("list") List<DO> list);

mapper

    <insert id="batchInsert" useGeneratedKeys="true" keyProperty="list.id">
        insert into do_${idx} (name, address)
        values
        <foreach collection="list" item="item" separator=",">
            (#{item.name},#{item.address})
        </foreach>
    </insert>

Note: because the function has multiple parameters, it is impossible to determine which parameter ID is taken, so it needs to indicate that it is list in keyproperty = “list.ID”.

Read More: