广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >Mybatis的where标签使用总结梳理
  • 900
分享到

Mybatis的where标签使用总结梳理

2024-04-02 19:04:59 900人浏览 薄情痞子
摘要

目录背景原始的手动拼接mybatis where标签的使用进阶:自定义trim标签where语句的坑小结背景 在上篇文章,我们系统地学习了where 1

背景

在上篇文章,我们系统地学习where 1=1 相关的知识点,大家可以回看《Mysql中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}
      </if>
    </where>
  </select>

上述SQL语句中添加了 的注释,生成的SQL语句为:

select * from t_user WHERE username = ?  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标签的替代作用,同时,也提到了在使用时可能会出现的坑。内容虽然简单,但如果能够很好地实践、避免踩坑也是能力的体现。

到此这篇关于Mybatis的where标签使用总结梳理的文章就介绍到这了,更多相关Mybatis的where标签 内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Mybatis的where标签使用总结梳理

本文链接: https://www.lsjlt.com/news/149677.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Mybatis的where标签使用总结梳理
    目录背景原始的手动拼接Mybatis where标签的使用进阶:自定义trim标签where语句的坑小结背景 在上篇文章,我们系统地学习了where 1...
    99+
    2022-11-13
  • Mybatis中where标签与if标签怎么结合使用
    这篇文章主要介绍“Mybatis中where标签与if标签怎么结合使用”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Mybatis中where标签与if标签怎么结合使用”文章能帮...
    99+
    2023-07-05
  • mybatis的where标签怎么使用
    本篇内容主要讲解“mybatis的where标签怎么使用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“mybatis的where标签怎么使用”吧!我们经常在动态构造sql时,...
    99+
    2023-06-29
  • Mybatis的where标签如何使用
    这篇文章主要讲解了“Mybatis的where标签如何使用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Mybatis的where标签如何使用”吧!原始的手动拼接在不使用...
    99+
    2023-06-30
  • Mybatis中where标签与if标签结合使用详细说明
    目录前言使用<where>标签总结不使用<where>标签总结前言 由于不小心将and或者or写在了语句后面,导致mybatis无法自主判...
    99+
    2023-03-03
    mybatis where标签 mybatis where mybatis if标签
  • MyBatis常用标签以及使用技巧总结
    前言 MyBatis常用标签及标签使用技巧 MyBatis的常用标签有很多,比如 <sql id="">:预定义可以复用的sql语句 <include refid=...
    99+
    2022-11-12
  • 正则表达式在js前端的15个使用场景梳理总结
    目录引言千分位格式化解析链接参数驼峰字符串小写转大写实现 trim()HTML 转义HTML 反转义校验 24 小时制校验日期格式匹配颜色值判断 HTTPS/HTTP校验版...
    99+
    2022-11-13
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作