<!--
        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. adb help

    Android Debug Bridge version 1.0.32Revision cc6ae925e394-android -a - directs adb to listen on all i ...

  2. Interactive Data Display如何显示菜单?

    貌似直接跟其它控件一样添加菜单就行了. 不过这个版本已经取消了默认菜单,所有的菜单功能都得自己去实现.

  3. 如何替换Windows的Shell(即explorer.exe)

    原文:如何替换Windows的Shell(即explorer.exe) 下载一个可以查看用户的SID的软件,如SysInternals套装中的PsGetsid.exe(地址:http://www.it ...

  4. QT字符编码转换,可用于中文内码传输

    串口.TCP.UDP传输中文字符时,先将字符串转内码.客户端接收到数据后,将内码转为字符串就OK了 QByteArray CommonFunction::strToInterCode(constQSt ...

  5. C++的类型转换:static_cast、dynamic_cast、reinterpret_cast和const_cast(dynamic_cast还支持交叉转换,const_cast将一个类的const、volatile以及__unaligned属性去掉)

    在C++中,存在类型转换,通常意味着存在缺陷(并非绝对).所以,对于类型转换,有如下几个原则:(1)尽量避免类型转换,包括隐式的类型转换(2)如果需要类型转换,尽量使用显式的类型转换,在编译期间转换( ...

  6. SYN6101型 RS485子钟

    SYN6101型  RS485子钟 产品概述 SYN6101型RS485子钟是由西安同步电子科技有限公司精心设计.自行研发生产的一套以通过RS485总线复接或串行与母钟连接的子钟,接收母钟发送来的时间 ...

  7. Logback详细整理,基于springboot的日志配置

    Logback的配置介绍: 1.Logger.appender及layout Logger作为日志的记录器,把它关联到应用的对应的context上后,主要用于存放日志对象,也可以定义日志类型.级别. ...

  8. Java面试常问问题及答案(非常详细)

    一:java基础1.简述string对象,StringBuffer.StringBuilder区分string是final的,内部用一个final类型的char数组存储数据,它的拼接效率比较低,实际上 ...

  9. (1)Linux文件系统的目录组成

    记忆秘诀:BBDEH OPRLM TLSUV 宝贝的恩惠 欧派入联盟 偷了suv,19   目录 英文释义 简写 详解 1 /   根目录 整个文件系统的唯一根目录 2 /bin Binary 普通命 ...

  10. 找不到 main 方法

    前几天打开了好久不用的eclipse,发现报了个奇怪的错误 ***中找不到 main 方法, 请将 main 方法定义为: public static void main(String[] args) ...