Java Reference
In-Depth Information
If we had used a bean for the results, that bean could have an int property to
receive the results, as in the following example:
public class PrimitiveResult {
private int orderCount;
public int getOrderCount() {
return orderCount;
}
public void setOrderCount(int orderCount) {
this.orderCount = orderCount;
}
}
<resultMap id="primitiveResultMapExample"
class="PrimitiveResult">
<result property="orderCount"
column="orderCount" />
</resultMap>
<select id="getPrimitiveById"
resultMap="primitiveResultMapExample">
select count(*) as orderCount
from order
where accountId = #accountId#
</select>
So, the bottom line is this: i BATIS can map results into any type, but because it
only returns Object instances, primitive values must be wrapped in one of the fol-
lowing: a simple value wrapper, a bean, or a Map.
Again, if you are able to use J2SE 5 , this is not completely true. Methods
such as queryForObject() still return an instance of Object. You cannot
cast Object directly to a primitive (such as int or long), but you can cast it
to the boxed type (Integer or Long), then let the compiler unbox that
into the primitive type. In this case, however, there is a caveat: if the
returned Object is null, the value of the boxed type will also be null.
When that situation arises, the application will throw a NullPointerEx-
ception because the unboxed type cannot be null.
NOTE
Sometimes (most of the time, actually) you will want to get more than just a single
column from your SQL statement. In those cases, you will want to put your results
into a JavaBean or Map.
4.4.2
JavaBean and Map results
The i BATIS framework will allow you to use either Map or bean objects for result
mapping (in addition to the primitive wrapper classes— Integer , Long , etc.). Both
Search WWH ::




Custom Search