MyBatis如何传入多个参数

本文详细介绍了在MyBatis中处理单个及多个参数的方法,包括直接使用、通过Map封装以及利用@Param注解传递参数到XML映射文件中的技巧。

一、单个参数

mapper

public List<Test> getTestList(String id);

xml

<select id = "getTestList" parameterType = "java.lang.String" resultType = "com.test.Test">
  select t.* from test t where t.id = #{id} 
</select>

二、多个参数

1、使用索引

mapper

public List<Test> getTestList(String id, String name);

xml

<select id = "getTestList" resultType = "com.test.Test">
  select t.* from test t where t.id = #{0} and t.name = #{1}  
</select>

2、使用Map封装多参数

mapper

public List<Test> getTestList(HashMap map);

xml

<select id = "getTestList" parameterType = "hashmap" resultType = "com.test.Test">
  select t.* from test t where t.id = #{id} and t.name= #{name}  
</select>  

#{}中的变量名要和map中的key对应。

3、使用注解

mapper

public List<Test> getTestList(@Param("id")int id, @Param("name")int name);

xml

<select id = "getTestList" resultMap = "com.test.Test">
   select t.* from test t where t.id = #{id} and t.name = #{name}
</select>

 

 

参考-致谢:

1、mingyue1818 ---- MyBatis传入多个参数的问题

 

转载于:https://www.cnblogs.com/soupk/p/9761378.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值