First of all, inserting a record to return the Mybatis version requires a low point, while batch inserting returns with primary keys requires upgrading to version 3.3.1, and before 3.3.0, none of them are possible.
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>3.3.1</version></dependency>
1.MySQL
<insert id="insertByBatch" useGeneratedKeys="true" keyProperty="id" parameterType="java.util.List"> insert into test (sblsh, xh, jsjg, is_success, is_display, gmt_create, gmt_modify, create_user) values <foreach collection="list" item="item" index="index" separator=","> ( #{item.sblsh,jdbcType=BIGINT}, #{item.xh,jdbcType=CHAR}, #{item.jsjg,jdbcType=BIGINT}, #{item.isSuccess,jdbcType=TINYINT}, #{item.isDisplay,jdbcType=TINYINT}, SYSDATE(), SYSDATE(), #{item.createUser,jdbcType=VARCHAR}) </foreach> </insert>Use GeneratedKeys="true" keyProperty="primary key field"
2.Oracle
<insert id="insertBatch"> <selectKey keyProperty="id" resultType="Long" order="BEFORE"> select test.nextval as id from dual </selectKey> insert into test (id,value,gmt_create,gmt_modified) select test.nextval, A.* FROM ( <foreach collection="list" item="item" index="index" separator="union all"> select #{item.value,jdbcType=VARCHAR}, #{item.gmtCreate,jdbcType=DATE}, #{item.gmtModified,jdbcType=DATE} from dual </foreach> ) A </insert>The above article Mybatis insertion or batch insertion returns an example with self-growth primary key record is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.