动态SQL

作者: Yanl__ | 来源:发表于2019-12-27 08:56 被阅读0次

动态SQL: Mybatis中可以在写mapper.xml时添加逻辑判断
常用关键字:if、where、「choose when otherwise」、set、trim、bind、foreach、「sql include」

if

  • 要加if的逻辑判断默认要在select * from log后加where 1=1
  • 条件写在test属性中
<select id="selByAccinAccout" resultType="log">
    select * from log where 1=1
<!-- OGNL 表达式,直接写key 或对象的属性.不需要添加任
何特字符号-->
<if test="accin!=null and accin!=''">
    and accin=#{accin}
</if>
<if test="accout!=null and accout!=''">
    and accout=#{accout}
</if>
</select>

where

  1. 当编写where标签时,如果内容中第一个是and就去掉第一个and
  2. 如果<where>中有内容会生成where关键字,如果没有,则不生成
  3. 比直接使用if关键字少一个where 1=1
<select id="selByAccinAccout" resultType="log">
    select * from log
    <where>
        <if test="accin!=null and accin!=''">
            and accin=#{accin}
        </if>
        <if test="accout!=null and accout!=''">
            and accout=#{accout}
        </if>
    </where>
</select>

choose、when

只有一个条件成立,其他的都不执行

<select id="selByAccinAccout" resultType="log">
    select * from log
    <where>
        <choose>
            <when test="accin!=null and accin!=''">
                and accin=#{accin}
            </when>
            <when test="accout!=null and accout!=''">
                and accout=#{accout}
            </when>
        </choose>
    </where>
</select>

set

  • 如果set中有内容就生成set关键字,如果没有就不生成。由于SQL中修改语句没有set的话,会报语法错误,所以默认在set下添加一个id=#{id}
  • set会去掉最后一个逗号
<update id="upd" parameterType="log" >
    update log
    <set>
        id=#{id},
        <if test="accIn!=null and accIn!=''">
            accin=#{accIn},
        </if>
        <if test="accOut!=null and accOut!=''">
            accout=#{accOut},
        </if>
    </set>
    where id=#{id}
</update>

trim

trim的四个属性

  1. prefix : 在前面添加内容
  2. prefixOverrides :去掉前面内容
  3. suffix : 在后面添加内容
  4. suffixOverrieds : 去掉后面内容

执行顺序:去掉内容后添加内容

<update id="upd" parameterType="log">
    update log
    <trim prefix="set" suffixOverrides=",">
        a=a,
    </trim>
    where id=100
</update>

bind

作用:给参数重新赋值
使用场景:模糊查询、在原内容前或后添加内容

模糊查询:例如用户要查询张姓的人名,输入“张”,我们可以通过bind来替换。“张”-->“张%” or “%张%”

<select id="selByLog" parameterType="log" resultType="log">
    <bind name="accin" value="'%'+accin+'%'"/>
        #{money}
</select>

foreach

作用:循环参数内容,还具备在内容的前后添加内容,还具备添加分隔符功能.
使用场景:in查询中、批量新增

foreach的三个属性:

  1. open:循环后左侧添加的内容
  2. close:循环后右侧添加的内容
  3. separator:每次循环时,元素之间的分隔符
<select id="selIn" parameterType="list"
resultType="log">
    select * from log where id in
    <foreach collection="list" item="abc" open="(" close=")" separator=",">
        #{abc}
    </foreach>
</select>

sql和include

某些sql片段,如果希望复用,可以使用<sql id=“id值”>定义这个片段的内容,使用<include refid="id值">来引用

<sql id="mysql">
    id,accin,accout,money
</sql>

<select id="">
    select <include refid="mysql"></include>
    from log
</select>

相关文章

  • MyBatis学习:动态sql

    1.动态sql 动态sql是mybatis中的一个核心,什么是动态sql?动态sql即对sql语句进行灵活操作,通...

  • 第十三章 使用动态SQL(一)

    第十三章 使用动态SQL(一) 动态SQL简介 动态SQL是指在运行时准备并执行的SQL语句。在动态SQL中,准备...

  • 第八章 动态SQL

    动态SQL中的元素介绍 动态SQL有什么作用 MyBatis提供了对SQL语句动态组装的功能 动态SQL中的元素 ...

  • 关于Mybatis的一些问题讨论

    Mybatis动态sql是做什么的?都有哪些动态sql?简述一下动态sql的执行原理 动态sql的用途 Mybat...

  • MyBatis5-动态 SQL

    动态 SQL 什么是动态 SQL 就是动态的对 SQL 进行组装 拼接. : 可以自动去...

  • geoserver动态颜色参数样式、动态sql配置实现

    geoserver动态颜色参数样式、动态sql配置实现 动态颜色参数样式、动态sql 访问方式: http://l...

  • 强大的动态SQL

    1 动态SQL# 那么,问题来了: 什么是动态SQL? 动态SQL有什么作用? 传统的使用JDBC的方法,相信大家...

  • MyBatis:动态 SQL

    1. 动态 SQL 简而言之,动态 SQL 就是在 Mapper 中使用分支、循环等逻辑。常见的动态 SQL 元素...

  • 无标题文章

    ### 一、简答题 #### 1、Mybatis动态sql是做什么的?都有哪些动态sql?简述一下动态sql的执行...

  • 五,MyBatis动态SQL

    一,含义:动态SQL是指根据参数数据动态组织SQL的技术 二,动态SQL的应用场景:比如淘宝搜索的时候,可以动态的...

网友评论

      本文标题:动态SQL

      本文链接:https://www.haomeiwen.com/subject/joxloctx.html