mybatis在设置自动返回主键,就报错org.apache.ibatis.binding.BindingException: Parameter 'id' not found

发布于 2021-11-30 22:55:01 字数 891 浏览 993 评论 11

大神们,请帮忙看看。添加了useGeneratedKeys="true" keyProperty="id"到这个SQL中,如下:

<insert id="add" parameterType="Teacher" useGeneratedKeys="true" keyProperty="id">

dao层方法中使用注解add(@Param(“teacher”) Teacher teacher),在调用时,就报错。如下:

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.executor.ExecutorException: Error getting generated key or setting result to parameter object. Cause: org.apache.ibatis.binding.BindingException: Parameter 'id' not found. Available parameters are [telecomOrder, param1]
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:73)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:364)
at com.sun.proxy.$Proxy17.insert(Unknown Source)

为什么会把keyProperty="id"中的id当做参数?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

小瓶盖 2021-12-05 08:24:59

博主写得很棒,这里推荐大家一个专注于Java开发的个人博客Queen's Blog(黛玛Queen),每天更新文章,干货满满哦,不容错过,需要的点这里咯。
http://www.marsitman.com/mybatis/mybatis-oracle-getid.html

你曾走过我的故事 2021-12-05 08:24:56

这个答案解决我的问题

丢了幸福的猪 2021-12-05 08:24:54

@Param(“teacher”) 时候 keyProperties="teacher.id"

英雄似剑 2021-12-05 08:24:37

谢谢指点。我只是在原项目框架上写代码,并未添加新的注解等内容。

悸初 2021-12-05 08:14:28

用框架,就别自己正各种幺蛾子,id,就对象中有,表中也有,一样的字段,保证没问题。你要用框架,又各种自由发挥的奇葩命名,自找麻烦。

陌上芳菲 2021-12-05 07:53:42

使用selectKey的话,不报错了。但是没取到返回的主键id。

离去的眼神 2021-12-05 06:47:14

insert id="insert"  parameterType="cn.org.lupa.ec.model.Resource"  useGeneratedKeys="true" keyProperty="id" > 

insert into resource (name,description,file,rtid,ispublic,status) values(#{name},#{description},#{file},#{rtid},#{ispublic},#{status}) 

<selectKey resultType="java.lang.Integer" order="AFTER" keyProperty="id"> 

SELECT LAST_INSERT_ID() 

</selectKey> 

</insert> 

转载自http://maosiyu.iteye.com/blog/answered_problems

屌丝范 2021-12-05 03:25:02

谢谢。这种方式试过了,还是报一样的错误。像是因为在dao层使用了param注解的原因。最后还是放弃了获得返回主键的方法。

终遇你 2021-12-04 21:22:42

回复
要么方法声明去掉注解,要么sql语句增加注解的内容作为前缀

傾城如夢未必闌珊 2021-12-04 01:40:47

回复
太赞了。sql语句增加注解的内容作为前缀,就可以了。

樱花落人离去 2021-12-04 00:18:20

配置有错误,

<insert id="insert" useGeneratedKeys="true" keyProperty="id" keyColumn="cc_id" parameterType="Ccbox">

keyProperty是实体类的属性,keyColumn是数据库表里面的列名

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文