詳解Mybatis 傳遞參數(shù)類型為List的取值問題
問題描述:
參數(shù)傳遞為List時:
當傳遞一個 List 實例或者數(shù)組作為參數(shù)對象傳給 Mybatis。此時,Mybatis 會自動將它包裝在一個 Map 中,用名稱在作為鍵。List 實例將會以“l(fā)ist” 作為鍵,而數(shù)組實例將會以“array”作為鍵。所以,當我們傳遞的是一個List集合時,mybatis會自動把我們的list集合包裝成以list為Key值的map。
DAO 層:
List<User> selectUserByIDs( List IDs);
XML文件:
<select parameterType='java.util.List' resultType='user'> select * from user <where> <if test='IDs != null and IDs.size() >0'><foreach collection='IDs' open=' and id in (' close=')' item='uid' separator=','> #{uid}</foreach> </if> </where></select>
報錯信息:
org.apache.ibatis.binding.BindingException: Parameter ‘IDs’ not found. Available parameters are [collection, list]
解決方法:
方法一:將我們的XML中collection屬性值直接設(shè)置為list
DAO 層:
List<User> selectUserByIDs( List IDs);
XML文件:
<select parameterType='java.util.List' resultType='user'> select * from user <where> <if test='list != null and list.size() >0'><foreach collection='list' open=' and id in (' close=')' item='uid' separator=','> #{uid}</foreach> </if> </where></select>
方法二: 利用注解@Param指定我們的入?yún)⒚Q
DAO層:
List<User> selectUserByIDs(@Param('IDs') List IDs);
XML文件:
<select parameterType='java.util.List' resultType='user'> select * from user <where> <if test='IDs != null and IDs.size() >0'><foreach collection='IDs' open=' and id in (' close=')' item='uid' separator=','> #{uid}</foreach> </if> </where></select>
到此這篇關(guān)于詳解Mybatis 傳遞參數(shù)類型為List的取值問題的文章就介紹到這了,更多相關(guān)Mybatis 傳遞參數(shù)類型為List的取值內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. MySQL中 concat函數(shù)的使用2. 快速刪除ORACLE重復(fù)記錄3. MySQL Community Server 5.1.494. Mysql入門系列:MYSQL創(chuàng)建、刪除、索引和更改表5. 數(shù)據(jù)庫相關(guān)的幾個技能:ACCESS轉(zhuǎn)SQL6. 導(dǎo)出錯誤編碼的mysql數(shù)據(jù)庫7. MYSQL數(shù)據(jù)庫存文本轉(zhuǎn)存數(shù)據(jù)庫問題8. Delphi中的Access技巧集9. 整理Oracle數(shù)據(jù)庫碎片10. Oracle災(zāi)難防護的關(guān)鍵技術(shù)

網(wǎng)公網(wǎng)安備