我在发布问题后立即看到了解决方案.在
BaseTypeHandler代码:
[...]
public T getResult(ResultSet rs, String columnName) throws SQLException {
T result = getNullableResult(rs, columnName);
if (rs.wasNull()) {
return null;
} else {
return result;
}
}
public T getResult(ResultSet rs, int columnIndex) throws SQLException {
T result = getNullableResult(rs, columnIndex);
if (rs.wasNull()) {
return null;
} else {
return result;
}
}
public T getResult(CallableStatement cs, int columnIndex) throws SQLException {
T result = getNullableResult(cs, columnIndex);
if (cs.wasNull()) {
return null;
} else {
return result;
}
}
public abstract void setNonNullParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException;
public abstract T getNullableResult(ResultSet rs, String columnName) throws SQLException;
public abstract T getNullableResult(ResultSet rs, int columnIndex) throws SQLException;
public abstract T getNullableResult(CallableStatement cs, int columnIndex) throws SQLException;
[...]
显然,当数据库中的值为null时,BaseTypeHandler将返回null,因为在这种情况下cs.WasNull()为true.因此,解决方案是创建一个具体的TypeHandler,返回适当的实现(在这种情况下,当数据库中的值为null时为NullObject实现),而不继承BaseTypeHandler.
EDITED(以下是quux00的评论):当然,我们可以覆盖BaseTypeHandler的getResult方法,因为它的方法setParameters提供的功能很有用.