Java MyBatis枚举字符串值 [英] Java MyBatis Enum string value

查看:425
本文介绍了Java MyBatis枚举字符串值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得这是一个简单的问题,但是我尝试过的所有事情都不适合我.我有一个枚举,我有字符串构造函数的原因是因为Java不允许枚举为数字..我在没有字符串构造函数的情况下直接尝试了AA,AB,2C,但是却给出了错误.请注意,对于现有的枚举,我要添加C("2C").

I feel like this is a simple problem, but none of the things i tried work for me. I have an enum, the reason i have string constructor is because Java doesn't allow enum to be numerical..I tried AA, AB, 2C directly without string constructor but that gives an error. Note that for the existing enum i am adding C("2C").

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.

Now i have a mybatis mapper which does a merge this is existing and one of the param to the mapper is TestEnum. Until now this worked fine since enum value and string value are same, but i added C("2C"). Now i want to insert 2C to the table using mybaits, but it always inserts 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 inserts C, so i changed that to #{testEnum.toString()} which gave me a there is no getter for property name toString() error. I tried #{testEnum.display} and #{testEnum.name} both of them still inserts C whereas i want it to insert 2C. Do you guys know an easier way of handling this?

我不想更改模型对象以传递String而不是TestEnum,因为该对象已在许多地方使用.是否有一种方法可以在mybatis映射器中完成而不更改模型对象?

I don't want to change the model object to pass String rather than TestEnum because this object is being used in many places.Is there a way this can be done in the mybatis mapper without changing model object?

感谢您的帮助:)

推荐答案

您需要的是首先,在您的TestEnum中添加静态方法以返回给定显示字符串的TestEnum:

First, add a static method to your TestEnum to return a TestEnum given a display string:

public static TestEnum fromDisplay(String display){
    for (TestEnum v : TestEnum.values()){
        if (v.getDisplay().equals(display)){
            return v;
        }
    }
    return null;
}

然后使用它来创建您的TypeHandler:

Then use it to create your TypeHandler:

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

public class TestEnumTypeHandler extends BaseTypeHandler<TestEnum> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, TestEnum parameter, JdbcType jdbcType)
            throws SQLException {
        ps.setString(i, parameter.getDisplay());
    }

    @Override
    public TestEnum getNullableResult(ResultSet rs, String columnName) throws SQLException {
        return TestEnum.fromDisplay(rs.getString(columnName));
    }

    @Override
    public TestEnum getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        return TestEnum.fromDisplay(rs.getString(columnIndex));
    }

    @Override
    public TestEnum getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return TestEnum.fromDisplay(cs.getString(columnIndex));
    }
}

最后,在mybatis xml中注册TypeHandler:

Finally, register your TypeHandler in your mybatis xml:

<typeHandlers>
  <typeHandler handler="blah.blah.TestEnumTypeHandler "/>
</typeHandlers>

这篇关于Java MyBatis枚举字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆