MyBatis:简述MyBatis映射文件中的参数传递

MyBatis:简述MyBatis映射文件中的参数传递

1.方法中传递单个参数

public List<XXBean> getXXBeanList(String id);  

<select id="getXXXBeanList" parameterType="java.lang.String" resultType="XXBean">
  select * from tableName  where id= #{id}  
</select>  

其中方法名和select标签的id一致,#{}中的参数名与方法中的参数名一致。select 后的字段列表要和bean中的属性名一致, 如果不一致的可以用 as来补充,或者使用ResultMap自己配置。

2.方法中直接传递多个参数

public List<XXXBean> getXXXBeanList(String id, String name);  

<select id="getXXXBeanList" resultType="XXBean">
  select * from tableName where id = #{0} and name = #{1}  
</select>  

由于是多参数,所以就不能使用parameterType, 改用#{index}。是第几个就用第几个的索引,索引从0开始。

3.方法中通过Map封装传递多个参数

public List<XXXBean> getXXXBeanList(HashMap map);  

<select id="getXXXBeanList" parameterType="java.util.HashMap" resultType="XXBean">
  select * from tableName where id=#{id} and name= #{name}  
</select>  

#{}中的参数名称为map中key的值。

4.方法中通过List封装传递多个参数

public List<XXXBean> getXXXBeanList(List<String> list);  

<select id="getXXXBeanList" parameterType="java.util.List" resultType="XXBean">
  select * from tableName where id in
  <foreach item="item" index="index" collection="list" open="(" separator="," close=")">  
    #{item}  
  </foreach>  
</select>  

foreach 最后的效果是select * from tableName where id in (‘1’,‘2’,‘3’,‘4’)。

5.方法中通过@Param注解传递多个参数

public List<XXXBean> getXXXBeanList(@Param("id")String id, @Param("name")String name);  

<select id="getXXXBeanList"  resultType="XXBean">
  select * from tableName where id=#{id} and name= #{name} 
</select>  

#{}中的参数名称为@Param注解中的值。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王小二(海阔天空)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值