MyBatis - 传递参数 7 种方式


方法0:单参数传递

public User selectUser(int deptId);

<select id="selectUser" resultMap="UserResultMap">
    select * from user where dept_id = #{param}
</select>

注:单参数传递名字可以和形参不一样。

方法1:顺序传参法(Default)

public User selectUser(String name, int deptId);

<select id="selectUser" resultMap="UserResultMap">
    select * from user
    where user_name = #{0} and dept_id = #{1}
</select>

#{}里面的数字代表你传入参数的顺序。

多参数传递时,Mybatis默认将传过来的参数用【arg0】【arg1】【……】或者【param1】【param2】【……】来替换。

Ps:在MyBatis3.4.4版不能直接使用#{0}要使用 #{arg0}。这种方法不建议使用,sql层表达不直观,且一旦顺序调整容易出错。

方法2:@Param注解传参法

public User selectUser(@Param("userName") String name, int @Param("deptId") deptId);

<select id="selectUser" resultMap="UserResultMap">
    select * from user
    where user_name = #{userName} and dept_id = #{deptId}
</select>

#{}里面的名称对应的是注解@Param括号里面修饰的名称。

这种方法在参数不多的情况还是比较直观的,推荐使用。

方法3:Map传参法

public User selectUser(Map<String, Object> params);

<select id="selectUser" parameterType="java.util.Map" resultMap="UserResultMap">
    select * from user
    where user_name = #{userName} and dept_id = #{deptId}
</select>

#{}里面的名称对应的是Map里面的key名称。

这种方法适合传递多个参数,且参数易变能灵活传递的情况。

方法4:Java Bean 传参法

public User selectUser(User user);

<select id="selectUser" parameterType="com.test.User" resultMap="UserResultMap">
    select * from user
    where user_name = #{userName} and dept_id = #{deptId}
</select>

#{}里面的名称对应的是User类里面的成员属性。

这种方法很直观,但需要建一个实体类,扩展不容易,需要加属性,看情况使用。

方法5:@Param + 默认传参

public User selectUser(@Param("userName") String name, int deptId);

<select id="selectUser" resultMap="UserResultMap">
    select * from user
    where user_name = #{userName} and dept_id = #{param2}
</select>

@Param + 默认传参混合传参时,没有被标记@Param的形参,一一对应序号【argN(从0开始)】或【paramN(从1开始)】。

Ps:在MyBatis3.4.4版不能直接使用#{0}要使用 #{arg0}。

方法6:集合类型传参

  1. Collection
    public User selectUser(Collection listId);
    
    <select id="selectUser" resultMap="UserResultMap">
        select * from user where dept_id = #{collection[0]}
    </select>
  2. List
    public User selectUser(List listId);
    
    <select id="selectUser" resultMap="UserResultMap">
        select * from user where dept_id = #{collection[0]}
    </select>
    public User selectUser(List listId);
    
    <select id="selectUser" resultMap="UserResultMap">
        select * from user where dept_id = #{list[0]}
    </select>
  3. Array
    public User selectUser(int[] ids);
    
    <select id="selectUser" resultMap="UserResultMap">
        select * from user where dept_id = #{array[0]}
    </select>

    Ps:Collection 在版本3.3以及之后,List 在版本3.2以及之后。

方法7:@Param + 数组

public User selectUser(@Param("param") int[] ids);

<select id="selectUser" resultMap="UserResultMap">
    select * from user where dept_id = #{test[0]}
</select>

  1. parameterType 写不写?
    答:parameterType写的话,则限制了输入的参数类型。当然也可以不用写,因为mybatis能自动识别,但返回值类型不能不写,因为mybatis需要将获得结果封装到相应的类中,查询的字段与类的属性需要一致(不一致的需要显示的配置)。 
  2. 待更新...
  • 6
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

放羊的牧码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值