org.apache.ibatis.binding.BindingException: Parameter 'idList' not found解決辦法



https://blog.csdn.net/qq_28379809/article/details/83342196 

問題描述
使用Mybatis查詢數據庫報錯:
org.apache.ibatis.binding.BindingException: Parameter 'idList' not found
1
接口是這樣的:
public List<User> findByIdList(List<Integer> idList);
1
XML是這樣的:
<select id="findByIdList" resultType="com.example.bean.User">
SELECT id, name, password, address, enable
FROM user
<where>
        <if test="idList != null and idList.size() > 0">
            id IN
            <foreach collection="idList" item="ietm" index="index" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
</where>
</select>
1
2
3
4
5
6
7
8
9
10
11
12
運行報錯:org.apache.ibatis.binding.BindingException: Parameter 'idList' not found.
原因分析
Mybatis傳遞參數是按位置傳遞的,也就是說下面一個接口:public User find(String name, String password), XML中使用參數是這樣的select * from user where name = #{0} and password = #{1}.
如果想要按值傳遞,就得這樣寫:
// 接口
public User find(@Param("name")String name, @Param("password")String password)
<!-- xml -->
select * from user where name = #{name} and password = #{password}
1
2
3
4
5
這樣一看是不是明白了?Mybatis是按順序傳遞參數的。
想要在xml中通過參數的name獲取,就得加上@Param("")注解,不然就只能使用Mybatis默認的寫法。
解決辦法
解決辦法有兩種:
Mybatis默認寫法——list
第一種寫法是使用Myabtis默認的寫法, 在xml中用list接收參數,如下:
// 接口
public List<User> findByIdList(List<Integer> idList);
<select id="findByIdList" resultType="com.example.bean.User">
SELECT id, name, password, address, enable
FROM user
<where>
        <if test="list!= null and list.size() > 0">
            id IN
            <foreach collection="list" item="ietm" index="index" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
</where>
</select>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
使用注解
第二種方式就是使用@Param("")注解,如下:
// 接口
public List<User> findByIdList(@Param("idList")List<Integer> idList);
<select id="findByIdList" resultType="com.example.bean.User">
SELECT id, name, password, address, enable
FROM user
<where>
        <if test="idList!= null and idList.size() > 0">
            id IN
            <foreach collection="idList" item="ietm" index="index" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
</where>
</select>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Mybatis默認參數傳遞方式
Mybatis關於各種類型的單參數默認的寫法如下:
類型 接收參數方式
基本數據類型 順序,如#{0},也可以用name直接獲取,如#{name}
List list
數組 array
Map 根據key獲取map中各參數即可,如#{key}
自定義的對象 根據get方法對應的參數,使用name獲取即可,如#{name}
如果是多參數,比如public User find(String address, List<Integer> idList), 使用注解@Param("")或者考慮封裝在map中傳遞。
--------------------- 
作者:eknows 
來源:CSDN 
原文:https://blog.csdn.net/qq_28379809/article/details/83342196 
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!

 


免責聲明!

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



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