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:
- Using mybatis statement.getGenreatedKeys(); usegeneratedkeys = “true”; using self incrementing primary key to get primary key value policy and Oracle do not support self incrementing, Oracle uses sequence
- Mapper.xml Error: Error setting non null for parameter #3 with JdbcType null.
- [Solved] mybatis plus Insert Error: mybatis plus Error setting null for parameter #1 with JdbcType OTHER
- [Solved] asList().add() error: UnsupportedOperationException
- Mybatis sets the primary key Auto-Increment error: No setter found for the keyProperty
- Elasticsearch Startup Error: unable to install syscall filter: java.lang.UnsupportedOperationException: seccomp
- Mybatis single parameter pass in exception (How to Fix)
- [Solved] Mybatis Error: attempting to get column ‘XX’ from result set
- [Solved] Specified key was too long; max key length is 767 bytes
- How to Solve the Primary Key of mybatisPlus Inserted Data is too Large Issue
- The Java class generated by protocol reports an error: cannot access
- [Solved] JPA sett in parameter error: Java.lang.illegalargumentexception
- [Solved] ES Error: request contains unrecognized parameter [ignore_throttled]
- [Solved] IO exception: NL exception was generated
- [Solved] Error attempting to get column ‘xxxx_time‘ from result set. Cause: java.sql.SQLFeatureNotSupportedEx
- Flink Error: is not serializable. The object probably contains or references non serializable fields.
- [Solved] stream Convert list to map Error: java.lang.IllegalStateException: Duplicate key
- [Solved] Java.lang.IllegalStateException: Type handler was null on parameter mapping for property ‘__frch_it
- List: How to de-duplication according to an attribute of an object
- [Solved] swagger Docmentation Access Error: Illegal DefaultValue 1024 for parameter type integer, java.lang.NumberFormatException