Java Reference
In-Depth Information
mapped statement to run, the parameters required by the mapped statement, and
the row handler instance. The following example creates an XML document with
all of the accounts returned by a mapped statement encoded as XML :
AccountXmlRowHandler rh = new AccountXmlRowHandler();
sqlMapClient.queryWithRowHandler("Account.getAll", null, rh);
String xmlData = rh.getAccountListXml();
If XML is not your cup of tea, maybe this next example will help you see how use-
ful row handlers can be.
Another more interesting RowHandler example
Another example of how to use a row handler is to handle several aspects of mul-
tiple table relationships. For example, in our sample database, we have accounts
(or customers), who can have multiple orders, which can have multiple order
items, which have a product, and each product has a manufacturer. Figure 6.1
shows the data model for the relationships.
Let's imagine a requirement where we need to provide a list of products that
were ordered and a list of the accounts that had ordered that product. We also
want a list of accounts, and we want each of those accounts to have a list of manu-
facturers that they had ordered from. It might also be nice to have these as a Map
object (by ID ) that we could use to quickly find an account or product.
Although we could do that using the existing groupBy attribute and the query-
ForMap method, using four mapped statements, this approach would require four
separate select statements (meaning more database I/O ) and would potentially
give us multiple copies of each object. The customer objects returned by the first
Account
PK
OrderItem
PK
Order
PK
accountId
orderItemId
orderId
FK1
accountId
FK1 orderId
FK2 productId
Manufacturer
PK manufacturerId
Product
PK
productId
FK1 manufacturerId
Figure 6.1
Entity relationship diagram for examples
Search WWH ::




Custom Search