关于mybatis:Mybatis的where标签竟然还有这么多不知道的

背景

在上篇文章,咱们系统地学习了where 1=1 相干的知识点,大家能够回看《不要再用where 1=1了!有更好的写法!》这篇文章。文章中波及到了Mybatis的代替计划,有好学的敌人在评论区有敌人问了基于Mybatis写法的问题。

于是,就有了这篇文章。本篇文章会将Mybatis中where标签的根本应用模式、小技巧以及容易踩到的坑进行总结梳理,不便大家更好地实际使用d

原始的手动拼接

在不应用Mybatis的where标签时,咱们通常是依据查问条件进行手动拼接,也就是用到了下面提到的where 1=1的形式,示例如下:

  <select id="selectSelective" resultType="com.secbro.entity.User">
    select * from t_user
    where 1=1
    <if test="username != null and username != ''">
     and username = #{username}
    </if>
    <if test="idNo != null and idNo != ''">
      and id_no = #{idNo}
    </if>
  </select>

这种形式次要就是为了防止语句拼接谬误,呈现相似如下的谬误SQL:

select * from t_user where and username = 'Tom' and id = '1001';
select * from t_user where and id = '1001';

当增加上1=1时,SQL语句便是正确的了:

select * from t_user where 1=1 and username = 'Tom' and id = '1001';
select * from t_user where 1=1 and id = '1001';

这个咱们之前曾经提到过,多少对MySQL数据库的有肯定的压力。因为1=1条件的优化过滤是须要MySQL做的。如果可能将这部分放到应用程序来做,就缩小了MySQL的压力。毕竟,应用程序是能够轻易地横向扩大的。

Mybatis where标签的应用

为了能达到MySQL性能的调优,咱们能够基于Mybatis的where标签来进行实现。where标签是顶层的遍历标签,须要配合if标签应用,独自应用无意义。通常有上面两种实现模式。

形式一:

  <select id="selectSelective" resultType="com.secbro.entity.User">
    select * from t_user
    <where>
      <if test="username != null and username != ''">
        username = #{username}
      </if>
      <if test="idNo != null and idNo != ''">
        and id_no = #{idNo}
      </if>
    </where>
  </select>

形式二:

  <select id="selectSelective" resultType="com.secbro.entity.User">
    select * from t_user
    <where>
      <if test="username != null and username != ''">
        and username = #{username}
      </if>
      <if test="idNo != null and idNo != ''">
        and id_no = #{idNo}
      </if>
    </where>
  </select>

仔细观察会发现,这两种形式的区别在于第一if条件中的SQL语句是否有and

这里就波及到where标签的两个个性:

  • 第一,只有if标签有内容的状况下才会插入where子句;
  • 第二,若子句的开明为 “AND” 或 “OR”,where标签会将它替换去除;

所以说,下面的两种写法都是能够了,Mybatis的where标签会替咱们做一些事件。

但须要留神的是:where标签只会 智能的去除(疏忽)首个满足条件语句的前缀。所以倡议在应用where标签时,每个语句都最好写上 and 前缀或者 or 前缀,否则像以下写法就会呈现问题:

  <select id="selectSelective" resultType="com.secbro.entity.User">
    select * from t_user
    <where>
      <if test="username != null and username != ''">
        username = #{username}
      </if>
      <if test="idNo != null and idNo != ''">
        id_no = #{idNo}
      </if>
    </where>
  </select>

生成的SQL语句如下:

select * from t_user      WHERE username = ?  id_no = ?

很显然,语法是谬误的。

因而,在应用where标签时,倡议将所有条件都增加上and或or

进阶:自定义trim标签

下面应用where标签能够达到拼接条件语句时,主动去掉首个条件的and或or,那么如果是其余自定义的关键字是否也能去掉呢?

此时,where标签就无能为力了,该trim标签上场了,它也能够实现where标签的性能。

  <select id="selectSelective" resultType="com.secbro.entity.User">
    select * from t_user
    <trim prefix="where" prefixOverrides="and | or ">
      <if test="username != null and username != ''">
        and username = #{username}
      </if>
      <if test="idNo != null and idNo != ''">
        and id_no = #{idNo}
      </if>
    </trim>
  </select>

将下面基于where标签的写改写为trim标签,发现执行成果齐全一样。而且trim标签具备了更加灵便的自定义性。

where语句的坑

另外,在应用where语句或其余语句时肯定要留神一个中央,那就是:正文的应用。

先来看例子:

  <select id="selectSelective" resultType="com.secbro.entity.User">
    select * from t_user
    <where>
      <if test="username != null and username != ''">
        and username = #{username}
      </if>
      <if test="idNo != null and idNo != ''">
        /* and id_no = #{idNo}*/
        and id_no = #{idNo}
      </if>
    </where>
  </select>

上述SQL语句中增加了 /**/的正文,生成的SQL语句为:

select * from t_user WHERE username = ? /* and id_no = ?*/ and id_no = ? 

执行时,间接报错。

还有一个示例:

  <select id="selectSelective" resultType="com.secbro.entity.User">
    select * from t_user
    <where>
      <if test="username != null and username != ''">
        -- and username = #{username}
        and username = #{username}
      </if>
      <if test="idNo != null and idNo != ''">
        and id_no = #{idNo}
      </if>
    </where>
  </select>

生成的SQL语句为:

select * from t_user WHERE -- and username = ? and username = ? and id_no = ? 

同样会导致报错。

这是因为咱们应用 XML 形式配置 SQL 时,如果在 where 标签之后增加了正文,那么当有子元素满足条件时,除了 < !– –> 正文会被 where 疏忽解析以外,其它正文例如 // 或 /**/ 或 — 等都会被 where 当成首个子句元素解决,导致后续真正的首个 AND 子句元素或 OR 子句元素没能被胜利替换掉前缀,从而引起语法错误。

同时,集体在实践中也常常发现因为在XML中应用正文不当导致SQL语法错误或执行出谬误的后果。强烈建议,非必要,不要在XML中正文掉SQL,能够通过版本管理工具来追溯历史记录和批改。

小结

本文基于Mybatis中where标签的应用,开展讲了它的应用形式、个性以及拓展到trim标签的代替作用,同时,也提到了在应用时可能会呈现的坑。内容尽管简略,但如果可能很好地实际、防止踩坑也是能力的体现。

博主简介:《SpringBoot技术底细》技术图书作者,热爱钻研技术,写技术干货文章。

公众号:「程序新视界」,博主的公众号,欢送关注~

技术交换:请分割博主微信号:zhuan2quan

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理