Java Reference
In-Depth Information
and generally speaking, using it is a bad practice. But sometimes, you don't have
any choice—and in some cases it may make perfect sense.
Because
iBATIS
is configured with
XML
files, the null value replacement is
specified as a String. For example:
<result property="enabled" column="Enabled" nullValue="NO"/>
For that reason, something has to perform the translation into the real type.
iBA-
TIS
relies on the
valueOf()
method of the
TypeHandlerCallback
to do the transla-
tion. In our case, we'd need to translate the value
NO
into a boolean value of
false
.
Luckily, doing this is usually very similar to the translation we've already done to
get a result. In fact, in the case of our
YesNoTypeHandlerCallback
, it is exactly the
same. So the implementation would look like this:
public Object valueOf(String s) {
return yesNoToBoolean(s);
}
That's it! We've completed our custom type handler. Listing 12.2 contains the
complete source.
Listing 12.2
A TypeHandler
public class YesNoTypeHandlerCallback
implements TypeHandlerCallback {
Contains constants for yes
and no database values
public static final String YES = "YES";
public static final String NO = "NO";
Sets parameters with
our type handler
public void setParameter(
ParameterSetter setter, Object parameter
)
throws SQLException {
setter.setString(booleanToYesNo((Boolean)parameter));
}
Gets results with our
type handler
public Object getResult(ResultGetter getter)
throws SQLException {
return yesNoToBoolean(getter.getString());
}
Converts string to our type
public Object valueOf(String s) {
return yesNoToBoolean(s);
}
Converts string to Boolean
private Boolean yesNoToBoolean(String s) {
if (YES.equalsIgnoreCase(s)) {
return Boolean.TRUE;



















