@Param注解的用法解析

解析1

实例一 @Param注解单一属性

dao层示例

Public User selectUser(@param(“userName”) String name,@param(“userpassword”) String password);

xml映射对应示例

  1. <select id=" selectUser" resultMap="BaseResultMap">  
  2.    select  *  from user_user_t   where user_name = #{userNamejdbcType=VARCHAR} and user_password=#{userPassword,jdbcType=VARCHAR}  
  3. </select>


 

注意:采用#{}的方式把@Param注解括号内的参数进行引用(括号内参数对应的是形参如 userName对应的是name);

实例二 @Param注解JavaBean对象

dao层示例

public List<user> getUserInformation(@Param("user") User user);
 

xml映射对应示例

  1. <select id="getUserInformation" parameterType="com.github.demo.vo.User" resultMap="userMapper">  
  2.         select   
  3.         <include refid="User_Base_Column_List" />  
  4.         from mo_user t where 1=1  
  5.                       <!-- 因为传进来的是对象所以这样写是取不到值得 -->  
  6.             <if test="user.userName!=null  and user.userName!=''">   and   t.user_name = #{user.userName}  </if>  
  7.             <if test="user.userAge!=null  and user.userAge!=''">   and   t.user_age = #{user.userAge}  </if>  
  8.     </select>  


 

以下内容为摘录内容:源于 冲吧,不要停! 如有侵权,请通知作者,及时删除~~~

1,使用@Param注解

当以下面的方式进行写SQL语句时:

    @Select("select column from table where userid = #{userid} ")
    public int selectColumn(int userid);

当你使用了使用@Param注解来声明参数时,如果使用 #{} ${} 的方式都可以。

    @Select("select column from table where userid = ${userid} ")
    public int selectColumn(@Param("userid") int userid);

当你不使用@Param注解来声明参数时,必须使用使用 #{}方式。如果使用 ${} 的方式,会报错。

    @Select("select column from table where userid = ${userid} ")
    public int selectColumn(@Param("userid") int userid);

 

2,不使用@Param注解

不使用@Param注解时,参数只能有一个,并且是JavabeanSQL语句里可以引用JavaBean的属性,而且只能引用JavaBean的属性。

    // 这里iduser的属性

@Select("SELECT * from Table where id = ${id}")
    Enchashment selectUserById(User user);

解析2

https://www.cnblogs.com/thomas12112406/p/6217211.html

用注解来简化xml配置的时候,@Param注解的作用是给参数命名,参数命名后就能根据名字得到参数值,正确的将参数传入sql语句中 

 

我们先来看Mapper接口中的@Select方法

1

2

3

4

5

6

7

8

9

10

11

12

13

package Mapper; 

   

   

public interface Mapper { 

   

@Select("select s_id id,s_name name,class_id classid from student where  s_name= #{aaaa} and class_id = #{bbbb}") 

    public Student select(@Param("aaaa") String name,@Param("bbbb")int class_id);  

   

@Delete...... 

       

@Insert...... 

    

  

这里解释一下

1.@Select(....)注解的作用就是告诉mybatis框架,执行括号内的sql语句

2.s_id id,s_name name,class_id classid  格式是 字段名+属性名,例如s_id是数据库中的字段名,id是类中的属性名

    这段代码的作用就是实现数据库字段名和实体类属性的一一映射,不然数据库不知道如何匹配

 

3.where  s_name= #{aaaa} and class_id = #{bbbb} 表示sql语句要接受2个参数,一个参数名是aaaa,一个参数名是bbbb,如果要正确的传入参数,那么就要给参数命名,因为不用xml配置文件,那么我们就要用别的方式来给参数命名,这个方式就是@Param注解

 

4.在方法参数的前面写上@Param("参数名"),表示给参数命名,名称就是括号中的内容

public Student select(@Param("aaaa") String name,@Param("bbbb")int class_id); 
给入参 String name 命名为aaaa,然后sql语句....where  s_name= #{aaaa} 中就可以根据aaaa得到参数值了

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值