0
点赞
收藏
分享

微信扫一扫

JAVA_EE_MyBatis之动态SQL

飞鸟不急 2021-09-28 阅读 46
  • 根据不同的条件需要执行不同的 SQL 命令.称为动态 SQL
  • MyBatis 中动态 SQL 在 mapper.xml 中添加逻辑判断等

一、if使用

<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>
</

二、where

  • 当编写 where 标签时,如果内容中第一个是 and 去掉第一个and
  • 如果<where>中有内容会生成 where 关键字,如果没有内容不生成 where 关键字
<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> <otherwise>

  • 只有有一个成立,其他都不执行.
  • 如果 accin 和 accout 都不是 null 或不是””生成的 sql 中只有 where accin=?
<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>用在修改 SQL 中 set 从句

  • 作用:去掉最后一个逗号
  • 作用:如果<set>里面有内容生成 set 关键字,没有就不生成
  • 下面代码的前面id=#{id} 目的防止<set>中没有内容,mybatis 不生成 set 关键字,如果修改中没有 set 从句 SQL 语法错误.
<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

  • prefix 在前面添加内容
  • prefixOverrides 去掉前面内容
  • suffix 在后面添加内容
  • suffixOverrieds 去掉后面内容
  • 执行顺序是:去掉内容后添加内容
<update id="upd" parameterType="log">
      update log
    <trim prefix="set" suffixOverrides=",">
        a=a,
    </trim>
    where id=100
</update>

六、<bind>

  • 作用:给参数重新赋值
  • 场景:模糊查询、在原内容前或后添加内容
<select id="selByLog" parameterType="log" resultType="log">
    <bind name="accin" value="'%'+accin+'%'"/>
        #{money}
</select>

七、<foreach>标签

  • 循环参数内容,还具备在内容的前后添加内容,还具备添加分隔符功能.
  • 适用场景:in 查询中.批量新增中(mybatis 中 foreach 效率比较低)
  • 如果希望批量新增,SQL 命令如下
insert into log VALUES (default,1,2,3),(default,2,3,4),(default,3,4,5)
  • 使用openSession()必须指定如下代码,其原理是底层 JDBC 的PreparedStatement.addBatch();
factory.openSession(ExecutorType.BATCH);
  • collectino=”” 要遍历的集合
  • item 迭代变量, #{迭代变量名}获取内容9.3.3 open 循环后左侧添加的内容
  • close 循环后右侧添加的内容
  • 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>定义这个片段
<sql id="mysql">
    id,accin,accout,money
</sql>
  • 在<select>或<delete>或<update>或<insert>中使用<include>引用
<select id="">
        select <include refid="mysql"></include>
        from log
</select>
举报

相关推荐

0 条评论