mybatis中@Param的使用

时间:2023-03-09 05:39:40
mybatis中@Param的使用

@Param:当映射器方法需要多个参数时,这个注解可以被用于:给映射器方法中的每个参数来取一个名字。否则,多参数将会以它们的顺序位置和SQL语句中的表达式进行映射,这是默认的。
       语法要求:若使用@Param("id"),则SQL中参数应该被命名为:#{id}。

用代码说明:

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select; import com.game.domain.User; /**
* UserMapper接口
*/
public interface UserMapper { //根据用户的用户名、密码判断登录
@Select("select * from user where userName = #{UserName} and userPwd = #{UserPwd}")
User findWithLoginnameAndPassword(@Param("UserName")String Name,
@Param("UserPwd") String Pwd); //根据id删除用户
@Delete(" delete from user where userID = #{id}")
void deleteById(@Param("id") Integer ID);

这里:@Param("UserName")注解表示给该注解后面的参数(String Name)取一个参数名称(命名为UserName),对应@Select注解中的#{UserName}。
   如果没有使用@Param注解,则参数将会以它们的顺序位置来和SQL语句中的表达式进行映射。
   然后sql语句:select * from user where userName = #{UserName} and userPwd = #{UserPwd}中,就可以根据"UserName"和"UserPwd"得到参数值了。