Mybatis传递参数

方法一:使用map接口传递参数
map是一个键值对应的集合,要通过知道它的键,才能明了其作用,使用map不能限定其传递的数据类型。
publicList findRolesByMap(Map<String, Object> parameterMap);

<select id="findRolesByMap" parameterType="map" resultType="role">
    select id, role_name as roleName, note from t_role where role_name like concat('%', #{roleName}, '%') and note like concat('%', #{note}, '%')
</select>

方法二:使用注解传递多个参数
注解@Param(org.apache.ibatis.annotations.Param),可以通过它去定义映射器的参数名称,使用它可以得到更好的可读性。
public List findRolesByAnnotation(@Param(“roleName”) String rolename, @Param(“note”) String note);

<select id="findRolesByAnnotation" resultType="role">
    select id, role_name as roleName, note from t_role where role_name like concat('%', #{roleName}, '%') and note like concat('%', #{note}, '%')
</select>

方法三:通过Java Bean传递多个参数
public List findRolesByBean(RoleParams roleParam);

select id, role_name as roleName, note from t_role where role_name like concat(’%’, #{roleName}, ‘%’) and note like concat(’%’, #{note}, ‘%’)

方法四:混合使用
在一些情况下需要混合使用几种方法来传递参数。
public List findByMix(@Param(“params”) RoleParams roleParams, @Param(“page”) PageParam PageParam);

select id, role_name as roleName, note from t_role
where role_name like concat(’%’, #{params.roleName}, ‘%’) and note like concat(’%’, #{params.note}, ‘%’) limit #{page.start}, #{page.limit}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值