プロジェクトのシナリオ:
提示:黑马新版web项目,
追加、削除、変更、確認のための条件付きクエリ
問題の説明:
データベースへの接続に失敗しました。最初は mybatis のせいだと思いましたが、後で XML 構成の SQL 構文の問題であることがわかりました。
問題のキーワード: パラメータ「xxxxx」が見つかりません
エラーメッセージ
Caused by: org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: org.apache.ibatis.binding.BindingException: Parameter 'brandName' not found. Available parameters are [size, param3, begin, brand, param1, param2]
### Cause: org.apache.ibatis.binding.BindingException: Parameter 'brandName' not found. Available parameters are [size, param3, begin, brand, param1, param2
原因分析:
問題コード:
<select id="selectByPageAndCondition" resultMap="brandResultMap" >
select *
from tb_brand
<where>
<if test="brand.brandName != null and brandName != '' ">
and brand_name like #{brand.brandName}
</if>
<if test="brand.companyName != null and companyName!='' ">
and company_name like #{brand.companyName}
</if>
<if test="brand.status != null">
and status = #{brand.status}
</if>
</where>
limit #{begin} , #{size}
</select>
問題の分析: この問題のいくつかの状況
1. マッパーインターフェイスには @Param(xxx) アノテーションはありませんが、xxx. 属性が使用されるか、複数の変数が渡されます。
2. アノテーションの追加では、xxx.attribute を使用して属性名を直接使用しません(私の間違い)
解決:
属性の前にオブジェクトを追加し、それを object に変更します。
<select id="selectByPageAndCondition" resultMap="brandResultMap" >
select *
from tb_brand
<where>
<if test="brand.brandName != null and brand.brandName != '' ">
and brand_name like #{brand.brandName}
</if>
<if test="brand.companyName != null and brand.companyName!='' ">
and company_name like #{brand.companyName}
</if>
<if test="brand.status != null">
and status = #{brand.status}
</if>
</where>
limit #{begin} , #{size}
</select>