关于Mybatis @Param 注解,官方文档: http://www.mybatis.org/mybatis-3/zh/java-api.html
其中关于 @param部分的说明是:
@Param Parameter N/A 如果你的映射器的方法需要多个参数, 这个注解可以被应用于映射器的方法 参数来给每个参数一个名字。否则,多 参数将会以它们的顺序位置来被命名 (不包括任何 RowBounds 参数) 比如。 #{param1} , #{param2} 等 , 这 是 默 认 的 。 使 用 @Param(“person”),参数应该被命名为 #{person}。
最重要的一点,就是在写对于dao接口的时候,对于方法中有多个参数的情况,一定要使用@Param这个注解,正规一些,避免错误!!!
当你使用了使用@Param注解来声明参数时,如果使用 #{} 或 ${} 的方式都可以。
当你不使用@Param注解来声明参数时,必须使用使用 #{}方式。如果使用 ${} 的方式,会报错。
上代码看下:
第一种:
Dao层的方法
Public User selectUser(String name,String password);
对应的Mapper.xml
<select id="selectUser" resultMap="BaseResultMap">
select * from user_user_t where user_name = #{0} and user_password=#{1}
</select>
第二种:
该方法采用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_password=#{userPassword,jdbcType=VARCHAR}
</select>
第三种:
Dao层的方法
Public User selectUser(@param(“userName”)Stringname,@param(“userpassword”)String password);
对应的Mapper.xml
<select id=" selectUser" resultMap="BaseResultMap">
select * from user_user_t where user_name = #{userName,jdbcType=VARCHAR} and user_password=#{userPassword,jdbcType=VARCHAR}
</select>
第四种:
mapper.java:
public List<User> selectUser(User use);
对应Mapper.xml:
<select id=" selectUser" resultMap="BaseResultMap">
select * from user_user_t where user_name = #{userName,jdbcType=VARCHAR} and user_password=#{userPassword,jdbcType=VARCHAR}
</select>