I know this has been asked before, but I wasn\'t able to implement a solution based on the information I found so far. so perhaps someone can explain it to me.
I have a
You can use Custom TypeHandler for converting you result directly into ENUM so that you don't need to put all values in your database as UPPER CASE ENUM Names.
This is how your Status Enum Custom Handler will look like
public class StatusTypeHandler implements TypeHandler {
public Status getResult(ResultSet rs, String param) throws SQLException {
return Status.getEnum(rs.getInt(param));
}
public Status getResult(CallableStatement cs, int col) throws SQLException {
return Status.getEnum(cs.getInt(col));
}
public void setParameter(PreparedStatement ps, int paramInt, Status paramType, JdbcType jdbctype)
throws SQLException {
ps.setInt(paramInt, paramType.getId());
}
}
Define your TypeHandler to handle Status by default in your mybatis-config.xml by adding this code.
Now let us consider an example where you have following two functions in your Dao,
Status getStatusById(int code);
Status getStatusByName(String name);
Your mapper will look like
Now as the resultType for both the mapper is Status, myBatis will use the CustomTypeHandler for this type i.e. StatusTypeHandler instead of EnumTypeHandler that it uses by default for Handling Enums, so there would be no need to maintain proper Enum names in your database.