1、 @Param的作用就是给参数命名,一般使用在dao(mapper)层,当添加注解后(@Param("userId") int id),也就是说外部想要取出传入的id值,只需要取它的参数名userId就可以了。将参数值传如SQL语句中,通过#{userId}进行取值给SQL的参数赋值。
例:void select(@Param("userName") string name,@Param("password") string pwd)
<select id="selectUser" resultMap="User">
select * from user where user_name = #{userName} and user_password=#{password}
</select>
#{userName} #{password}中的userName和password都是从注解@Param()里面取出来的,取出来的值就是方法中形式参数 String name 和 String pwd的值
注:
@Param注解来声明参数的时候,SQL语句取值使用#{},${}取值都可以。
不使用@Param注解声明参数的时候,必须使用的是#{}来取参数。使用${}方式取值会报错。
不使用@Param注解时,参数只能有一个,并且是Javabean。在SQL语句里可以引用JavaBean的属性,而且只能引用JavaBean的属性。