Mybatis之动态查询:choose、when和otherwise标签使用

发布时间 2023-07-28 15:10:24作者: 勇敢-的心
【使用场景】
有的时候,我们需要根据不同的选择,关联不同的表,这个时候<choose/><when/>和<otherwise>标签就发挥作用了。
比如说,内部用户和外部用户表是分开的,在查询用户的时候,我们就需要根据角色类型去选择不同的关系表进行关联。

SELECT
*
FROM role r
LEFT JOIN
<!-- 当角色分类选择分包商的时候.就和用户分包商角色表进行关联查询;否则,就和用户角色表关联查询.-->
<choose>
<when test="roleVo.roleType != null and roleVo.roleType == @com.test.constant.RoleConstant@SUB_CONTRACTOR_ROLE_TYPE">
user_sub_contractor_role u
</when>
<otherwise>
user_role u
</otherwise>
</choose>
on u.role_id=r.id
WHERE
... ...