mybatis自定义sql insert报错:selectKey不起作用、以及报错找不到getter方法

在mybatis的xml配置文件中,如果用自定义insert语句,需要注意如下两点:

1)如果要使用selectKey提前自动生成字段ID,就在mapper interface方法定义中,如果使用@param。就需要在使用insert语句中,通过@param指定的名称来方位bean的属性;如果不使用@param就可以省去名称。

下面是使用@param的写法(含mapper interface和xml):

在这里插入图片描述在这里插入图片描述
下面是不使用@param的写法(含mapper interface和xml):

在这里插入图片描述

就这个例子来说,不使用@param会更简洁。

2)使用selectKey获取多列数据时,千万不要在keyProperty中的列之间加空格(一般处于编码习惯,都会自然而然的加一个空格)。如下图:

在这里插入图片描述
否则,会报错如下:
ERRORServlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.executor.ExecutorException: No setter found for the keyProperty ’ timestamp’ in com.huaguang.observ.model.ch.SpanSnapshot.] with root cause
org.apache.ibatis.executor.ExecutorException: No setter found for the keyProperty ’ timestamp’ in com.huaguang.observ.model.ch.SpanSnapshot.
at org.apache.ibatis.executor.keygen.SelectKeyGenerator.setValue(SelectKeyGenerator.java:117)
在这里插入图片描述
正确的写法,去掉空格,如下图:
在这里插入图片描述
然后代码就能够正常运行了。

  • 9
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值