itfsw / mybatis-generator-plugin

Mybatis Generator 代码生成插件拓展,增加:查询单条数据插件(SelectOneByExamplePlugin)、MySQL分页插件(LimitPlugin)、数据Model链式构建插件(ModelBuilderPlugin)、Example Criteria 增强插件(ExampleEnhancedPlugin)、Example 目标包修改插件(ExampleTargetPlugin)、批量插入插件(BatchInsertPlugin)、逻辑删除插件(LogicalDeletePlugin)、数据Model属性对应Column获取插件(ModelColumnPlugin)、存在即更新(UpsertPlugin)、Selective选择插入更新增强插件(SelectiveEnhancedPlugin)、Table增加前缀插件(TableSuffixPlugin)、自定义注释插件(CommentPlugin)、增量插件(IncrementsPlugin)、查询结果选择性返回插件(SelectSelectivePlugin)、乐观锁插件(OptimisticLockerPlugin)、LombokPlugin等拓展。

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

springboot2.0 使用batchInsertSelective接口发现的问题

LuckyDL opened this issue · 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版本配置解决这个问题