[英]mapping enum on ibatis
I have an enum something like this 我有一个像这样的枚举
public Enum MyEnum {
NEW("NEW"),
OLD("OLD"),
IN_PROCESS("IN PROCESS");
}
The mapping on ibatis works fine for the NEW and OLD , but encounters an error when the IN_PROCESS is encountered since the value of my IN_PROCESS in the DB is IN PROCESS , and the error indicates that ibatis tries to find an enum with that value, can someone suggest a solution? ibatis上的映射适用于NEW和OLD ,但遇到IN_PROCESS时遇到错误,因为DB中我的IN_PROCESS的值是IN PROCESS ,并且错误表明ibatis试图找到具有该值的枚举,can有人建议解决方案?
MyBatis use an EnumTypeHandler to do mappings with enums. MyBatis使用EnumTypeHandler与枚举进行映射。 In this Enum type handler it uses the
name()
method of the Enums which returns the string value of the variable name. 在这个Enum类型处理程序中,它使用Enums的
name()
方法,该方法返回变量名称的字符串值。 For example NEW -> "NEW" and IN_PROCESS -> "IN_PROCESS". 例如NEW - >“NEW”和IN_PROCESS - >“IN_PROCESS”。
Otherwise, to get the value it uses Enum.valueOf(type, s);
否则,要获取它使用的值
Enum.valueOf(type, s);
which gets the value of the Enum through the String value which corresponds with the variable name ("NEW" -> MyEnum.NEW, "IN_PROCESS" -> MyEnum.IN_PROCESS) and internally is used the method name()
. 它通过与变量名称对应的String值获取Enum的值(“NEW” - > MyEnum.NEW,“IN_PROCESS” - > MyEnum.IN_PROCESS),并在内部使用方法
name()
。
You cannot overwrite name()
because is marked as final so you options are: 你不能覆盖
name()
因为标记为final,所以你的选项是:
The easy way is to use IN_PROCESS instead of IN PROCESS. 简单的方法是使用IN_PROCESS而不是IN PROCESS。 I prefer this, is easier and fast.
我更喜欢这个,更容易,更快。
The second option is create your TypeHanlder for this enum and check if the parameter
is IN PROCESS and search with IN_PROCESS. 第二个选项是为此枚举创建TypeHanlder并检查
parameter
是否为IN PROCESS并使用IN_PROCESS进行搜索。
I had the same issue, I ended up writing a custom setter on my POJO to convert the string value. 我有同样的问题,我最终在我的POJO上编写了一个自定义setter来转换字符串值。
public Enum MyEnum {
NEW("NEW"),
OLD("OLD"),
IN_PROCESS("IN PROCESS");
public static MyEnum fromValue(String v){
.... find the enum based on value
}
}
public class POJO {
private MyEnum myEnum;
public void setMyEnum(String strV){
myEnum=MyEnum.fromValue(strV)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.