mybatis WhenPresent
时间: 2023-11-12 11:07:49 浏览: 72
In MyBatis, the "whenPresent" tag is used in the `<set>` element of an update statement to conditionally update a column if the corresponding property is not null. It provides a way to dynamically include or exclude columns from the update statement based on their values.
Here's an example of how to use the "whenPresent" tag in MyBatis:
```xml
<update id="updateUser" parameterType="User">
UPDATE users
<set>
<if test="name != null">
name = #{name},
</if>
<if test="age != null">
age = #{age},
</if>
</set>
WHERE id = #{id}
</update>
```
In this example, the `name` and `age` columns are conditionally updated based on the values of the `name` and `age` properties of the `User` object. If either of the properties is not null, the corresponding column will be included in the update statement.
I hope this helps! Let me know if you have any further questions.
阅读全文