MyBatis: getting id from inserted array of object

2019-02-10 16:09发布

问题:

I use mybatis 3.3.0-SNAPSHOT. I want to insert list of objects, and get id of every object. In interface I have:

public void createCore(@Param("cores")List<Object> cores);  

In xml mapper I have:

<insert id="createCore" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
  INSERT INTO mytable (raz,dva,tri )
  VALUES
    <foreach collection="cores" item="core" separator=",">
     (#{core.raz}, #{core.dva}, #{core.tri})
    </foreach>
</insert>

And I get:

Error updating database. Cause: org.apache.ibatis.executor.ExecutorException: Error getting generated

key or setting result to parameter object. Cause: org.apache.ibatis.binding.BindingException: Parameter 'id' not found. Available parameters are [cores, param1]

I tried and keyProperty="core.id" but get the same error.

How to fix it?

回答1:

This is issue is fixed in version 3.3.1



回答2:

Got that error too while trying to insert multiple rows with usegeneratedkeys.

The workaround fix I found was mentioned here:http://mybatis-user.963551.n3.nabble.com/MyBatys-Batch-Insert-doubt-error-retrieving-generated-id-binding-to-POJO-td4029977.html

All you need is to rename the param to "list". so "cores" -> "list" in the example above, and auto generated ids are working.



标签: java mybatis