zoukankan      html  css  js  c++  java
  • MyBatis映射异常:nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter

    MyBatis映射异常:nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter

    nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'relationId' in 'class java.lang.Long'
    

    当时很不理解,

    实体类的一个属性,说在Long类里面没有get方法

    可是我的确是用了Data注解的,看了编译后的文件是存在get、set方法的

    后来在网上找到一个靠谱的解释

    我们都知道MyBatis在进行参数判断的时候,直接可以用 就可以了,如下:

    1、常规代码

    <update id="update" parametertype="com.cq2022.zago.order.entity.Test">
        update t_test_l
        <set>
            <if test="trnsctWayId != null">
                trnsct_way_id = #{trnsctWayId,jdbcType=TINYINT},
            </if>
            <if test="langId != null">
                lang_id = #{langId,jdbcType=INTEGER},
            </if>
        </set>
        where trnsct_way_l_id = #{trnsctWayLId,jdbcType=INTEGER}
    </update>
    

    2、特殊代码

    但是单个参数和多参数的判断有个不同点,当我们的入参为entity实体,或者map的时候,使用if 参数判断没任何问题。

    但是当我们的入参为java.lang.Integer 或者 java.lang.String的时候,这时候就需要注意一些事情了

    具体代码如下(咱们看着代码说,先展示错误代码):

    <select id="getTrnsctListByLangId" parameterType="java.lang.Integer" resultType="java.lang.Integer">
            select
            trnsct_id
            from  t_trnsct_way_l where
            <if test="langId != null" >
                and lang_id = #{langId}
            </if>
        </select>
    </mapper>
    

    上述代码存在一些问题,首先入参是java.lang.Integer, 而不是map或者实体的入参方式,对于这类单个入参然后用if判断的,mybatis有自己的内置对象,

    如果你在if判断里面 写的是你的入参的对象名,那就报异常:Internal error : nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'langId' in 'class java.lang.Integer'

    3、正确代码:

    <select id="getTrnsctListByLangId" parameterType="java.lang.Integer" resultType="java.lang.Integer">
            select
            trnsct_id
            from t_trnsct_way_l where
            <if test="_parameter != null">
                and lang_id = #{langId,jdbcType=INTEGER}
            </if>
        </select>
    

    微信截图_20200402173353.png

    ①使用MyBatis的内置对象_parameter,对于单个参数的传入和判断的话,必须用 _parameter来处理,而不是传入对项目langId

    ②这里最好加上数据类型

  • 相关阅读:
    JUnit报错 java.lang.Exception:No tests found matching
    tomcat配置好后,启动eclipse中的server,不能出现有猫的页面,提示404
    eclipse中的项目无法添加到server下?
    将web应用部署到Tomcat的三种方式
    启动eclipse弹出提示Version 1.7.0_79 of the JVM is not suitable for this product. Version: 1.8 or greater is required怎样解决
    EXISTS 与 NOT EXISTS 的用法及返回结果
    删除具有联合主键的记录
    序列化与反序列化
    tomcat 线程池
    Hibernate的实体类中为什么要继承Serializable?
  • 原文地址:https://www.cnblogs.com/mengw/p/12622071.html
Copyright © 2011-2022 走看看