【Mybatis Plus基础使用】Mapper.java传递多个参数

根据实际情况总结了以下几种多参数传递的方法:

  • 顺序传参法。不推荐使用,参数顺序易出错
  • Java Bean传参
  • Map传参
  • @Param 注解传参

顺序传参法

根据Mapper.java中参数的顺序进行相应的调用

Mapper.java

List<PlatformUser> selectUserPage(String creator, String userId);

Mapper.xml

<select id="selectUserPage" resultType="com.act.platform.entity.PlatformUser">
         select * from platform_user a where a.creator = #{0} and a.user_id = #{1}
</select>

Java Bean传参

可在Mapper.xml中通过#{}直接调用Java Bean中属性进行参数传递
Mapper.java

List<PlatformUser> selectUserPage(PlatformUser user);

Mapper.xml

<select id="selectUserPage" resultType="com.act.platform.entity.PlatformUser" parameterType="com.act.platform.entity.PlatformUser" >
         select * from platform_user a 
        <where>
            <if test = " creator != null and creator != '' ">
                and a.creator = #{creator}
            </if>
            <if test = " userId != null and userId != '' ">
                and a.user_id like "%"#{userId}"%"
            </if>
            <if test = " userName != null and userName != '' ">
                and a.user_name like "%"#{userName}"%"
            </if>
            <if test = " ip != null and ip != '' ">
                and a.ip like #{ip}
            </if>
        </where>
          GROUP BY a.user_id ORDER BY a.create_date
    </select>

Map传参

可在Mapper.xml中通过#{}直接调用Map中key 值进行参数传递
Mapper.java

List<PlatformUser> selectUserPage(Map<String,Object> params);

Mapper.xml

<select id="selectUserPage" resultType="com.act.platform.entity.PlatformUser" parameterType="java.util.Map" >
         select * from platform_user a 
        <where>
            <if test = " creator != null and creator != '' ">
                and a.creator = #{creator}
            </if>
            <if test = " userId != null and userId != '' ">
                and a.user_id like "%"#{userId}"%"
            </if>
            <if test = " userName != null and userName != '' ">
                and a.user_name like "%"#{userName}"%"
            </if>
            <if test = " ip != null and ip != '' ">
                and a.ip like #{ip}
            </if>
        </where>
          GROUP BY a.user_id ORDER BY a.create_date
    </select>

@Param 注解传参

可在Mapper.xml中通过@Param注解值进行参数调用,如果有参数为Java Bean,则可以通过
@Param注解值调用Java Bean的属性进行参数传递

Mapper.java

List<PlatformUser> selectUserPage(@Param("systemId") Integer systemId, @Param("platformUser")PlatformUser platformUser);

Mapper.xml

<select id="selectUserPage" resultType="com.act.platform.entity.PlatformUser"  >
         select * from platform_user a 
         where a.systemId = #{systemId}
        <if test = " platformUser.creator != null and platformUser.creator != '' ">
            and a.creator = #{platformUser.creator}
        </if>
        <if test = " platformUser.userId != null and platformUser.userId != '' ">
            and a.user_id like "%"#{platformUser.userId}"%"
        </if>
        <if test = " platformUser.userName != null and platformUser.userName != '' ">
            and a.user_name like "%"#{platformUser.userName}"%"
        </if>
        <if test = " platformUser.ip != null and platformUser.ip != '' ">
            and a.ip like #{platformUser.ip}
        </if>
          GROUP BY a.user_id ORDER BY a.create_date
    </select>
  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值