Java Reference
In-Depth Information
RowHandler
objects can help speed up the processing of large sets of data if
needed. This is a last resort for dealing with large data sets, but it is also the Swiss
army knife of i
BATIS
. You can do almost anything with a
RowHandler
.
In section 6.1.2, we looked at the
XML
result-generation capabilities in i
BATIS
,
and found them to be lacking in some ways—notably in the case where you want
to get a single
XML
document for a list of objects or for a complex object. In that
section we promised to show you how to get
XML
data using less memory than get-
ting an entire list or object graph and iterating through it. Using a
RowHandler
, we
still iterate through the objects, but only one element of that list is in memory at
one time. Here is a row handler that builds a single
XML
document with multiple
<account>
elements in it:
public class AccountXmlRowHandler implements RowHandler {
private StringBuffer xmlDocument = new StringBuffer("<AccountList>");
private String returnValue = null;
public void handleRow(Object valueObject) {
Account account = (Account) valueObject;
xmlDocument.append("<account>");
xmlDocument.append("<accountId>");
xmlDocument.append(account.getAccountId());
xmlDocument.append("</accountId>");
xmlDocument.append("<username>");
xmlDocument.append(account.getUsername());
xmlDocument.append("</username>");
xmlDocument.append("<password>");
xmlDocument.append(account.getPassword());
xmlDocument.append("</password>");
xmlDocument.append("</account>");
}
public String getAccountListXml(){
if (null == returnValue){
xmlDocument.append("</AccountList>");
returnValue = xmlDocument.toString();
}
return returnValue;
}
}
The code to use this with an existing mapped statement that returns a list of
Account
objects is remarkably simple. The basic design is that you create an
instance of a
RowHandler
and call the
queryWithRowHandler
method, passing in the
