Mybitis中参数的几种传递方式

1.单独一个参数
(1)直接进行传递

SysUserInfo getSysUserByUsername(String username);
<select id="getSysUserByUsername" resultType="com.fulan.entity.SysUserInfo">
        select * from sys_user_info where username=#{username}
 </select>

(2)用注解@param

SysUserInfo getSysUserByUsername(@Param("username")String username);
<select id="getSysUserByUsername" resultType="com.fulan.entity.SysUserInfo">
        select * from sys_user_info where username=#{username}
 </select>

但是不建议这样搞
2.多个参数
(1)用对象封装后传入

public int addResourceInfo(ResourceInfo resourceInfo);
<insert id="addResourceInfo" parameterType="com.fulan.entity.ResourceInfo">
        INSERT INTO resource_info (resource_name, creator_id, created_time, last_modifier_id, last_modified_time)
        VALUES (#{resourceName}, #{creatorId}, now(), NULL, NULL)
 </insert>

(2)用@param注解

public int addResourceInfo(@Param("resourceName")String resourceName,@Param("creatorId")int creatorId);
<insert id="addResourceInfo">
        INSERT INTO resource_info (resource_name, creator_id, created_time, last_modifier_id, last_modified_time)
        VALUES (#{resourceName}, #{creatorId}, now(), NULL, NULL)
 </insert>

(3)用map进行封装

public int addResourceInfo(Hashmap map);
<insert id="addResourceInfo" parameterType="com.fulan.entity.ResourceInfo">
        INSERT INTO resource_info (resource_name, creator_id, created_time, last_modifier_id, last_modified_time)
        VALUES (#{resourceName}, #{creatorId}, now(), NULL, NULL)
 </insert>

如果map中存放的是封装的resource对象

<insert id="addResourceInfo" parameterType="com.fulan.entity.ResourceInfo">
        INSERT INTO resource_info (resource_name, creator_id, created_time, last_modifier_id, last_modified_time)
        VALUES (#{resource.resourceName}, #{resource.creatorId}, now(), NULL, NULL)
 </insert>

上文中如果对象中封装对象也需要点出属性,@param传的是对象的话同。
个人认为@param是将参数装在一个map里,望各位大佬指教

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值