mybatis的sql查询中的枚举常量

mak*_*aks 4 java sql mybatis

我需要在查询中引用枚举常量。我试过下一个例子

<select=...>
  select * from tableA where value = @MyEnum@Value.tostring()
</select>
Run Code Online (Sandbox Code Playgroud)

但它只是插入@MyEnum@Value.tostring() 值。我也试过

#{@MyEnum@Value.tostring()}
Run Code Online (Sandbox Code Playgroud)

但它被视为查询参数。那么如何在查询中使用枚举常量呢?

PS 值列是 varchar

Krz*_*ski 6

如果你想访问 MyBatis 中的任何枚举常量,你应该使用这种形式:

给定枚举:

package org.sample.domain;

public enum Currency {
    USD("$"), YEN("Y"), PLN("zl");

    private String symbol;

    Currency(String symbol) {
        this.symbol = symbol;
    }

    public String getSymbol() {
        return this.symbol
    }
}
Run Code Online (Sandbox Code Playgroud)

当您想使用枚举的任何属性时。

然后你必须在你的 xml 文件中使用这个表单:

<select id="report" resultMap="resultMap">
    SELECT *
    FROM invoices
    WHERE currency_symbol = '${@org.sample.domain.Currency@USD.getSymbol()}'
</select>
Run Code Online (Sandbox Code Playgroud)

MyBatis 注入的值没有任何引号,因此您必须将其括在引号中。

用 MyBatis 3.3.1 测试