myBatis xml if、where、if-else?、foreach 心得

文章转载自:https://blog.csdn.net/qq_34886018/article/details/86478403

       MyBatis 的强大特性之一便是它的动态 SQL。如果你有使用 JDBC 或其它类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句的痛苦。例如拼接时要确保不能忘记添加必要的空格,还要注意去掉列表最后一个列名的逗号。利用动态 SQL 这一特性可以彻底摆脱这种痛苦。
       虽然在以前使用动态 SQL 并非一件易事,但正是 MyBatis 提供了可以被用在任意 SQL 映射语句中的强大的动态 SQL 语言得以改进这种情形。
       动态 SQL 元素和 JSTL 或基于类似 XML 的文本处理器相似。在 MyBatis 之前的版本中,有很多元素需要花时间了解。MyBatis 3 大大精简了元素种类,现在只需学习原来一半的元素便可。MyBatis 采用功能强大的基于 OGNL 的表达式来淘汰其它大部分元素。

if

       mapper中编写sql,使用<if test = ' '> </if>,可以使你的接口很便捷

举个栗子:

select * from student
<if test = " id != null ">
     where student.id =#{id}
</if>

       一个<if>标签还是不够用的,你单独使用<if>的时候肯定还会遇到这样的问题

select * from student
where
<if test = " id != null ">
student.id = #{id}
</if>
<if test = " name != null and name != '' ">
and student.name = #{name}
</if>

       如果当你的id为空时,name前面的and是没有必要的,运行会抛异常
或者当这两个<if>都为空时,只剩一个空的where,还是会报错

where

select * from student
<where>
<if test = " id != null ">
    and student.id = #{id}
</if>
<if test = " name != null and name != '' ">
    and student.name = #{name}
</if>
</where>

       where 元素只会在至少有一个子元素的条件返回 SQL 子句的情况下才去插入WHERE子句。而且,若语句的开头为AND或OR,where 元素也会将它们去除。

if-else =>> choose, when, otherwise

       首先,在myBatis中是不支持if-else的,想要是用if-else的话,可以使用choose代替。
choose,when,otherwise有点像Java中的switch

<select id="findActiveBlogLike"
     resultType="Blog">
  SELECT * FROM BLOG WHERE state = ‘ACTIVE’
  <choose>
    <when test="title != null">
      AND title like #{title}
    </when>
    <when test="author != null and author.name != null">
      AND author_name like #{author.name}
    </when>
    <otherwise>
      AND featured = 1
    </otherwise>
  </choose>
</select>

关于mybatis的动态sql,建议查看,中文哦官方文档

  • 8
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatisXML文件中,可以使用if标签来编写条件判断语句。if标签可以用于动态生成SQL语句的一部分,根据条件的不同来决定是否包含该部分。\[1\] 如果你想要使用if-else的逻辑,可以使用choose、when和otherwise标签来代替。choose标签类似于Java中的switch语句,可以根据条件选择执行不同的分支。when标签用于定义条件,otherwise标签则相当于else语句,用于定义默认的分支。\[2\] 在使用if标签时,需要注意一些常见的错误。例如,如果你在比较字符串时没有使用单引号,可能会导致类型转换错误。另外,如果你在比较数字时使用了非数字的字符串,也会导致类型转换错误。确保在比较字符串时使用单引号,并且在比较数字时使用正确的数据类型。\[3\] 希望这些信息对你有帮助!如果还有其他问题,请随时提问。 #### 引用[.reference_title] - *1* *2* [myBatis xml文件SQL编写( if、where、if-else、set 、foreach )](https://blog.csdn.net/baidu_41847368/article/details/114593640)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [mybatis xml中<if test 判断字符串的总结](https://blog.csdn.net/steamPlayGame/article/details/128645195)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值