There is no getter for property named ‘xxxxx’ in ‘class com.xxx.xx.xx.xxxx'”,

原文链接:There is no getter for property named ‘xxxxx’ in ‘class com.xxx.xx.xx.xxxx’”, – 每天进步一点点 (longkui.site)

 

0.背景

SpringMVC架构,使用mybatis执行insert语句,然后开始报错:

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named ‘xxxxx’ in ‘class com.xxx.xxx.api.xxxx'”,

mapper层的写法:

void insertStudent(Student student);

xml层的写法:

 <insert id="insertStudent" >
  insert into stu(id,name)
  values
  (#{id},#{name})
 </insert >

然后就开始报上面的错误了。

1.问题原因及解法方法
  • 实体类缺失getter/setter 方法。 解决办法,加入getter/setter方法
  • 实体类名与数据库类名不符。这个一般遇不到,而且可以不一定要对应。
  • 实体类名与 xml 中的 property 名不符, xml 中 column 名与 property 名不符。 解决办法: 这个如果你要做一一对应的话就写成一样的,要不然自己写参数对应也行。
  • 编写 sql 语句不要多空格少逗号之类的,不要写错少些字母。解法办法:请仔细检查sql语句。

最后,就是我实际遇到的,mapper层和xml层写错了。

上面的mapper层代码应该改成下面这样:

void insertStudent(@Param("student") Student student);

xml层应该改成下面这样:

 <insert id="insertStudent"  parameterType="com.xxx.xxx.api.Student">
  insert into stu(id,name)
  values
  (#{student.id},#{student.name})
 </insert >

PS: 不要过度依赖代码生成器,有的代码自己写两遍才知道问题所在。

posted on   longkui  阅读(124)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示