<!--
        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从入门到精通》

MyBatis if标签的用法的更多相关文章

  1. MyBatis bind标签的用法

    From<MyBatis从入门到精通> <!-- 4.5 bind用法 bind标签可以使用OGNL表达式创建一个变量并将其绑定到上下文中. 需求: concat函数连接字符串,在M ...

  2. MyBatis foreach标签的用法

    From<MyBatis从入门到精通> 一.foreach实现in集合 1.映射文件中添加的代码: <!-- 4.4 foreach用法 SQL语句有时会使用IN关键字,例如id i ...

  3. MyBatis select标签的用法

    From<MyBatis从入门到精通> 第一步,在接口中添加方法: public interface UserMapper { SysUser selectById(Long id); } ...

  4. mybatis001-动态标签Trim用法

    Mybatis动态标签Trim用法 一.<trim></trim>标签用法 示例一: select * from user <trim prefix="WHER ...

  5. 9.mybatis动态SQL标签的用法

    mybatis动态SQL标签的用法   动态 SQL MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句有多么 ...

  6. SpringMVC +mybatis+spring 结合easyui用法及常见问题总结

    SpringMVC +mybatis+spring 结合easyui用法及常见问题总结 1.FormatString的用法. 2.用postAjaxFillGrid实现dataGrid 把form表单 ...

  7. Mybatis foreach标签含义

    背景 考虑以下场景: InfoTable(信息表): Name Gender Age Score 张三 男 21 90 李四 女 20 87 王五 男 22 92 赵六 女 19 94 孙七 女 23 ...

  8. Java-MyBatis-杂项: MyBatis 中 in 的用法2

    ylbtech-Java-MyBatis-杂项: MyBatis 中 in 的用法2 1.返回顶部 1. 一.简介 在SQL语法中如果我们想使用in的话直接可以像如下一样使用: select * fr ...

  9. Java-MyBatis:MyBatis 中 in 的用法

    ylbtech-Java-MyBatis-杂项:MyBatis  中  in 的用法 1.返回顶部 1. foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合.foreach元 ...

随机推荐

  1. murmurhash2算法 和 DJB Hash算法是目前最流行的hash算法

    murmurhash2算法 和 DJB Hash算法是目前最流行的hash算法 1.DJB HASH算法 1 2 3 4 5 6 7 8 9 10 11 /* the famous DJB Hash ...

  2. 微信小程序把玩(五)页面生命周期

    原文:微信小程序把玩(五)页面生命周期 这里只要熟悉页面的基本生命周期即可,业务在指定生命周期函数内书写. 以下是官网给出的生命周期函数方法和状态图 上面的生周期函数图对于做Android 或者IOS ...

  3. java模拟post请求发送json数据

    import com.alibaba.fastjson.JSONObject; import org.apache.http.client.methods.CloseableHttpResponse; ...

  4. Vm安装

      说明:都是默认安装,并不需要繁琐设置,所以没有文字说明

  5. mysql 的用户权限

    查看MySQL的用户权限 show grants for "username"@'host'; 添加新用户 允许本地IP访问localhost:127.0.0.1 use mysq ...

  6. UWP中String 转为Path Data

    定义方法: Geometry PathMarkupToGeometry(string pathMarkup) { string xaml = "<Path " + " ...

  7. 【操作系统】关于Linux桌面操作系统

    以前是Win+Ubuntu+黑苹果,周末想体验一下deepin,于是简单安装了一下,安装过程很简单,这里不再描述.安装之后,第一次打开系统,确实很惊艳,赏心悦目的操作系统. 之前用Ubuntu时候,C ...

  8. 【Linux】Linux相关资料

    linux相关技术资料: linux技术资料大全: http://t.cn/zYNBwFs

  9. 学习Java,值得你留意的问题(1)更名为《学习Java,容易被你忽略的小细节(1)》

    记得大二快要结束的时候,有个女孩子突然问我“你会Java吗,帮我做大作业好吗?” 实话说,那个女孩真的很漂亮,我当时也非常想帮她.但是我从来没有接触过Java,让我在短短的几天内完成Java程序设计课 ...

  10. Spring单例模式多线程安全问题-有状态的Bean

    Spring单例与线程安全小结 一.Spring单例模式与线程安全 Spring框架里的bean,或者说组件,获取实例的时候都是默认的单例模式,这是在多线程开发的时候要尤其注意的地方. 单例模式的意思 ...