判断 num 是否为 null 或者等于null 的sql mybatis 条件表达式
时间: 2024-02-16 10:03:06 浏览: 30
在 MyBatis 中,可以使用以下方式判断一个属性是否为 null 或者等于 null:
```
<if test="num == null or num.equals(null)">
<!-- 如果 num 为 null 或者等于 null,则执行这里的代码 -->
</if>
```
在这个例子中,判断 num 是否为 null 或者等于 null 的表达式是 `num == null or num.equals(null)`,如果为 true,就会执行 if 标签内的代码。
需要注意的是,如果使用 `num == null` 来判断一个属性是否等于 null,那么当 num 为 null 时会抛出空指针异常,因为 null 不是一个对象,不能调用它的方法。因此,如果要判断一个属性是否等于 null,最好使用 `num.equals(null)` 这种方式。
相关问题
mybatis Sql if标签 判断等于null或者空字符串
在 MyBatis 中,可以使用 `<if>` 标签来判断一个参数是否为 `null` 或者空字符串。示例如下:
```xml
<select id="selectUser" resultType="User">
SELECT * FROM user
<where>
<if test="username != null and username != ''">
AND username = #{username}
</if>
</where>
</select>
```
在上面的例子中,我们使用了 `<if>` 标签来判断 `username` 参数是否为 `null` 或者空字符串,如果是,则不加入 SQL 语句中。
注意,这里的判断条件是 `username != null and username != ''`,也就是说,既要判断是否为 `null`,又要判断是否为空字符串。如果只想判断是否为 `null`,可以简化成 `username != null`。如果只想判断是否为空字符串,可以使用 MyBatis 提供的 `StringUtils` 工具类,如下所示:
```xml
<select id="selectUser" resultType="User">
SELECT * FROM user
<where>
<if test="@org.apache.commons.lang3.StringUtils@isNotBlank(username)">
AND username = #{username}
</if>
</where>
</select>
```
在上面的例子中,我们使用了 `StringUtils.isNotBlank()` 方法来判断 `username` 参数是否为空字符串。需要注意的是,这里使用了 `@` 符号来引用 `StringUtils` 类,因为它不是 MyBatis 内置的类。如果你不想使用 `@` 符号,也可以在 MyBatis 配置文件中配置一个别名来简化使用。
mybatis里面判断值等于null或者“”
在 MyBatis 中,可以使用 `<if>` 标签来判断值是否为 `null` 或空字符串 `""`。具体用法如下:
```xml
<select id="selectUserByName" parameterType="java.lang.String" resultType="User">
SELECT * FROM users
<where>
<if test="name != null and name != ''">
AND name = #{name}
</if>
</where>
</select>
```
在上面的例子中,使用了 `<if>` 标签来判断 `name` 是否为 `null` 或空字符串 `""`。如果不是,则会在 SQL 语句中添加一个 `AND` 条件,查询名字为 `name` 的用户信息。注意,`test` 属性中的表达式需要使用 OGNL 表达式语言来编写。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)