Mybatis参数传递

1、顺序传参法

顺序传参方法是根据参数的顺序来赋值,这种方式不太容易观察,且顺序容易搞乱,不推荐使用。示例如下所示:

public User selectEmp(String id, String user_name);

<select id="selectEmp" resultMap="cool.ale.pojo.Emp">
    select * from emp
    	where id = #{0} and user_name = #{1}
</select>

2、@Param注解传参法

这里可以用 @Param 注解来指定一个名称,参数在不多的情况下还是比较直观的。示例如下所示:

public User selectEmp(@Param("id") String id, String @Param("user_name") user_name);

<select id="selectEmp" resultMap="cool.ale.pojo.Emp">
    select * from emp
    	where id = #{id} and user_name = #{user_name}
</select>

3、Map传参法

这种方式适合传递多个参数,且比较灵活。

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

<select id="selectEmp" parameterType="java.util.Map" resultMap="cool.ale.pojo.Emp">
    select * from emp
    	where id = #{id} and user_name = #{user_name}
</select>

4、Java Bean传递参数方法

这里直接用属性名取值即可

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

<select id="selectEmp" parameterType="java.util.Map" resultMap="cool.ale.pojo.Emp">
    select * from emp
    	where id = #{id} and user_name = #{user_name}
</select>

5、传入一个数组

示例代码如下:

List<Emp> selectEmp(List<Integer> list);

<select id="selectEmp" resultType="cool.ale.pojo.Emp">
   select id,name from emp where id = #{list[0]} or id = #{list[1]}
</select>

6、多个无规则参数传递

多个无规则的参数传递,mybatis会自己封装成下面的样子

map1

{
    "arg0": "value1",
    "arg1": "value2"
}

map2

{
    "param1": "value1",
    "param2": "value2"
}

所以我们取值的话可以用arg0、arg1或者param1、param2参数名称取值

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值