错误信息:
nested exception is org.apache.ibatis.binding.BindingException: Parameter ‘loginName’ not found. Available parameters are [1, 0, param1, param2]
错误出处:
/**根据用户角色和账号查找用户
*@author SMF
*@date 2018/03/1
*@Description 根据用户的角色id和用户的账号名查找特定的用户*/
User getUserByRoleAndLoginName(@Param("loginName")String loginName,@Param("roleId")Integer roleId);
我使用mybatis的时候,dao中有一个方法进行数据库查询的时候是需要两个参数的,一般情况下,都是一个参数,但是当需要两个参数是就必须要用“@Param(“args”)” 进行注释。这里我已经加上了注解,没有注解就会报错。关于@Param在mybatis中的使用可以查看官方文档:
http://www.mybatis.org/mybatis-3/zh/java-api.html
@Param Parameter N/A 如果你的映射器的方法需要多个参数, 这个注解可以被应用于映射器的方法 参数来给每个参数一个名字。否则,多 参数将会以它们的顺序位置来被命名 (不包括任何 RowBounds 参数) 比如。 #{param1} , #{param2} 等 , 这 是 默 认 的 。 使 用 @Param(“person”),参数应该被命名为 #{person}。
也可以查看这位的: http://blog.youkuaiyun.com/hotdust/article/details/51568289
另外关于Mybatis中的传递多个参数传递可以参考一下:
传多个参数的方案有三种。
第一种方案
DAO层的函数方法*
Public User selectUser(String name,String area);
对应的Mapper.xml
<select id="selectUser" resultMap="BaseResultMap">
select * from user where name = #{0} and user_area=#{1}
</select>
其中,#{0}代表接收的是dao层中的第一个参数,#{1}代表dao层中第二参数,更多参数一致往后加即可。
第二种方案
此方法采用Map传多参数.
Dao层的函数方法:
Public User selectUser(Map paramMap);
对应的Mapper.xml:
<select id=" selectUser" resultMap="BaseResultMap">
select * from user_user_t where user_name = #{userName,jdbcType=VARCHAR} and user_area=#{userArea,jdbcType=VARCHAR}
</select>
Service层调用:
Private User xxxSelectUser(){
Map paramMap=new hashMap();
paramMap.put(“userName”,”对应具体的参数值”);
paramMap.put(“userArea”,”对应具体的参数值”);
User user=xxx. selectUser(paramMap);}
个人认为此方法不够直观,见到接口方法不能直接的知道要传的参数是什么。
第三种方案
Dao层的函数方法“”
Public User selectUser(@param(“userName”)Stringname,@param(“userArea”)String area);
对应的Mapper.xml:
<select id=" selectUser" resultMap="BaseResultMap">
select * from user_user_t where user_name = #{userName,jdbcType=VARCHAR} and user_area=#{userArea,jdbcType=VARCHAR}
</select>
个人觉得这种方法比较好,能让开发者看到dao层方法就知道该传什么样的参数,比较直观,个人推荐用此种方案。