sql
这个元素可以被用来定义可重用的 SQL 代码段,可以包含在其他语句中。它可以被静态地(在加载参数) 参数化. 不同的属性值通过包含的实例变化. 比如:
- <sql id="userColumns"> ${alias}.id,${alias}.username,${alias}.password </sql>
这个 SQL 片段可以被包含在其他语句中,例如:
- <select id="selectUsers" resultType="map">
- select
- <include refid="userColumns"><property name="alias" value="t1"/></include>,
- <include refid="userColumns"><property name="alias" value="t2"/></include>
- from some_table t1
- cross join some_table t2
- </select>
属性值也可以被用在 include 元素的 refid 属性里(
- <include refid="${include_target}"/>
)或 include 内部语句中(
- ${prefix}Table
),例如:
- <sql id="sometable">
- ${prefix}Table
- </sql>
- <sql id="someinclude">
- from
- <include refid="${include_target}"/>
- </sql>
- <select id="select" resultType="map">
- select
- field1, field2, field3
- <include refid="someinclude">
- <property name="prefix" value="Some"/>
- <property name="include_target" value="sometable"/>
- </include>
- </select>