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

6次阅读

共计 3543 个字符,预计需要花费 9 分钟才能阅读完成。

背景

在上篇文章,咱们系统地学习了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

正文完
 0