13

列挙定数パラメーターに基づいて myBatis 3.1.1 で動的 SQL を実行する方法は?

4

4 に答える 4

17

列挙型定数に基づく動的 SQL の実行方法

public enum Test {
    A, B;
}

Mapper.java:
    int test(@Param("t") Test t);

Mapper.xml:
    <select id="test" resultType="int">
        select
        <choose>
            <when test='t.name().equals("A")'>65</when>
            <when test='t.name().equals("B")'>66</when>
            <otherwise>0</otherwise>
        </choose>
    </select>   

ノート

  • テスト式は、一重引用符ではなく二重引用符を使用して文字列を参照する必要があります。
  • 定数は比較できず、文字列のみを比較できます。
于 2012-10-17T11:58:33.623 に答える
7

MyBatis では、 (または) ステートメント内の文字列==の代わりに使用できます。したがって、以下も機能します (引用符は関係ありません)。equalsifwhen

public enum Test {
    A, B;
}

Mapper.java:

int test(@Param("t") Test t);

Mapper.xml:

<select id="test" resultType="int">
    select
    <choose>
        <when test="t.name() == 'A'">65</when>
        <when test="t.name() == 'B'">66</when>
        <otherwise>0</otherwise>
    </choose>
</select>
于 2014-03-18T12:21:01.980 に答える