1. 程式人生 > >二、MyBatis教程之三—多參數的獲取方式

二、MyBatis教程之三—多參數的獲取方式

傳遞多參數 update1 https str detail net set pda param

如果接口中的方法擁有多個參數,那麽在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

二、MyBatis教程之三—多參數的獲取方式