mybatis的几种传参方式

本文介绍了MyBatis中四种不同的参数传递方法:通过序号传参、通过Map传参、使用@Param注解和传递对象类型参数。每种方法都详细展示了Dao层和对应的Mapper.xml文件中的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.通过序号传参

Dao层代码:

Public User selectUser(Map paramMap);

对应的Mapper.xml的代码:

<select id="selectUser" resultMap="BaseResultMap">

    select  *  from user  where user_name = #{0} and user_area=#{1}

</select>

 

2.通过map传参

Dao层代码:

Public User selectUser(Map paramMap);

对应的Mapper.xml的代码:

<select id=" selectUser" resultMap="BaseResultMap">

   select  *  from user  where user_name = #{userNamejdbcType=VARCHAR} and user_area=#{userArea,jdbcType=VARCHAR}

</select>

 

3.@Param通过单个参数名传递参数

Dao层代码:

public boolean modifyEnd(@Param("end")String end,@Param("id")Integer id);

对应的Mapper.xml的代码:

<update id="modifyEnd">

update calendar set end = #{end} where id =  #{id}

</update>

 

4.传递对象类型的参数

Dao层的代码:

public boolean modify(Calendar calendar);

对应的Mapper.xml的代码:

<update id="modify" parameterType="Calendar">

update calendar set title = #{title}, start = #{start}, end = #{end}, allDay = #{allDay}, color = #{color} where id = #{id}

</update>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值