mybatis 实现 if..else

mybatis中可以通过使用下列标签,配合if标签 实现java 中 if...else...的效果,减少查库的次数

<choose>
<when>
</when>
<otherwise>
</otherwise>
</choose>

下面是实例展示,先判空,之后若不为空,在包含'02'这个属性的情况下,走when标签下的sql
否则,会走otherwise下的sql,达成if...else...的效果

点击查看代码
<if test='typeArr != null and typeArr.length > 0'>
	<choose>
		<when test="type.contains('02')">
			AND (u.type in
			<foreach collection="typeArr" item="type" separator="," close=")" open="(">
				<if test="type != '02'">
					#{type}
					</if>
			</foreach>
			or left(u.resign_date,7) = left(#{date},7))
		</when>
		<otherwise>
			AND u.type in
			<foreach collection="typeArr" item="type" separator="," close=")" open="(">
				#{type}
			</foreach>
		</otherwise>
	</choose>
</if>

实例中 使用了 'test='type.contains('02')' ,test中的表达式可以使用其所属基本类型自带的方法,比如String可以使用contains()方法。

posted @ 2023-03-02 18:33  已至谷底余生只留前进  阅读(743)  评论(0)    收藏  举报