如何重用带有参数的SQL片段?

Jin*_*won 6 ibatis mybatis

我打算制作一个片段,用于重用参数.

<insert ...>
  <selectKey keyProperty="id" resultType="_long" order="BEFORE">
    <choose>
      <when test="_databaseId == 'derby'">
        VALUES NEXT VALUE FOR SOME_ID_SEQ
      </when>
      <otherwise>
        SELECT SOME_ID_SEQ.NEXTVAL FROM DUAL
      </otherwise>
    </choose>
  </selectKey>
  INSERT INTO ...
</insert>
Run Code Online (Sandbox Code Playgroud)

我可以使用参数创建SQL片段吗?

<sql id="selectKeyFromSequence">
  <selectKey keyProperty="id" resultType="_long" order="BEFORE">
    <choose>
      <when test="_databaseId == 'derby'">
        VALUES NEXT VALUE FOR #{sequenceName}
      </when>
      <otherwise>
        SELECT #{sequenceName}.NEXTVAL FROM DUAL
      </otherwise>
    </choose>
  </selectKey>
</sql>
Run Code Online (Sandbox Code Playgroud)

这样我可以像这样重用它们吗?

<insert ...>
  <include refid="...selectKeyFromSequence"/> <!-- How can I pass a parameter? -->
  INSERT INTO ...
</insert>
Run Code Online (Sandbox Code Playgroud)

这可能吗?

Vla*_*ejs 13

从版本3.3.0开始,您可以这样做:

<sql id="myinclude">
  from ${myproperty}
</sql>

<include refid="myinclude">
  <property name="myproperty" value="tablename"/>
</include>
Run Code Online (Sandbox Code Playgroud)

请参阅http://www.mybatis.org/mybatis-3/sqlmap-xml.html中的 SQL部分