溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

mybatis對傳入基本類型參數(shù)的判斷方式有哪些

發(fā)布時間:2022-03-14 09:16:58 來源:億速云 閱讀:367 作者:小新 欄目:開發(fā)技術

這篇文章主要介紹mybatis對傳入基本類型參數(shù)的判斷方式有哪些,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

對傳入基本類型參數(shù)的判斷

mybatis的xml文件的sql語句中parameterType為基本類型,如:

<select id="getCustomer" parameterType="Integer" resultType="Customer">
    select * from customer
    where
    <if test="id != null">id=#{id}</if>
<select>

會報錯:There is no getter for property named 'id' in 'class java.lang.Integer'

這是因為Integer對象中沒有id屬性

解決辦法

<select id="getCustomer" parameterType="Integer" resultType="Customer">
    select * from Customer
    where
    <if test="_parameter != null">id=#{_parameter}</if>
<select>

即將接收參數(shù)的參數(shù)名改為_parameter,注意改成其他參數(shù)名沒用。

傳入基本類型參數(shù)時test判斷報錯

在使用mybatis的時候出現(xiàn)了這樣的問題:

//Dao層的接口中的代碼
List<Map<String,Object>> getName(String username);
//對應的mapper中的代碼
<select id="getName" resultType="java.util.Map">
    select name,client_id
    from table1
    <where> 
        <if test=" username!= null and username!='' ">
            and username= #{id}
        </if>
    </where> 
</select>

//報的錯誤
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: 
There is no getter for property named 'username' in 'class java.lang.String'

分析

There is no getter for property named &lsquo;username&rsquo; in &lsquo;class java.lang.String&rsquo;,這句話打大概意思是:在“class java.lang.String”中沒有名為“username”的屬性的getter方法。因為mybatis默認采用ONGL解析參數(shù),所以會自動采用對象樹的形式取string.num值,引起錯誤。

解決辦法

if test中的id用_parameter替換,而實際的語句不需要修改and a.id =#{id},因為Mybatis當只傳入一個參數(shù)時#{ } 中的內(nèi)容沒有要求。

在Mapper中給出入?yún)⒃O置名稱,例:public &hellip; getName(@Param(“username”) String username);這樣修改后我們前面的寫法就不會報錯了。

以上是“mybatis對傳入基本類型參數(shù)的判斷方式有哪些”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關知識,歡迎關注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI