一、区别
- isNotNull:表示参数不为 NULL,返回 TRUE 反之则为 FALSE
- isNull:表示参数为 NULL,返回 TRUE 反之则为 FALSE
- isNotEmpty:表示参数不为 NULL 也不为空,返回 TRUE 反之则返回 FALSE
- isEmpty:表示参数为 NULL 或着为空,返回 TRUE 反之则为 FALSE
二、对比
当 xml 中用 isNotEmpty 配置时
<select id="queryTable" resultClass="int" parameterClass="QryCondition">
select count(id) from table
<dynamic prepend="WHERE">
<isNotEmpty prepend="AND" property="abc">
( table.filed1 LIKE '%$abc$%')
</isNotEmpty>
<isNotEmpty prepend="AND" property="def">
( table.filed2 LIKE '%$def$%')
</isNotEmpty>
</dynamic>
</select>
当之传入参数 abc 而不传入 def 时,产生的 sql 语句为:
select count(id) from table where(table.filed1 LIKE '%abc%');
当 xml 中用 isNotNull 配置时(注意它们的区别):
<select id="queryTable" resultClass="int" parameterClass="QryCondition">
select count(id) from table
<dynamic prepend="WHERE">
<isNotNull prepend="AND" property="abc">
(table.field1 LIKE '%$abc$%')
</isNotNull>
<isNotNull prepend="AND" property="def">
(table.field2 LIKE '%$def$%')
</isNotNull>
</dynamic>
</select>
同样,当传入参数 abc 而不传入 def 时,产生的 sql 语句为:
select count(id) from table
where (table.field1 LIKE '%abc%') AND (table.field2 LIKE '%%')
三、注意
必须使用 ISNULL() 来判断是否为 NULL 值。
说明:NULL 与任何值的直接比较都为 NULL。
1) NULL<>NULL 的返回结果是 NULL,而不是 false。
2) NULL=NULL 的返回结果是 NULL,而不是 true。
3) NULL<>1 的返回结果是 NULL,而不是 true。