sql

This element can be used to define a reusable fragment of SQL code that can be included in other statements. It can be statically (during load phase) parametrized. Different property values can vary in include instances. For example:

  1. <sql id="userColumns"> ${alias}.id,${alias}.username,${alias}.password </sql>

The SQL fragment can then be included in another statement, for example:

  1. <select id="selectUsers" resultType="map">
  2. select
  3. <include refid="userColumns"><property name="alias" value="t1"/></include>,
  4. <include refid="userColumns"><property name="alias" value="t2"/></include>
  5. from some_table t1
  6. cross join some_table t2
  7. </select>

Property value can be also used in include refid attribute or property values inside include clause, for example:

  1. <sql id="sometable">
  2. ${prefix}Table
  3. </sql>
  4. <sql id="someinclude">
  5. from
  6. <include refid="${include_target}"/>
  7. </sql>
  8. <select id="select" resultType="map">
  9. select
  10. field1, field2, field3
  11. <include refid="someinclude">
  12. <property name="prefix" value="Some"/>
  13. <property name="include_target" value="sometable"/>
  14. </include>
  15. </select>