title: MyBatis/iBatis - reusable sql fragments in a separate SQL Map file?
body: <p>I would like to put sql fragments used by several of my SQL Map XML files in a separate file. At the moment, the <code>&lt;sql&gt;</code> elements with these fragments are in one of the mappers together with other elements like <code>&lt;select&gt;</code>, which makes them hard to find.</p>
  
  <p>Can I have a mapper that defines just a few <code>&lt;sql&gt;</code> elements and is not used to generate an implementation to an interface? What would be the correct namespace of this mapper?</p>
  
  <p>This is the SQL Map file with the framents:</p>
  
  <pre><code>&lt;mapper namespace="com.company.project.dao.someDao"&gt;
  
      &lt;sql id="whereDate"&gt;
          WHERE date(`time`) BETWEEN #{startDate} AND #{endDate}
      &lt;/sql&gt;  
  
      &lt;sql id="someOtherSqlFragment"&gt;
          ...
      &lt;/sql&gt;
  
      &lt;select id="getSomeData" resultType="SomeClass" parameterType="DateParam" &gt;
          SELECT some_column, another_column          
          FROM some_table
          &lt;include refid="whereDate"/&gt;
          &lt;include refid="otherSqlFragment"/&gt;
      &lt;/select&gt;
  
  &lt;/mapper&gt;
  </code></pre>
  
  <p>I'd like to separate the elements like this:<br>
  First Sql Map file:</p>
  
  <pre><code>&lt;mapper namespace="com.company.project.dao.???"&gt;
  
      &lt;sql id="whereDate"&gt;
          WHERE date(`time`) BETWEEN #{startDate} AND #{endDate}
      &lt;/sql&gt;  
  
      &lt;sql id="someOtherSqlFragment"&gt;
          ...
      &lt;/sql&gt;
  
  &lt;/mapper&gt;
  </code></pre>
  
  <p>Second Sql Map file:</p>
  
  <pre><code>&lt;mapper namespace="com.company.project.dao.someDao"&gt;
  
      &lt;select id="getSomeData" resultType="SomeClass" parameterType="DateParam" &gt;
          SELECT some_column, another_column          
          FROM some_table     
          &lt;include refid="whereDate"/&gt;
          &lt;include refid="otherSqlFragment"/&gt;
      &lt;/select&gt;
  
  &lt;/mapper&gt;
  </code></pre>