MyBatis中动态SQL判断等值的方式

一般情况下在使用mybatis的动态SQL时,常用的是用来判空,如下:

<if test="userType != null and userType != ''">  
    <![CDATA[ and user_type = #{userType} ]]>  
</if>  

有时会遇到判断条件是某一个值的时候执行特殊的sql条件或语句,如下:

1.数值型

示例如下:

<if test="userType != null and userType == 2">  
    <![CDATA[ and xxxx > 1  ]]>  
</if>

2.字符型

示例如下:

<if test="userType != null and userType == '2'.toString()">  
    <![CDATA[ and xxxx > 1  ]]>  
</if>

注意: 当条件判断值是字符,即字符串的时候,推荐使用'abc'.toString()方式, 或者使用 userType eq 2 的形式也可

 

3.拓展
大于小于的写法

原含义 代替写法 英文全称 用法
< &lt; less than a.score &lt; '60'
> &gt; greater than a.score &gt; '0'
= eq equal type eq 1
& &amp;    
&apos;    
" &quot;    

第二种写法:

<if test="type eq 1 ">
	(  ssc.status= '2' OR  ssc.exam_source = '4'  )
</if>

 

posted @ 2023-08-20 16:08  酸菜鱼没有鱼  阅读(723)  评论(0编辑  收藏  举报