Mybatis插入返回自增主键报错

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/bobozai86/article/details/81981809

执行mybatis插入,配置返回主键如下:

@Insert("insert urun_plan.spider_record (id,taskid,starttime,endtime,receiver,spiderstatus,remarks,timestamp) value(#{record.id},#{record.taskid},#{record.starttime},#{record.endtime},#{record.receiver},#{record.spiderstatus,typeHandler=org.apache.ibatis.type.EnumTypeHandler},#{record.remarks},#{record.timestamp})")
    @Options(useGeneratedKeys=true, keyProperty="id",keyColumn="id"  )
    public int saveSpiderRecord(@Param("record")TaskRecord record);

总是报错:

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

解决办法:

@Options(useGeneratedKeys=true, keyProperty="id",keyColumn="id" )中keyProperty=”id”,换成keyProperty="record.id"则一切正常。因为本方法中,参数并不是直接用字段或者Map作为载体,而是对象,所以找不到id就是必然。

 

展开阅读全文

没有更多推荐了,返回首页