title: MyBatis - Nested Conditions in Where Clause
body: <p>I'm dynamically generating the where clause inside MyBatis v3 mapper xml. However placing parentheses is really cumbersome. Is there an easier way to handle the problem without using if statements?</p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;where&gt;
    &lt;if test="filter != null"&gt;
      &lt;choose&gt;
        &lt;when test="filter.lref != null"&gt;
          file.lref = #{filter.lref}
        &lt;/when&gt;
        &lt;otherwise&gt;
          &lt;!-- I don't want to use this --&gt;
          &lt;if test="filter.forLike != null || filter.forInt != null"&gt;
            ( 
          &lt;/if&gt;
          &lt;if test="filter.forLike != null" &gt;
            subject LIKE #{filter.forLike}    
            OR requester_identifier LIKE #{filter.forLike}
            OR requester_name LIKE #{filter.forLike}
          &lt;/if&gt;
          &lt;if test="filter.forInt != null"&gt;
            OR file_id = #{filter.forInt}
          &lt;/if&gt;
  
          &lt;!-- I don't want to use this --&gt;
          &lt;if test="filter.forLike != null || filter.forInt != null"&gt;
            ) 
          &lt;/if&gt;
        &lt;/otherwise&gt;
      &lt;/choose&gt;
    &lt;/if&gt;
    &lt;if test="listMode &gt; 0"&gt;
      &lt;choose&gt;
         &lt;when test="listMode == 1"&gt;
           AND file_status_link.dosya_ref is not NULL
         &lt;/when&gt;
         &lt;otherwise&gt;
           AND file_status_link.dosya_ref is NULL
         &lt;/otherwise&gt;
      &lt;/choose&gt;
     &lt;/if&gt;            
  &lt;/where&gt;
  </code></pre>
  
  <p>Sample dynamically generated SQL output is as follows</p>
  
  <pre><code>WHERE ( subject LIKE ? OR requester_identifier LIKE ? OR requester_name LIKE ? ) 
  AND file_status_link.dosya_ref is NULL 
  </code></pre>