MyBatis 动态SQL标签

1. if标签

  A. 标签属性:test —— 表示条件,条件成立就把元素体中的字符串拼接到sql语句中,否则不拼接;

  B. 应用场景:通常用于WHERE语句、UPDATE语句、INSERT语句中,通过判断参数值来决定是否使用某个查询条件、判断是否更新某一个字段、判断是否插入某个字段的值;

  C. 示例

2. choose/when/otherwise标签

  A. when标签中test属性与if中test类似,按顺序判断when中的条件是否成立,如果有一个成立,则choose结束,当choose中所有when的条件都不满足时,则执行otherwise中的sql,这个类似于Java的switch语句;

  B. 应用场景:当我们并不想应用所有条件,只想从多个选项中选择一个;

  C. 示例

3. trim标签

  A. 应用场景:trim标签一般用于去除sql语句中多余的and关键字、,逗号或者给sql语句前拼接 where、set以及values(正括号等前缀,或者添加)反括号等后缀,可用于选择性插入、更新、删除或者条件查询等操作;

  B. 标签属性

属性 描述
prefix
给sql语句拼接的前缀
prefixOverrides

去除sql语句前面的关键字或者字符,该关键字或者字符由prefixesToOverride属性指定,假设该属性指定为AND,当sql语句的开头为AND,trim标签将会去除该AND
suffix
给sql语句拼接的后缀
suffixOverrides
去除sql语句后面的关键字或者字符,该关键字或者字符由suffixesToOverride属性指定

  C. 示例 

<insert id="insertSelective" parameterType="com.ruhuanxingyun.Manufacture" >
    insert into manufacture
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="productType != null" >
        product_type,
      </if>
      <if test="sdkNum != null" >
        sdk_num,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=BIGINT},
      </if>
      <if test="productType != null" >
        #{productType,jdbcType=VARCHAR},
      </if>
      <if test="sdkNum != null" >
        #{sdkNum,jdbcType=CHAR},
      </if>
      <if test="deviceType != null" >
        #{deviceType,jdbcType=BIT},
      </if>
    </trim>
  </insert>

4. where标签

  A. where标签只会在至少有一个子元素的条件返回SQL子句的情况下才去插入WHERE子句,若语句的开头为AND或OR,where元素会将它们自动去除;

  B. 可以用trim标签来处理

<trim prefix="WHERE" prefixOverrides="AND | OR">
  ... 
</trim>

  C. 示例

<select id="findAreaInfoListByCode" parameterType="java.lang.String" resultType="com.model.map.AreaInfoTree">
    select area_name as title, concat (area_code, '_', ifnull (xpoint, ''), '_', ifnull (ypoint, '')) as `key`, if (xpoint || ypoint, 0, 1) as disabled from area_info
        <where>
            p_area_code = #{areaCode}
            <if test="areaCode == 0">
                and area_code != '660000'
            </if>
        </where>
</select>

5. set标签

  A. set标签会动态前置SET关键字,同时也会删掉无关的逗号;

  B. 可以用trim标签替代

<trim prefix="SET" suffixOverrides=",">
  ...
</trim>

  C. 示例

<update id="updateFactoryInfo" parameterType="com.model.dockingmanager.FactoryInfo">
    update factory_info
    <set>
        <if test="factoryName!=null and factoryName!='' ">
            factory_name = #{factoryName},
        </if>
        <if test="wEquipmentFactory!=null and wEquipmentFactory!=''">
            w_equipment_factory = #{wEquipmentFactory,jdbcType=INTEGER},
        </if>
        update_time = now()
    </set>
    WHERE id = #{id}
</update>

6. foreach标签

  A. 应用场景:对集合数据进行遍历,比如在构建IN条件语句时;

  B. 标签属性

属性 描述
collection
表示传递进来的参数名称
item

表示循环中当前的元素或键值
index
表示当前元素在集合的位置下标(List/Set/Array)或键key(Map)
open/close
表示以什么符号将这些集合元素包装起来
separator
表示各个元素的间隔符

  C. 示例

 

可参考:MyBatis 动态SQL

    MyBatis详解 — 动态SQL使用及原理

 

posted @ 2019-05-29 10:34  如幻行云  阅读(3476)  评论(0编辑  收藏  举报