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

如果接口中的方法拥有多个参数,那么在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 ta_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>

https://blog.csdn.net/xingfei_work/article/details/76896153

原文地址:https://www.cnblogs.com/arrows/p/10364975.html