本文介绍了Mybatis枚举按ID选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的MyBatis代码未加载枚举类型(版本:3.4.4).

My MyBatis code doesn't load the enum type (version: 3.4.4).

在MySQL数据库中,我有一个带有"cartype"字段的表,该表是INT(11)类型.在Java中,我创建了一个ENUM来处理汽车类型:

At MySQL database, I have a table with "cartype" field which is an INT(11) type.In Java I created an ENUM for handling car types:

public enum CarType {
    SEDAN(1), LIMUSIN(2), WAGON(3);

    private int id;

    CarType(int id) {
        this.id = id;
    }

    public int getId() {
        return this.id;
    }
}

汽车映射器xml看起来像这样(不包含所有数据):

Car mapper xml looks like this (contains no all data):

<select id="selectCar" parameterType="Car" resultMap="carResultMap">
SELECT * FROM Cars WHERE car_name="#{carName}";
</select>

<resultMap id="carResultMap" type="Car">
    <id property="id" column="car_name"/>
    <result property="carType" column="cartype"/>
</resultMap>

最后,我的bean如下所示:

Finally my bean look like the followings:

public class Car {
    private Integer id;
    private CarType carType;
}

bean也包含getter和setter.

The bean contains getters and setters too.

当我尝试用Java获取Car时,它会引发以下异常:

When I try to get the Car in java, it throws the following exception:

Caused by: java.lang.IllegalArgumentException: No enum constant org.data.bean.CarType.1
    at java.lang.Enum.valueOf(Enum.java:238)
    at org.apache.ibatis.type.EnumTypeHandler.getNullableResult(EnumTypeHandler.java:49)
    at org.apache.ibatis.type.EnumTypeHandler.getNullableResult(EnumTypeHandler.java:26)
    at org.apache.ibatis.type.BaseTypeHandler.getResult(BaseTypeHandler.java:66)

推荐答案

这是需要存储在数据库中的枚举名称,而不是id.

It's the enum name that needs to be stored in the database, not the id.

此处.默认的EnumTypeHandler期望使用varchar:

Take a look here . The default EnumTypeHandler expects a varchar:

如果要或必须存储ID,则需要自定义 TypeHandler .像这样:

If you want or have to store the id you will need a custom TypeHandler. Something like this:

public class CarTypeTypeHandler implements TypeHandler<CarType> {

   public void setParameter(PreparedStatement ps, int paramInt, CarType paramType, JdbcType jdbctype)
         throws SQLException {
      ps.setInt(paramInt, paramType.getId());
   }

   @Override
   public CarType getResult(ResultSet rs, String param) throws SQLException {
      return CarType.get(rs.getInt(param));
   }

   @Override
   public CarType getResult(CallableStatement cs, int col) throws SQLException {
      return CarType.get(cs.getInt(col));
   }
}

在枚举中也具有查找方法会很方便:

It will be convenient to have a lookup method in your enum too:

public static CarType get(int code) {
   for(CarType s : values()) {
      if(s.id == code) return s;
   }
   return null;
}

您可能还需要在映射器中明确指出枚举类型.像这样添加一个javaType参数(我还包括一个示例类型处理程序定义):

You may also need to indicate the enum type explicitly in your mapper. Add a javaType parameter like so (I'm also including a sample type handler definition):

<result property="carType"
  column="cartype" javaType="path.to.package.CarType"
  typeHandler="path.to.package.CarTypeTypeHandler"/>

这篇关于Mybatis枚举按ID选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 09:27