项目场景:
提示:黑马新版web项目,
增删改查的条件查询
问题描述:
连接数据库失败,一开始以为是mybatis的原因,后面发现是xml配置sql语法问题
问题关键字:parameter 'xxxxx' not found
报错信息
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,没有在mapper接口中加注解@Param(xxx) 却使用 xxx.属性 或传入多个变量
2,加了注解没有用 xxx.属性 直接使用属性名 (我犯的错误)
解决方案:
在属性前面加上对象,改成 对象.属性
<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>