Using a Map (e.g., HashMap, TreeMap) as your domain model is not rec-
ommended, but this does show the level of mapping flexibility that i BA-
TIS provides. You don't necessarily always need a JavaBean to map to—
you can map directly to Maps and primitives as well.
Believe it or not, you have now seen all of the code and configuration required to
use i BATIS . We have intentionally spread it out for printing, but even with that, it
is only about 50 lines of code, including Java and XML . But the more important
point is that 45 of the 50 lines are configuration and are only written once in an
application, not for every single statement. As you saw earlier in this chapter, JDBC
can end up costing you 50 lines of code or more per statement.
Building the application
Usually when building a large application, you will use something like Ant to
make it simpler. Because this is only one class, we are not going to bother building
an Ant script for it. To build it, the only JAR files you need on the classpath are
ibatis-common-2.jar and ibatis-sqlmap-2.jar , so we will just key them in on the
command line to the Java compiler:
javac -classpath <your-path>ibatis-common-2.jar;
Of course, that should all be on one line, and instead of <your-path> , you should
substitute the actual path to the JAR files. If everything is OK , there should be no
output from the compiler to the screen, but a Main.class file should be created in
the current directory.
Running the application
We will have a few more JAR s when we run the application, but not many. To run
our application, the only JAR files we need on the classpath are ibatis-common-
2.jar , ibatis-sqlmap-2.jar , commons-logging.jar , and our JDBC driver (in this
case, mysql-connector-java.jar ), so next enter this command:
java -classpath <your-path>;mysql-connector.jar;commons-logging.jar;
Again, as with the compilation, this should all go on one line, and <your-path>
should be replaced with the actual paths on your system.
The program should run and tell you how many records were selected, and
then output the data in a rough format, something like this:
Search WWH ::