MyBatis3錯誤:Parameter 'id' not found. Available parameters are [arg2, arg1, arg0, param3, param1, param2]或者Parameter '0' not found. Available parameters are [arg2, arg1, arg0, param3, param1, param2]


這個問題涉及到MyBatis3在使用select節點查詢時傳遞多個參數的問題。問題分析如下:

1、如果是單個查詢一般是這樣配置:

    <select id="getUserArticles" parameterType="int" resultMap="resultUserArticleList">
        select user.id,user.userName,user.userAddress,article.id as aid,article.title,article.content from user,article where user.id=article.userid and user.id=#{id} </select>
public List<Article> getUserArticles(int id);

2、如果使用了多個參數之后,我們一般是這樣配置:

    <select id="getUserArticlesByLimit" parameterType="int" resultMap="resultUserArticleList">
        select user.id,user.userName,user.userAddress,article.id as aid,article.title,article.content from user,article where user.id=article.userid and user.id=#{id} limit #{start},#{limit} </select>
public List<Article> getUserArticlesByLimit(int id,int start,int limit);

而以上寫法有兩個錯誤的地方,第一個是parameterType是要去掉的,雖然這里的參數全部都是int類型,如果涉及多個類型那就必須去掉;第二個是#{id}...及后面的參數寫法也是錯誤的,因為在MyBatis3中不會識別這樣的參數,所以應該改成#{arg0},{arg1}這樣的形式。

所以,修改后的配置如下:

    <select id="getUserArticlesByLimit" resultMap="resultUserArticleList">
        select user.id,user.userName,user.userAddress,article.id as aid,article.title,article.content from user,article where user.id=article.userid and user.id=#{arg0} limit #{arg1},#{arg2} </select>

通過以上修改完后,錯誤解決

3、當然,這個涉及到了MyBatis3的多參數查詢方法,還有很多解決方法,比如使用HashMap類型、對參數進行@Param注解、使用對象傳輸等的參數進行裝載,詳細參考:http://www.cnblogs.com/mingyue1818/p/3714162.html


免責聲明!

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



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