mybatis choose
时间: 2023-09-26 18:12:28 浏览: 93
Mybatis的一些实践
MyBatis `choose` element is used in XML mapping files to perform conditional branching. It allows you to specify multiple conditions and their corresponding SQL statements. The `choose` element works similar to the `switch` statement in programming languages.
Here's an example usage of the `choose` element in MyBatis:
```xml
<select id="getUsers" resultType="User">
SELECT * FROM users
<where>
<choose>
<when test="status == 'active'">
AND status = 'active'
</when>
<when test="status == 'inactive'">
AND status = 'inactive'
</when>
<otherwise>
AND status IS NULL
</otherwise>
</choose>
</where>
</select>
```
In this example, the SQL query will include different conditions based on the value of the `status` parameter. If the `status` parameter is "active", it will add the condition `AND status = 'active'`. If the `status` parameter is "inactive", it will add the condition `AND status = 'inactive'`. Otherwise, it will add the condition `AND status IS NULL`.
The `choose` element is useful when you need to dynamically generate SQL statements based on certain conditions.
阅读全文