Java Reference
In-Depth Information
The queryForMap() methods
The queryForMap() methods return a Map (instead of a List) of one or more rows
from the database as Java objects. Just like the other query methods, it has two
forms as well:
Map queryForMap(String id, Object parameter, String key) throws SQLEx-
ception;
Map queryForMap(String id, Object parameter, String key, String value)
throws SQLException;
The first method will execute a query and return a Map of objects, where the key
to the objects is identified by the property named by the key parameter, and the
value objects are the complete objects from the mapped statement. The second
form will return a similar Map, but the objects will be the property of the objects
identified by the value parameter.
This is one of those times when an example is worth a thousand words. Let's
consider an example where you have a mapped statement that returns a set of
accounts. By using the first method, you could create a Map that had the accoun-
tId property as the key to the map and the full account bean as the value. Using
the second method, you could create a Map that had the accountId property as
the key to the map, and only the account name as the value:
Map accountMap = sqlMap.queryForMap(
"Account.getAll",
null,
"accountId");
System.out.println(accountMap);
accountMap = sqlMap.queryForMap(
"Account.getAll",
null,
"accountId",
"username");
System.out.println(accountMap);
Now that you have seen all the portions of the API that you need to start using
i BATIS , let's take a look at the different ways in which you can create mapped
statements.
4.1.3
Mapped statement types
Before you can use the SqlMap API that you just learned about, you need to know
how to create mapped statements to make it work. In the previous example, we
called a mapped statement named Account.getAll , but we did not talk about
Search WWH ::




Custom Search