Java Reference
In-Depth Information
accountId, username, password
) VALUES(
#accountId#, #username#, #password#)
</insert>
That mapped statement would expect a property named sequence which would
contain the name of the sequence to be used for the inserted record.
In the previous examples, we acquired the key by fetching the next value from
a sequence and setting it on the object before the record was inserted. On the
other hand, if we were using Microsoft SQL Server, we might use this mapped
statement instead:
<insert id="insert">
INSERT INTO Account (
username, password
) VALUES(
#username#, #password#)
<selectKey
keyProperty="accountId"
resultClass="int">
SELECT SCOPE_IDENTITY()
</selectKey>
</insert>
This example lets the database create the key when we insert the record, then
fetches the generated key and sets it onto the object that was passed into the
insert method. As far as your application is concerned, there is no difference at
all between these two mapped statements.
Earlier, we touched on the API that the JDBC 3.0 specification exposes to get
generated keys. At this point, i BATIS does not support this API , because only a lim-
ited numbers of JDBC drivers support it. As more and more begin to implement it,
it will be an option for using automatically generated keys as well.
5.3 Updating and deleting data
Now that we can insert rows into our database and figure out what the generated
keys are for the inserted data, let's take a look at updating and deleting data.
While the insert method returns an object, both the update and delete meth-
ods return a primitive integer value (or, an int value to be more correct) which
indicates how many records were updated or deleted by the mapped statement.
The i BATIS framework allows you to affect either single or multiple rows in
your database, depending on your need, with a single SQL statement. This is one
Search WWH ::




Custom Search