Cause:org.apache.ibatis.binding.BindingException: Parameter ‘xxx’ not found. Available parameters are [arg1, arg0, param1, param2]解决方法
**Mapper.xml
文件相关片段
<select id="selectUserByParams" resultType="com.hnu.entity.User">
SELECT * FROM USER WHERE name = #{name} and phone = #{phone}
</select>
控制台输出的错误信息:
org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: org.apache.ibatis.binding.BindingException: Parameter 'name' not found. Available parameters are [arg1, arg0, param1, param2]
### Cause: org.apache.ibatis.binding.BindingException: Parameter 'name' not found. Available parameters are [arg1, arg0, param1, param2]
方法1.在***Mapper.java的接口文件中
将:
User selectUserByParams(String name,String phone);
修改为:
User selectUserByParams(@Param("name")String name, @Param("phone")String phone);
其中@Param(“xxx”)中的xxx与 ***Mapper.xml
中#{xxx}中的xxx保持一致,为参数注解。
(@Param是MyBatis所提供的(org.apache.ibatis.annotations.Param),作为Dao层的注解,作用是用于传递参数,从而可以与SQL中的的字段名相对应,一般在2=<参数数<=5时使用最佳。)
控制台成功输出:
User{id=3, name='王五', phone='1536537158'}
Process finished with exit code 0
方法2.修改 **Mapper.xml
文件相关片段为:
<select id="selectUserByParams" resultType="com.hnu.entity.User">
SELECT * FROM USER WHERE name = #{arg0} and phone = #{arg1}
</select>
控制台成功输出:
User{id=3, name='王五', phone='1536537158'}
Process finished with exit code 0