MyBatis if标签的用法

    <!--
        4.1.1 在WHERE条件中使用if

            需求:
                实现一个用户管理高级查询功能,根据输入的条件去检索用户信息。这个功能
                还需要支持以下三种情况:当只有输入用户名时,需要根据用户名进行模糊查
                询;当只有输入邮箱时,根据邮箱进行完全匹配;当同时输入用户名与邮箱时
                用这两个条件去查询匹配的用户。

            <if>便签有一个必填的属性test,test的属性值是一个符合OGNL要求的判断表达式,
            表达式的结果可以是true或者false,初次之外所有的的非0值都为true,只有0为false。
            且有如下规则:
                1.判断条件property!=null或者property==null:适用于任何类型的字段,用于判断属性值是否为空
                2.判断条件property!=''或者property=='':仅适用于String类型的字段,用于判断是否为空字符串
                3.and和or:当有多个判断条件时,适用and或or进行连接,嵌套的判断可以适用小括号分组。
    -->

    <!--不能满足需求的代码,标记下模糊匹配的写法-->
    <select id="selectByUser" resultType="tk.mybatis.simple.model.SysUser">
      select
        id,
        use_name userName,
        user_password userPassword,
        user_email userEmail,
        user_info userInfo,
        head_img headImg,
        create_time createTime
      from sys_user
      where
        user_name like concat('%',#{userName},'%') and
        uer_email=#{userEmail}
    </select>

    <!--改进后的代码-->
    <select id="selectByUser" resultType="tk.mybatis.simple.model.SysUser">
      select
        id,
        use_name userName,
        user_password userPassword,
        user_email userEmail,
        user_info userInfo,
        head_img headImg,
        create_time createTime
      from sys_user
      where
        1=1
        <if test="userName!=null and userName!=''">
                and user_name like concat('%',#{userName},'%')
        </if>
        <if test="userEmail!=null and userEmail!=''">
                and user_email = #{userEmail}
        </if>
    </select>

    <!--
        4.1.3 在UPDATE更新列中使用if

            需求:
                只更新有变化的字段,需要注意,更新的时候不能将原来的值
                但没有发生变化的字段更新为空或null。
    -->

    <!--需求实现的代码-->
    <update id="updateByIdSelective">
        update sys_user
        set
          <if test="userName!=null and userName!=''">
                  user_name=#{userName},
          </if>
          <if test="userEmail!=null and userEmail!=''">
                  user_email=#{userEmail},
          </if>
          <if test="userInfo!=null and userInfo!=''">
                  user_info=#{userInfo},
          </if>
          <if test="headImg!=null">
                  head_img=#{headImg},
          </if>
          <if test="createTime!=null">
                  create_time=#{createTime},
          </if>
          id=#{id}
        where id=#{id}
    </update>

    <!--
        4.1.3 在INSERT动态插入列中使用if

            需求:
                在数据库中插入数据的时候,如果某一列的参数值不为空,就使用传入的值,如果传入
                的参数为空,就使用数据库中的默认值(通常是空),而不使用传入的空值。
    -->

    <insert id="insert2" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO sys_user
          (id,user_name,user_password,
          <if test="userEmail!=null and uerEmail!=''">
              user_email,
          </if>
          user_info,head_img,create_time)
        VALUES
          (#{id},#{userName},#{userPassword},
          <if test="userEmail!=null and uerEmail!=''">
              #{userEmail},
          </if>
          #{userInfo},#{headImg,jdbcType=BLOB},#{createTime,jdbcType=TIMESTAMP})
    </insert>

 

 

From《MyBatis从入门到精通》

转载于:https://www.cnblogs.com/junjie2019/p/10570215.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值