广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Mybatis的xml中使用if/else标签的具体使用
  • 501
分享到

Mybatis的xml中使用if/else标签的具体使用

2024-04-02 19:04:59 501人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

目录使用if标签进行查询where标签出场if/else 使用 choose,when,otherwise 代替使用if标签进行查询 SELECT ord

使用if标签进行查询

		SELECT
			orderNo,
			adname,
			orderstatus
		FROM
			order_A
		where
			<if test="order!=null">
				order=#{order}
			</if>
			<if test="title!=null">
				and title=#{title}
			</if>

需要注意的是:如果第一个if的order为null的话 第二值title也为null的话运行会报错,就算第一个if等于null 那么查询语句变成 where and title='哈哈哈' 这样运行的话也会出现错误。

where标签出场

		SELECT
			orderNo,
			adname,
			orderstatus
		FROM
			order_A
		<where>
			<if test="order!=null">
				order=#{order}
			</if>
			<if test="order!=null">
				and title=#{title}
			</if>
		</where>

where 元素只会在至少有一个子元素的条件返回 sql 子句的情况下才去插入WHERE子句。而且,若语句的开头为AND或OR,where 元素也会将它们去除。这个只能解决2个值都为空。
不能解决order值为空但是title值为空时还是会出现语句错误的情况,这个时候我们可以在and 前面用1=1或者true来解决

如:

或这样

if/else 使用 choose,when,otherwise 代替

由于mybatis中没有else标签但是可以通过choose,when,otherwise来使用

SELECT
			orderNo,
			adname,
			orderstatus
FROM
	<choose>
		<when test=" platfORMtype != null and platformtype.trim() != '' and platformtype == 1">
			 `orders_A` as orderTable
		</when>
		<when test=" platformtype != null and platformtype.trim() != '' and platformtype == 2">
			 `orders_B` as orderTable
		</when>
		<when test="  platformtype != null and platformtype.trim() != '' and platformtype == 3">
			 `orders_C` as orderTable
		</when>
		<otherwise>
			 `orders_A` as orderTable
		</otherwise>
	</choose>

翻译一下上面的语句:

当platformtype 值不为空并且把platformtype 值进行去除空字符串,并且值等于1时
就会把表orders_A进行拼接,如果条件都不符合的话就会走otherwise标签默认拼接orders_A表进行查询

choose,when,otherwise标签有点像Java中的switch 当where的test值满足时会拼接里面的表,otherwise表示其他when标签都不满足时执行拼接

到此这篇关于Mybatis的xml中使用if/else标签的具体使用的文章就介绍到这了,更多相关Mybatis xml=使用if/else标签内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Mybatis的xml中使用if/else标签的具体使用

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

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

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

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

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作