我覺得這是一個簡單的問題,但我試過的東西都不適合我。我有一個枚舉,我有字符串構造函數的原因是因爲Java不允許枚舉數字..我直接嘗試AA,AB,2C沒有字符串構造函數,但是,這給出了一個錯誤。請注意,對於現有的枚舉,我將添加C(「2C」)。Java MyBatis枚舉字符串值
public enum TestEnum{
AA("AA"), AB("AB"), C("2C");
private String display;
private TestEnum(String display) {
this.display = display;
}
public String toString() {
return display;
}
public String getDisplay() {
return display;
}
public void setDisplay(String display) {
this.display = display;
}
public String getName() {
return display;
}
現在我有一個MyBatis的映射器,它確實這是現有的和參數去映射器之一是TestEnum合併。直到現在這工作得很好,因爲枚舉值和字符串值是相同的,但我加了C(「2C」)。現在,我想用mybaits插入2C表,但它總是插入C.
merge into text t
using (select #{id} as id from dual) d on (d.id = t.id)
when matched then
update set
appId = #{applId},
src = #{testEnum}
testEnum插入C,所以我改變了對#{testEnum.toString()},它給了我一個不存在的getter爲屬性名稱toString()錯誤。我試過#{testEnum.display}和#{testEnum.name},它們仍然插入C,而我希望它插入2C。你們知道一個更簡單的方法來處理這個問題嗎?
我不想改變模型對象來傳遞字符串而不是TestEnum,因爲這個對象正在很多地方使用。是否有一種方法可以在mybatis映射器中完成而無需更改模型對象?
感謝您的幫助:)
太棒了,正是我需要的。非常感謝你 –