天天看點

org.apache.ibatis.binding.BindingException: Parameter 'receptionList' not found. Available parameter

今天遇到mybatis批量儲存報錯

org.apache.ibatis.binding.BindingException: Parameter 'receptionList' not found. Available parameters are [list, collection]

就其原因是因為在xml中寫SQL的時候

<insert id="batchInsert" parameterType="smsReceptionList">

        INSERT INTO t_sms_reception

        (

            SMS_SYS,

            SUBCODE,

            PHONE_NUM,

            SMS_CONTENT,

            REQUEST_TIME,

            READ_FLAG

        )

        VALUES

        <foreach collection="list" separator ="," item="smsReception">

            (

                #{smsReception.smsSys},

                #{smsReception.subcode},

                #{smsReception.phoneNum},

                #{smsReception.smsContent},

                #{smsReception.readFlag}

            )

        </foreach>    

    </insert>

根據報錯日志分析,是MyBatis在解析xml時找不到其中聲明的studentNameList,但是在Dao中明明傳的參數就是studentNameList,怎麼會報錯呢?

查詢了一下MyBatis官方的說明文檔,終于找到了原因,在http://mybatis.github.io/mybatis-3/zh/dynamic-sql.html#foreach裡有一段說明:

寫道 注意 你可以傳遞一個 List 執行個體或者數組作為參數對象傳給 MyBatis。當你這麼做的時 候,MyBatis 會自動将它包裝在一個 Map 中,用名稱在作為鍵。List 執行個體将會以“list” 作為鍵,而數組執行個體将會以“array”作為鍵。

因為我傳的參數隻有一個,而且傳入的是一個List集合,是以mybatis會自動封裝成Map<"list",studentNameList>。在解析的時候會通過“list”作為Map的key值去尋找。但是我在xml中卻聲明成studentNameList了,是以自然會報錯找不到。

繼續閱讀