mybatis choose when==
时间: 2023-10-22 14:05:58 浏览: 47
The "choose" statement in MyBatis is used to implement conditional logic within a SQL query. It works similar to the "switch" statement in programming languages. Here's an example:
```xml
<select id="getUser" resultType="User">
SELECT * FROM users
<where>
<choose>
<when test="id != null">
AND id = #{id}
</when>
<when test="name != null">
AND name = #{name}
</when>
<otherwise>
AND age >= #{minAge}
</otherwise>
</choose>
</where>
</select>
```
In this example, the "getUser" query selects records from the "users" table based on different conditions. If the "id" parameter is not null, it adds the condition "id = #{id}" to the query. If the "name" parameter is not null, it adds the condition "name = #{name}". Otherwise, it adds the condition "age >= #{minAge}" to the query.
The "choose" statement allows you to handle multiple conditions within a single query, making your SQL statements more flexible and dynamic.