设为首页收藏本站
网站公告 | 这是第一条公告
     

 找回密码
 立即注册
缓存时间06 现在时间06 缓存数据 时间和经历会练就一个优秀的你。新的一天,加油,给每一个人!

时间和经历会练就一个优秀的你。新的一天,加油,给每一个人!

查看: 855|回复: 2

Mybatis的where标签使用总结梳理

[复制链接]

  离线 

TA的专栏

  • 打卡等级:即来则安
  • 打卡总天数:20
  • 打卡月天数:0
  • 打卡总奖励:220
  • 最近打卡:2025-04-02 04:22:26
等级头衔

等級:晓枫资讯-上等兵

在线时间
0 小时

积分成就
威望
0
贡献
341
主题
289
精华
0
金钱
1175
积分
670
注册时间
2023-2-11
最后登录
2025-5-31

发表于 2023-2-12 00:50:21 | 显示全部楼层 |阅读模式
背景

在上篇文章,我们系统地学习了
  1. where 1=1
复制代码
相关的知识点,大家可以回看《MySQL中where 1=1方法的使用及改进》这篇文章。文章中涉及到了Mybatis的替代方案,有好学的朋友在评论区有朋友问了基于Mybatis写法的问题。
本篇文章会将Mybatis中where标签的基本使用形式、小技巧以及容易踩到的坑进行总结梳理,方便大家更好地实践运用d

原始的手动拼接

在不使用Mybatis的where标签时,我们通常是根据查询条件进行手动拼接,也就是用到了上面提到的
  1. where 1=1
复制代码
的方式,示例如下:
  1.   <select id="selectSelective" resultType="com.secbro.entity.User">
  2.    select * from t_user
  3.    where 1=1
  4.    <if test="username != null and username != ''">
  5.     and username = #{username}
  6.    </if>
  7.    <if test="idNo != null and idNo != ''">
  8.      and id_no = #{idNo}
  9.    </if>
  10.   </select>
复制代码
这种方式主要就是为了避免语句拼接错误,出现类似如下的错误SQL:
  1. select * from t_user where and username = 'Tom' and id = '1001';
  2. select * from t_user where and id = '1001';
复制代码
当添加上1=1时,SQL语句便是正确的了:
  1. select * from t_user where 1=1 and username = 'Tom' and id = '1001';
  2. select * from t_user where 1=1 and id = '1001';
复制代码
这个我们之前已经提到过,多少对MySQL数据库的有一定的压力。因为1=1条件的优化过滤是需要MySQL做的。如果能够将这部分放到应用程序来做,就减少了MySQL的压力。毕竟,应用程序是可以轻易地横向扩展的。

Mybatis where标签的使用

为了能达到MySQL性能的调优,我们可以基于Mybatis的
  1. where
复制代码
标签来进行实现。
  1. where
复制代码
标签是顶层的遍历标签,需要配合
  1. if
复制代码
标签使用,单独使用无意义。通常有下面两种实现形式。
方式一:
  1.   <select id="selectSelective" resultType="com.secbro.entity.User">
  2.    select * from t_user
  3.    <where>
  4.      <if test="username != null and username != ''">
  5.        username = #{username}
  6.      </if>
  7.      <if test="idNo != null and idNo != ''">
  8.        and id_no = #{idNo}
  9.      </if>
  10.    </where>
  11.   </select>
复制代码
方式二:
  1.   <select id="selectSelective" resultType="com.secbro.entity.User">
  2.    select * from t_user
  3.    <where>
  4.      <if test="username != null and username != ''">
  5.        and username = #{username}
  6.      </if>
  7.      <if test="idNo != null and idNo != ''">
  8.        and id_no = #{idNo}
  9.      </if>
  10.    </where>
  11.   </select>
复制代码
仔细观察会发现,这两种方式的区别在于第一if条件中的SQL语句是否有
  1. and
复制代码

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

  • 第一,只有if标签有内容的情况下才会插入
    1. where
    复制代码
    子句;
  • 第二,若子句的开通为 “
    1. AND
    复制代码
    ” 或 “
    1. OR
    复制代码
    ”,
    1. where
    复制代码
    标签会将它替换去除;
所以说,上面的两种写法都是可以了,Mybatis的
  1. where
复制代码
标签会替我们做一些事情。
但需要注意的是:
  1. where
复制代码
标签只会 智能的去除(忽略)首个满足条件语句的前缀。所以建议在使用
  1. where
复制代码
标签时,每个语句都最好写上 and 前缀或者 or 前缀,否则像以下写法就会出现问题:
  1.   <select id="selectSelective" resultType="com.secbro.entity.User">
  2.    select * from t_user
  3.    <where>
  4.      <if test="username != null and username != ''">
  5.        username = #{username}
  6.      </if>
  7.      <if test="idNo != null and idNo != ''">
  8.        id_no = #{idNo}
  9.      </if>
  10.    </where>
  11.   </select>
复制代码
生成的SQL语句如下:
  1. select * from t_user      WHERE username = ?  id_no = ?
复制代码
很显然,语法是错误的。
因此,在使用
  1. where
复制代码
标签时,建议将所有条件都添加上and或or

进阶:自定义trim标签

上面使用
  1. where
复制代码
标签可以达到拼接条件语句时,自动去掉首个条件的and或or,那么如果是其他自定义的关键字是否也能去掉呢?
此时,
  1. where
复制代码
标签就无能为力了,该
  1. trim
复制代码
标签上场了,它也可以实现
  1. where
复制代码
标签的功能。
  1.   <select id="selectSelective" resultType="com.secbro.entity.User">
  2.    select * from t_user
  3.    <trim prefix="where" prefixOverrides="and | or ">
  4.      <if test="username != null and username != ''">
  5.        and username = #{username}
  6.      </if>
  7.      <if test="idNo != null and idNo != ''">
  8.        and id_no = #{idNo}
  9.      </if>
  10.    </trim>
  11.   </select>
复制代码
将上面基于
  1. where
复制代码
标签的写改写为
  1. trim
复制代码
标签,发现执行效果完全一样。而且
  1. trim
复制代码
标签具有了更加灵活的自定义性。

where语句的坑

另外,在使用where语句或其他语句时一定要注意一个地方,那就是:注释的使用。
先来看例子:
  1.   <select id="selectSelective" resultType="com.secbro.entity.User">
  2.    select * from t_user
  3.    <where>
  4.      <if test="username != null and username != ''">
  5.        and username = #{username}
  6.      </if>
  7.      <if test="idNo != null and idNo != ''">
  8.        /* and id_no = #{idNo}*/
  9.        and id_no = #{idNo}
  10.      </if>
  11.    </where>
  12.   </select>
复制代码
上述SQL语句中添加了
  1. /**/
复制代码
的注释,生成的SQL语句为:
  1. select * from t_user WHERE username = ? /* and id_no = ?*/ and id_no = ?
复制代码
执行时,直接报错。
还有一个示例:
  1.   <select id="selectSelective" resultType="com.secbro.entity.User">
  2.    select * from t_user
  3.    <where>
  4.      <if test="username != null and username != ''">
  5.        -- and username = #{username}
  6.        and username = #{username}
  7.      </if>
  8.      <if test="idNo != null and idNo != ''">
  9.        and id_no = #{idNo}
  10.      </if>
  11.    </where>
  12.   </select>
复制代码
生成的SQL语句为:
  1. 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标签 内容请搜索晓枫资讯以前的文章或继续浏览下面的相关文章希望大家以后多多支持晓枫资讯!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
晓枫资讯-科技资讯社区-免责声明
免责声明:以上内容为本网站转自其它媒体,相关信息仅为传递更多信息之目的,不代表本网观点,亦不代表本网站赞同其观点或证实其内容的真实性。
      1、注册用户在本社区发表、转载的任何作品仅代表其个人观点,不代表本社区认同其观点。
      2、管理员及版主有权在不事先通知或不经作者准许的情况下删除其在本社区所发表的文章。
      3、本社区的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,举报反馈:点击这里给我发消息进行删除处理。
      4、本社区一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
      5、以上声明内容的最终解释权归《晓枫资讯-科技资讯社区》所有。
http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~

  离线 

TA的专栏

等级头衔

等級:晓枫资讯-列兵

在线时间
0 小时

积分成就
威望
0
贡献
0
主题
0
精华
0
金钱
16
积分
12
注册时间
2022-12-25
最后登录
2022-12-25

发表于 2024-10-29 22:07:43 | 显示全部楼层
顶顶更健康!!!
http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~

  离线 

TA的专栏

等级头衔

等級:晓枫资讯-列兵

在线时间
0 小时

积分成就
威望
0
贡献
0
主题
0
精华
0
金钱
18
积分
16
注册时间
2022-12-24
最后登录
2022-12-24

发表于 前天 07:06 | 显示全部楼层
感谢楼主分享。
http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~
严禁发布广告,淫秽、色情、赌博、暴力、凶杀、恐怖、间谍及其他违反国家法律法规的内容。!晓枫资讯-社区
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1楼
2楼
3楼

手机版|晓枫资讯--科技资讯社区 本站已运行

CopyRight © 2022-2025 晓枫资讯--科技资讯社区 ( BBS.yzwlo.com ) . All Rights Reserved .

晓枫资讯--科技资讯社区

本站内容由用户自主分享和转载自互联网,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。

如有侵权、违反国家法律政策行为,请联系我们,我们会第一时间及时清除和处理! 举报反馈邮箱:点击这里给我发消息

Powered by Discuz! X3.5

快速回复 返回顶部 返回列表