您的位置:首页 > 其它

MyBatis教程之三多参数的获取方式

2017-08-08 13:04 190 查看
如果接口中的方法拥有多个参数,那么在mapper文件中该如何获取呢?

有三种方式:

1、就是普通写法,在文件中通过arg或param获取

2、使用Map集合,在文件中使用#{key}获取

3、使用注解@Param,在文件中使用#{名称}

1、arg或param获取

接口对应的方法:

int update1(String xh,int id);


映射文件的获取:

<!--多参之一:接口中直接写,使用arg0或param1获取值  -->
<!-- update tb_car set xh=#{arg0} where id=#{arg1} -->
<update id="update1" >
update tb_car set xh=#{param1} where id=#{param2}
</update>


可以选择使用arg获取也可以使用param获取,但是arg从0开始,而param从1开始。

2、Map集合传递多参数

接口对应的方法:

//第二种:封装成集合
int update3(Map<String,Object> map);


映射文件获取

<!--多参之二:Map集合  -->
<update id="update3" parameterType="map">
update tb_car set color=#{c} where id=#{id}
</update>


3、注解@Param传递多参数

接口对应的方法:

int update5(@Param("xh") String x,@Param("id") int i);


映射文件获取:

<!--多参之三:注解  -->
<update id="update5" >
update tb_car set xh=#{xh} where id=#{id}
</update>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息