floater@cjsdn.com
Spring and iBATIS
Revised 11/19/2004 Page 5 of 21
<insert id="insertUser" parameterClass="org.springibatis.test.CertUser">
<selectKey resultClass=”long” keyProperty=”userId”>
IDENTITY_VAL_LOCAL()
</selectKey>
INSERT INTO user (name, age, st)
VALUES(#name#, #age#, #status#)
</insert>
<update id="updateUser" parameterClass="org.springibatis.test.CertUser">
UPDATE user
SET name=#name#, age=#age#, st=#status#
WHERE userid=#userId#
</update>
<delete id="deleteUser" parameterClass="org.springibatis.test.CertUser">
DELETE FROM user WHERE userid=#userId#
</delete>
</sqlMap>
We save this file as user.xml, to the same directory where the User class is. The implication of this is when we build the
java class, we need to copy this file to the class directory so we could read it from the class path. We will talk about the
package structures later on. Here are some more comments on this file:
We are using version 2 of SqlMap, there are some syntax difference between version 1.x and 2.0.
If the table column names and java class fields are difference in the select statements, we have to use “select …
as …” to translate them, otherwise we have to use parameterMap tags, which have to be above the statement
tags(The order is significant). The java class fields are referenced by field names, enclosed with a pair of #’s.
Use <![CDATA[ ...... ]]> syntax to avoid the conflict between SQL and XML, if necessary.
In the insert SQL statement, we have an extra selectKey call to retrieve the auto-incremental key, once the
insert is done, the key will be sent back and populated in the id field. This selectKey would work with the
insert statement in a transaction on the same connection.
The System ID entry in the DOCTYPE could be a troublemaker, depends on where you are going to load this
XML file and whether you have internet access to the real site. If you are building a web application, you may
save a copy of the dtd files under /WEB-INF/dtd and point the System ID to that location. If you try to run in a
JUNIT testcase, this will not work. If possible, you may just create a /WEB-INF/dtd directoy under C:, then it
works in both cases. Since normally these dtd files are not changed, this compromise is OK. Otherwise, it’s
annoying to change the pointer back and forth. In JBuilder or WSAD, you can reset the pointer in the
Tools/Preference/XML/SystemID catalog and don’t worry about it anymore, nice feature!
Having done these, now let’s turn to how to run these SQL statements. In order to do this, we need one more iBATIS
config file to specify the datasource. Here is the iBATIS config file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<transactionManager type="JDBC">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="COM.ibm.db2.jdbc.app.DB2Driver"/>
<property name="JDBC.ConnectionURL" value="jdbc:db2:DB_SS"/>
<property name="JDBC.Username" value="username"/>
<property name="JDBC.Password" value="password"/>
</dataSource>
</transactionManager>
<sqlMap resource="org/springibatis/test/user.xml"/>
</sqlMapConfig>
Here we define a data source using JDBC and hook the user.xml file for the User class to the SqlMap engine. For other
types of dataSource or TransactionManager, or other settings, check SqlMap documents. One note is that the order of
entries in this file is significant. Save this file as sqlMapConfig.xml (any name should be fine, but this is the
convention) in the same directory as the user.xml directory. Noting that this is a minimal setting for sqlMapConfig, you
may check the iBATIS developer manual for other setting, like resource properties, sessions, alias, etc.