当前位置: 代码网 > it编程>编程语言>Java > 解决Mybatis报错:org.apache.ibatis.reflection.ReflectionException: There is no getter for property named问题

解决Mybatis报错:org.apache.ibatis.reflection.ReflectionException: There is no getter for property named问题

2025年01月16日 Java 我要评论
错误提示服务器处理发生异常:nested exception is org.apache.ibatis.reflection.reflectionexception: there is no gett

错误提示

服务器处理发生异常:

nested exception is org.apache.ibatis.reflection.reflectionexception: there is no getter for property named ‘usertaskquerydto’ in ‘class com.lz.platform.trauma.api.interfaces.dto.task.usertaskquerydto’

解决方式

可能错误原因一

解决方法一:

dto没有写getter/setter方法,需要添加上。springboot在dto上加@data

@data是一个lombok提供的注解,可以自动为java类生成一些常用的方法,包括getter、setter、tostring、equals、hashcode等方法,从而简化java类的编写。

使用@data注解可以让代码更加简洁,提高开发效率。

@data
@apimodel(value = "模糊查询列表")
public class usertaskquerydto implements serializable {
    private string idno;

    private string  code;

    private string keyword;
}

可能错误原因二

解决方法二:

dao文件上没有加上注入方法@repository

dao文件上需要加上@repository

@repository是spring框架中的一个注解,用于标识一个类作为数据访问对象(dao)的组件。它的作用是将dao层的bean标记为spring容器中的bean,并自动执行bean的注册、依赖注入等操作。

可能错误原因三

解决方法三:

java中mapper文件不识别#{dto.propertyname}语法,将dto.去掉

mapper文件中方法dto注入不能带上dto名称。

错误mapper:

 <select id="queryusertraumatasklist" resulttype="com.lz.task.traumatask"  parametertype="com.lz.task.usertaskquerydto">
        select * from   task t where 1=1
        <if test=" usertaskquerydto.no != null and  usertaskquerydto.no != ''">
            and t.no = #{usertaskquerydto.no}
        </if>

        <if test="usertaskquerydto.keyword != null and usertaskquerydto.keyword !=''">
            and ( t.sterno like concat( concat('%',#{usertaskquerydto.keyword}),'%') or
            t.name like concat( concat('%',#{usertaskquerydto.keyword}),'%') or
            t.iclass like concat( concat('%',#{usertaskquerydto.keyword}),'%'))
        </if>

        order by t.date desc
    </select>

按照上边的写法会出现报错: 

服务器处理发生异常:nested exception is org.apache.ibatis.reflection.reflectionexception: there is no getter for property named ‘usertaskquerydto’ in ‘class com.lz.platform.trauma.api.interfaces.dto.task.usertaskquerydto’

下边的写法才是正确的写法: 

把dto去掉之后,报错就消失了,加了dto系统读取不到,去掉之后句好了

  <select id="queryusertraumatasklist" resulttype="com.lz.task.traumatask"  parametertype="com.lz.task.usertaskquerydto">
        select * from   task t where 1=1
        <if test=" no != null and  no != ''">
            and t.no = #{no}
        </if>

        <if test="keyword != null and keyword !=''">
            and ( t.sterno like concat( concat('%',#{keyword}),'%') or
            t.name like concat( concat('%',#{keyword}),'%') or
            t.iclass like concat( concat('%',#{keyword}),'%'))
        </if>

        order by t.date desc
    </select>

错误原因

在mapper文件中,使用dto(data transfer object)来传递数据,可以将数据从java代码传输到数据库,也可以将数据从数据库传输到java代码。

通常情况下,我们将dto中的属性名与数据库表的列名进行对应,以便在mapper文件中使用这些属性来构建sql语句。

  • 在使用dto的属性名填充mapper文件数据时,不需要在属性名前面添加dto名称。这是因为dto通常是一个java类,而不是一个数据库表,它的属性名称是在java类中定义的,而不是在数据库表中定义的。
  • 在mapper文件中,我们使用#{propertyname}来引用dto的属性。如果我们在属性名前面添加了dto名称,例如#{dto.propertyname},则会导致语法错误,因为mapper文件不能识别这个语法。

因此,在使用dto的属性名填充mapper文件数据时,应该只使用属性名本身,而不需要添加dto名称。

例如,如果dto中有一个属性名为id,我们可以在mapper文件中使用#{id}来引用这个属性,而不需要使用#{dto.id}。

可能错误原因四

解决方法四:

sql语句中的属性名称,跟接收,或者入参的属性名称不一致

例如:我传进来的参数名称为tmid,但是我sql语句里边写的确是tmhosid

把属性名称改为一致就可以了

注意:

  • 出参和入参的类型不要弄错了
  • 出参是一个list的话,resulttype放的是list参数的类型。
  • 例如:出参list《traumatask》 ,resulttype放的就是traumatask路径
  • 出参list《string 》,resulttype放的就是java,util.lang

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com