springboot2.0 使用batchInsertSelective接口发现的问题
LuckyDL opened this issue · 1 comments
最近项目中的springboot升级到的2.0版本,发现原来生成的batchInsertSelective这个接口不能正常使用了,原来生成的mapper如下:
<insert id="batchInsertSelective" keyColumn="camera_id" keyProperty="cameraId" parameterType="map" useGeneratedKeys="true">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
@project https://github.com/itfsw/mybatis-generator-plugin
-->
insert into camera
... ...
</insert>
mybatis版本:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
错误日志:
nested exception is org.apache.ibatis.executor.ExecutorException: Error getting generated key or setting result to parameter object. Cause: org.apache.ibatis.executor.ExecutorException: Could not determine which parameter to assign generated keys to. Note that when there are multiple parameters, 'keyProperty' must include the parameter name (e.g. 'param.id'). Specified key properties are [cameraId] and available parameters are [selective, list, param1, param2]
解决方法,开始把 keyProperty="cameraId"这个参数删掉了,发现主键无法回填,后来修改为了keyProperty="list.cameraId",接口可以正常使用,主键可以成功回填,如下所示:
<insert id="batchInsertSelective" keyColumn="camera_id" keyProperty="list.cameraId" parameterType="map" useGeneratedKeys="true">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.
@project https://github.com/itfsw/mybatis-generator-plugin
-->
insert into camera
... ...
</insert>
但是这种配置方式用在旧版本中,主键无法回填。
当mybatis版本升级到3.5.0之后会出现这个问题,插件版本1.2.18增加mybatis版本配置解决这个问题