mybatis else 表达式
时间: 2023-10-26 15:09:53 浏览: 59
MyBatis的`<otherwise>`标签是用于在`<choose>`语句中定义默认条件的。`<choose>`语句中可以有多个`<when>`标签,每个`<when>`标签定义一个条件,如果这些条件都不满足,就会执行`<otherwise>`标签中的语句。
以下是一个示例:
```
<select id="findOrders" parameterType="map" resultType="Order">
SELECT * FROM ORDERS
WHERE 1=1
<choose>
<when test="status != null">
AND STATUS = #{status}
</when>
<when test="startDate != null and endDate != null">
AND ORDER_DATE BETWEEN #{startDate} AND #{endDate}
</when>
<otherwise>
AND ORDER_DATE >= DATEADD(month, -1, GETDATE())
</otherwise>
</choose>
</select>
```
在这个例子中,如果传入了`status`参数,将按照该状态查询订单;如果传入了`startDate`和`endDate`参数,将按照订单日期范围查询订单;否则将查询最近一个月的订单。如果没有`<otherwise>`标签,那么当所有条件都不满足时,将不会有任何结果返回。