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

②這里最好加上數據類型


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM