org.apache.ibatis.executor.ExecutorException: No setter found for the keyProperty 'id'

问题场景:

db中表的主健采用了自增的方式

mybatis的配置如下:


org.apache.ibatis.executor.ExecutorException: No setter found for the keyProperty 'id'_第1张图片

但是在bean中并没有定义id属性,导致报错org.apache.ibatis.executor.ExecutorException: No setter found for the keyProperty 'id'

解决办法:

在配置中删除keyProperty="id"的配置

原因:

主健采用了自增的方式只需要配置:useGeneratedKeys="true"

keyProperty="id"的意思是 将主键的中的数据 放到传入对象的成员变量id里面,因为在对像的成员变量中没有定义属性id,也没有定时id属性的get,set方法,所以会报错:org.apache.ibatis.executor.ExecutorException: No setter found for the keyProperty 'id'  

如果db的表中没有指定主键:可以用属性keyColumn

useGeneratedKeys=true,keyProperty="userId",keyColumn="user_id"

这个注解的意思就是,使用数据库自动增长的主键,并从table中user_id字段里面把数据放到传入对象的成员变量userId里面。

如果我们已经在数据库表中指定了主键,那么keyColumn属性可以缺省。

你可能感兴趣的:(org.apache.ibatis.executor.ExecutorException: No setter found for the keyProperty 'id')