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

14160 ワード

問題の説明
Mybatisを使用してデータベースのエラーを問合せます.
org.apache.ibatis.binding.BindingException: Parameter 'idList' not found

インタフェースは次のとおりです.
public List<User> findByIdList(List<Integer> idList);

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>

運転エラー: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}

このように見ると分かりますか?Mybatisは、パラメータを順次伝達する.xmlでパラメータのnameで取得するには、@Param("")注記を付けなければなりません.そうしないと、Mybatisのデフォルトの書き方しか使用できません.
解決策
解決策は2つあります.
Mybatisデフォルトの書き方-list
1つ目の書き方は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>

注釈の使用
2つ目の方法は、@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>

Mybatisデフォルトパラメータ伝達方式
Mybatisの様々なタイプの単一パラメータのデフォルトの書き方は以下の通りです.
を選択します.
受信パラメータ方式
基本データ型
順序は、#{0}のように、nameで直接取得してもよい、例えば#{name}List
list
はいれつ
array
Map
keyに基づいてmapの各パラメータを取得すればよい、例えば#{key}カスタムオブジェクト
getメソッドに対応するパラメータに基づいて、nameを使用して取得すればよい.例えば、#{name}public User find(String address, List idList)のようなマルチパラメータの場合、注釈@Param("")を使用するか、mapにカプセル化された伝達を考慮する.