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

与世无争的帅哥 提交于 2020-01-17 09:47:07

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

  • 顺序传参法。不推荐使用,参数顺序易出错
  • 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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!