Sometimes, you may need to work with XML-based data. The iBATIS framework will allow you to use XML when passing parameters into a query, and also for returning results from them. In both cases, it probably does not add much value to use XML where you do not need to - using a plain old Java Object(POJO) instead is much more efficient in most cases.
XML parameters
Using XML to pass parameters into a mapped statement can be accomplished with either a STring value or a DOM object, both of which use the exact same structure. For example:
<parameter><accountId>3</accountId></parameter>
<select id="getByXmlId" resultClass="Account" parameterClass="xml">
select
accountId,
username,
password,
firstName,
lastName,
address1,
address2,
city,
state,
postalCode,
country
from Account
where accountId = #accountId#
</select>
Java Codes:
String parameter = "<parameter><accountId>3</accountId></parameter>";
Account account = (Account) sqlMapClient.queryForObject(
"Account.getByXmlId",
parameter);
Similarly, a DOM object can be passed into iBATIS to achieve the same results:
<select id="getByDomId" resultClass="Account" parameterClass="dom">
select
accountId,
username,
password,
firstName,
lastName,
address1,
address2,
city,
state,
postalCode,
country
from Account
where accountId = #accountId#
</select>
Document parameterDocument = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().newDocument();
Element paramElement = parameterDocument
.createElement("parameterDocument");
Element accountIdElement = parameterDocument
.createElement("accountId");
accountIdElement.setTextContent("3");
paramElement.appendChild(accountIdElement);
parameterDocument.appendChild(paramElement);
Account account = (Account) sqlMapClient.queryForObject(
"Account.getByXmlId", parameterDocument);
XML results
<select id="getByIdValueXml" resultClass="xml"
xmlResultName="account">
select
accountId,
username,
password
from Account
where accountId = #value#
</select>
String xmlData = (String) sqlMap.queryForObject(
"Account.getByIdValueXml",
new Integer(1));
XML parameters
Using XML to pass parameters into a mapped statement can be accomplished with either a STring value or a DOM object, both of which use the exact same structure. For example:
<parameter><accountId>3</accountId></parameter>
<select id="getByXmlId" resultClass="Account" parameterClass="xml">
select
accountId,
username,
password,
firstName,
lastName,
address1,
address2,
city,
state,
postalCode,
country
from Account
where accountId = #accountId#
</select>
Java Codes:
String parameter = "<parameter><accountId>3</accountId></parameter>";
Account account = (Account) sqlMapClient.queryForObject(
"Account.getByXmlId",
parameter);
Similarly, a DOM object can be passed into iBATIS to achieve the same results:
<select id="getByDomId" resultClass="Account" parameterClass="dom">
select
accountId,
username,
password,
firstName,
lastName,
address1,
address2,
city,
state,
postalCode,
country
from Account
where accountId = #accountId#
</select>
Document parameterDocument = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().newDocument();
Element paramElement = parameterDocument
.createElement("parameterDocument");
Element accountIdElement = parameterDocument
.createElement("accountId");
accountIdElement.setTextContent("3");
paramElement.appendChild(accountIdElement);
parameterDocument.appendChild(paramElement);
Account account = (Account) sqlMapClient.queryForObject(
"Account.getByXmlId", parameterDocument);
XML results
<select id="getByIdValueXml" resultClass="xml"
xmlResultName="account">
select
accountId,
username,
password
from Account
where accountId = #value#
</select>
String xmlData = (String) sqlMap.queryForObject(
"Account.getByIdValueXml",
new Integer(1));
Comments
Post a Comment