@Param注解在mybatis中的使用以及传入参数的几种方式

关于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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值