关于@Param
第一种方式:
在Dao层中
Public User selectUser(@param(“userName”) String name, @param(“userpassword”) String password);
在xml映射文件中
<select id=" selectUser" resultMap="BaseResultMap">
select * from user
where user_name = #{userName,jdbcType=VARCHAR} and user_password=#{userPassword,jdbcType=VARCHAR}
</select>
此时 @Param
的作用就相当于把 String name
的值引用到 括号内的参数中 username
,而xml文件同时也通过 #{}
的方式进行注入参数。
这里在说一下 #{}
和 ${}
的区别
#{}
是占位符,让SQL完成预编译处理,防止 SQL 的注入,在预编译的时候#{}
会被解析成?
,并添加上''
。即#{} -> '?'
${}
只是字符串的替换
关于@Pathvariable
这个注解的作用在于 RequestMapping URI 样式映射,通过 @Pathvariable
绑定它传过来的值到方法的参数上。
@Controller
//http://localhost:80808
public class ShowPage{
@RequestMapping("/{page}")
//http://localhost:80808/{page}
public void showPage(@Pathvariable("page") String mypage){
return mypage;
}
}
上面代码中,mypage
的值通过 @Pathvarible
注解绑定到参数 page
。而RequestMapping通过这个 page
参数来读取到 mypage
的值
如果RequestMapping 的参数和方法参数名称不一致,需要通过 @Pathvariable
来指定。如果一致可以省略
@Controller
//http://localhost:80808
public class ShowPage{
@RequestMapping("/{page}")
//http://localhost:80808/{page}
public void showPage(@Pathvariable String page){
return page;
}
}