在Mybatis中是没有单独提供if-else if-else这种语法的。不过使用它的choose语法来实现。语法如下:
<choose>
<when test="params!=null">
right JOIN
</when>
<otherwise>
LEFT JOIN
</otherwise>
</choose>
1.单个if-else使用
<select id="selectXxx" resultType="">
SELECT
*
FROM
xxx
WHERE
1=1
<choose>
<when test="xState == 1">
AND xName = #{name1}
</when>
<otherwise>
AND xName = #{name2}
</otherwise>
</choose>
</select>
2.多个if-else if-else if-else使用
<select id="selectXxx" resultType="">
SELECT
*
FROM
xxx
<where>
<choose>
<when test="xState == 1">
AND xName = #{name1}
</when>
<when test="xState == 2">
AND xName = #{name2}
</when>
<otherwise>
AND xName = #{name3}
</otherwise>
</choose>
</where>
</select>