MyBatis方法传入参数的获取的方式

MyBatis方法传入参数的获取

1.通过索引获取

1.#{arg0},#{arg1}的形式,方法参数的传入存放在一个数组中
2.#{param1},#{param2}.....依次获得方法中的参数

//通过#{arg0}的形式或的方法传入参数的第一个参数
    public User login(String name,String password);
    //通过#{param1}的形式或的方法传入参数的第一个参数
    public User login1(String name,String password);

<select id="login" resultType="user">
        select * from t_user where name =#{arg0} and password =#{arg1};
    </select>
    <select id="login1" resultType="user">
     select * from t_user where name =#{param1} and password =#{param2};
    </select>

2.通过注解给方法参数起别名

@Param(参数别名)

//通过注解为方法的参数起别名用于获取
    public User login2(@Param("name") String name,@Param("password") String password);

 <select id="login2" resultType="user">
     select * from t_user where name =#{name} and password =#{password};
    </select>

3.通过实例化对象获得方法参数

方法的参数类型是一个实例化对象,通过#{属性名}获得对应的属性值

 <select id="login3" resultType="user">
     select * from t_user where name =#{name} and password =#{password};
    </select>

//传入一个实例化对象通过#{属性名}的形式获得对应的值
    public User login3(User user);

4.通过传入一个map对象获得方法参数

方法的参数数据类型是Map<String,Object>通过map的key值获得对应的value值

//传入一个map对象,通过#{key值}获得对应的value值
    public User login4(Map<String,Object> map);

  <select id="login4" resultType="user">
     select * from t_user where name =#{n} and password =#{p};
    </select>

总结:

方法参数少,可以用注解的方法,如果参数多余5个就要想办法将其封装成一个对象,或者封装成一个map进行数据的传递
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值