need to map them all individually. (Though many developers consider it a good practice to always
specify the columns expected.)
For more about Result Maps, see Section 3.4.
3.2.4.5. resultClass
If a resultMap is not specified, you may specify a resultClass instead. The value of the resultClass attribute can
be a Type Alias or the fully qualified name of a class. The class specified will be automatically mapped to the
columns in the result, based on the result metadata. Example 14 shows a <statement> element with a resultClass
attribute.
Example 14. A <statement >element with resultClass attribute
<statement id="selectPerson" parameterClass=”int” resultClass="person">
SELECT
ER_ID as id,
PER_FIRST_NAME as firstName,
PER_LAST_NAME as lastName,
PER_BIRTH_DATE as birthDate,
PER_WEIGHT_KG as weightInKilograms,
PER_HEIGHT_M as heightInMeters
FROM PERSON
WHERE PER_ID = #value#
</statement>
In Example 14, the Person class has properties including: id, firstName, lastName, birthDate, weightInKilo-
grams and heightInMeters. Each of these corresponds with the column aliases described by the SQL select state-
ment (using the “as” keyword –a standard SQL feature). When executed, a Person object is instantiated and
populated by matching the object property names to the (aliased) column names from the query.
Using SQL aliases to map columns to properties saves defining a <parameterMap> element, but there are limita-
tions. There is no way to specify the types of the output columns (if necessary), there is no way to automatically
load related data (complex properties), and there is a slight performance consequence (from accessing the result
metadata). Architecturally, using aliases this way mixes "database logic" with "reporting logic", making the
query harder to read and maintain. You can overcome these limitations with an explicit Result Map (Section
3.4).
3.2.4.6. cacheModel
If you want to cache the result of a query, you can specify a Cache Model as part of the <statement> element.
Example 15 shows a <cacheModel> element and a corresponding <statement>.
Example 15. A <cacheModel> element with its corresponding <statement>
<cacheModel id="product-cache" implementation="LRU">
<flushInterval hours="24"/>
<flushOnExecute statement="insertProduct"/>
<flushOnExecute statement="updateProduct"/>
<flushOnExecute statement="deleteProduct"/>
<property name=”size” value=”1000” />
</cacheModel>
<statement id=”selectProductList” parameterClass=”int” cacheModel=”product-cache”>
select * from PRODUCT where PRD_CAT_ID = #value#
</statement>
In Example 15, a cache is defined for products that uses a LRU reference type and flushes every 24 hours or
whenever associated update statements are executed. For more about Cache Models, see Section 3.6.
iBATIS Data Mapper Developer
Guide
13