Java Reference
In-Depth Information
Therefore it is difficult for the
iBATIS
framework to support all databases using a
single type handler implementation. To deal with these situations,
iBATIS
supports
custom type handlers that allow you to customize the way that certain types are
handled. Using a custom type handler, you can tell
iBATIS
how to map relational
database types to Java types. You can even override the built-in type handlers. This
section will explain how.
12.2.1
Implementing a custom type handler
To implement a custom
TypeHandler
, you only need to implement part of the
functionality. That functionality is defined in a simple interface called
TypeHan-
dlerCallback
. It is defined in listing 12.1.
Listing 12.1
TypeHandlerCallback
public interface TypeHandlerCallback {
public void setParameter(
ParameterSetter setter, Object parameter)
throws SQLException;
public Object getResult(ResultGetter getter)
throws SQLException;
public Object valueOf(String s);
}
Let's step through the implementation of a
TypeHandlerCallback
. For the pur-
poses of this example, let's assume we have a database that uses the words “
YES
” and
“
NO
” to represent boolean values (i.e., true and false, respectively). Table 12.2
shows this example.
Table 12.2
Using YES and NO to represent boolean values
UserID
Username
PasswordHashcode
Enabled
1
asmith
1190B32A35FACBEF
YES
2
brobertson
35FACBEFAF35FAC2
YES
3
cjohnson
AF35FAC21190B32A
NO

















