我在 MyBatis v3 映射器 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
您可以尝试将该部分封装在里面<trim>
标签。它会是这样的:
<trim prefix="(" prefixOverrides="OR" suffix=")">
<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>
</trim>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)