Parameter 'xxx' not found. Available parameters are [arg1, arg0, param1, param2]


问题:

org.mybatis.spring.MyBatisSystemException:nested exception is org.apache.ibatis.binding.BindingException: Parameter 'xxx' not found. Available parameters are [arg1, arg0, param1, param2]

 

原因:

这个问题是因为我们在Mapper接口中声明方法的时候有多个参数,例如:

Mapper接口中方法:

int selectUser(String pid,String pname);

 

然后在mapper.xml配置如下:

<select id="selectUser" resultType="xxx.xxx.xxx.model.User">

select *

from user

where id = #{pid} and name = #{pname}

</select>

 

这样直接用pid,pname就会报错的。

 

解决方法:

这里使用@Param注解,这种方式Mybatis就会自动将参数封装成Map类型,@Param注解的值会作为Map中的key,例如:

Mapper接口中:

int selectUser(@Param("pid")String pid,@Param("pn")String pname);

 

mapper.xml中:

<select id="selectUser" resultType="xxx.xxx.xxx.model.User">

select *

from user

where id = #{pid} and name = #{pn}

</select>

 

这里注意一下:

@Param(“pn”) String pname,@Param里面的定义的名称(即pn)可以和参数名(即pname)不一致。在xml中的SQL 语句是以“pn”作为key的。也就是说在使用时是 name = #{pn},而不是 name = #{pname}了。

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM