Java Reference
In-Depth Information
} else if (NO.equalsIgnoreCase(s)) {
return Boolean.FALSE;
} else {
throw new IllegalArgumentException (
"Could not convert " + s +
" to a boolean value. " +
"Valid arguments are 'YES' and 'NO'.");
}
}
Converts Boolean to string
private String booleanToYesNo(Boolean b) {
if (b == null) {
throw new IllegalArgumentException (
"Could not convert null to a boolean value. " +
"Valid arguments are 'true' and 'false'.");
} else if (b.booleanValue()) {
return YES;
} else {
return NO;
}
}
}
Now that we've written our
TypeHandlerCallback
, we need to register it to be
used. The next section deals with that.
12.2.3
Registering a TypeHandlerCallback for use
To use a
TypeHandlerCallback
, we need some way to specify where and when it
should be used. There are three options:
Register it globally, in the
SqlMapConfig.xml
file.
■
Register it locally, within a single
SqlMap.xml
file.
■
Register it for a single result or parameter mapping.
■
To register the
TypeHandlerCallback
globally, simply add a
<typeHandler>
ele-
ment to your
SqlMapConfig.xml
. Here is the full example of the
<typeHandler>
element:
<typeHandler
callback="com.domain.package.YesNoTypeHandlerCallback"
javaType="boolean" jdbcType="VARCHAR" />
The
<typeHandler>
element accepts two or three attributes. The first is the
Type-
HandlerCallback
class itself. Simply specify the fully qualified class name, or if you
like, you can also use a type alias to keep your configuration more readable. The