publicCustomEnumTypeHandler(Class<E> type){ if (type == null) thrownew IllegalArgumentException("Type argument cannot be null"); this.type = type; E[] enums = type.getEnumConstants(); if (enums == null) thrownew IllegalArgumentException(type.getSimpleName() + " does not represent an enum type."); }
@Override publicvoidsetNonNullParameter(PreparedStatement ps, int i, E parameter, JdbcType jdbcType)throws SQLException { ps.setInt(i, parameter.getValue()); }
@Override public E getNullableResult(ResultSet rs, String columnName)throws SQLException { int i = rs.getInt(columnName);
if (rs.wasNull()) { returnnull; } else { try { return CommonEnum.getEnum(type, i); } catch (Exception ex) { thrownew IllegalArgumentException("Cannot convert " + i + " to " + type.getSimpleName() + " by int value.", ex); } } }
@Override public E getNullableResult(ResultSet rs, int columnIndex)throws SQLException { int i = rs.getInt(columnIndex); if (rs.wasNull()) { returnnull; } else { try { return CommonEnum.getEnum(type, i); } catch (Exception ex) { thrownew IllegalArgumentException("Cannot convert " + i + " to " + type.getSimpleName() + " by int value.", ex); } } }
@Override public E getNullableResult(CallableStatement cs, int columnIndex)throws SQLException { int i = cs.getInt(columnIndex); if (cs.wasNull()) { returnnull; } else { try { return CommonEnum.getEnum(type, i); } catch (Exception ex) { thrownew IllegalArgumentException("Cannot convert " + i + " to " + type.getSimpleName() + " by int value.", ex); } }