我在 MyBatis v3 mapper xml 中动态生成 where 子句。但是,放置括号确实很麻烦。有没有更简单的方法来处理问题而不使用 if 语句?
<where>
<if test="filter != null">
<choose>
<when test="filter.lref != null">
file.lref = #{filter.lref}
</when>
<otherwise>
<!-- I don't want to use this -->
<if test="filter.forLike != null || filter.forInt != null">
(
</if>
<if test="filter.forLike != null" >
subject LIKE #{filter.forLike}
OR requester_identifier LIKE #{filter.forLike}
OR requester_name LIKE #{filter.forLike}
</if>
<if test="filter.forInt != null">
OR file_id = #{filter.forInt}
</if>
<!-- I don't want to use this -->
<if test="filter.forLike != null || filter.forInt != null">
)
</if>
</otherwise>
</choose>
</if>
<if test="listMode > 0">
<choose>
<when test="listMode == 1">
AND file_status_link.dosya_ref is not NULL
</when>
<otherwise>
AND file_status_link.dosya_ref is NULL
</otherwise>
</choose>
</if>
</where>
示例动态生成的 SQL 输出如下
WHERE ( subject LIKE ? OR requester_identifier LIKE ? OR requester_name LIKE ? )
AND file_status_link.dosya_ref is NULL