| 1 |
lars |
1 |
<com:TContent ID="body">
|
|
|
2 |
|
|
|
3 |
<h1>Implicit Result Maps</h1>
|
|
|
4 |
<p>If the columns returned by a SQL statement match the result object, you may
|
|
|
5 |
not need an explicit Result Map. If you have control over the relational
|
|
|
6 |
schema, you might be able to name the columns so they also work as property
|
|
|
7 |
names. In the following example, the column names and property names
|
|
|
8 |
already match, so a result map is not needed.</p>
|
|
|
9 |
|
|
|
10 |
<com:TTextHighlighter Language="xml" CssClass="source">
|
|
|
11 |
<statement id="selectProduct" resultClass="Product">
|
|
|
12 |
select
|
|
|
13 |
id,
|
|
|
14 |
description
|
|
|
15 |
from PRODUCT
|
|
|
16 |
where id = #value#
|
|
|
17 |
</statement>
|
|
|
18 |
</com:TTextHighlighter>
|
|
|
19 |
|
|
|
20 |
<p>Another way to skip a result map is to use column aliasing to make the column
|
|
|
21 |
names match the properties names, as shown in the following example.</p>
|
|
|
22 |
|
|
|
23 |
<com:TTextHighlighter Language="xml" CssClass="source">
|
|
|
24 |
<statement id="selectProduct" resultClass="Product">
|
|
|
25 |
select
|
|
|
26 |
PRD_ID as id,
|
|
|
27 |
PRD_DESCRIPTION as description
|
|
|
28 |
from PRODUCT
|
|
|
29 |
where PRD_ID = #value#
|
|
|
30 |
</statement>
|
|
|
31 |
</com:TTextHighlighter>
|
|
|
32 |
|
|
|
33 |
<p>Of course, these techniques will not work if you need to specify a column
|
|
|
34 |
type, a null value, or any other property attributes.</p>
|
|
|
35 |
|
|
|
36 |
<h1>Primitive Results (i.e. String, Integer, Boolean)</h1>
|
|
|
37 |
<p>Many times, we don't need to return an object with multiple properties. We
|
|
|
38 |
just need a string, integer, boolean, and so forth. If you don't need to
|
|
|
39 |
populate an object, SQLMap can return one of the primitive types instead. If
|
|
|
40 |
you just need the value, you can use a primitive type as a result class, as
|
|
|
41 |
shown in following example.</p>
|
|
|
42 |
|
|
|
43 |
<com:TTextHighlighter Language="xml" CssClass="source">
|
|
|
44 |
<select id="selectProductCount" resultClass="integer">
|
|
|
45 |
select count(1)
|
|
|
46 |
from PRODUCT
|
|
|
47 |
</select>
|
|
|
48 |
</com:TTextHighlighter>
|
|
|
49 |
|
|
|
50 |
<com:TTextHighlighter Language="xml" CssClass="source">
|
|
|
51 |
<resultMap id="select-product-result" resultClass="string">
|
|
|
52 |
<result property="value" column="PRD_DESCRIPTION"/>
|
|
|
53 |
</resultMap>
|
|
|
54 |
</com:TTextHighlighter>
|
|
|
55 |
|
|
|
56 |
<h1>Maps with ResultMaps</h1>
|
|
|
57 |
<p>Instead of a rich object, sometimes all you might need is a simple key/value
|
|
|
58 |
list of the data, where each property is an entry on the list. If so, Result
|
|
|
59 |
Maps can populate an array instance as easily as property objects. The syntax
|
|
|
60 |
for using an array is identical to the rich object syntax. As shown in following example,
|
|
|
61 |
only the result object changes.</p>
|
|
|
62 |
|
|
|
63 |
<com:TTextHighlighter Language="xml" CssClass="source">
|
|
|
64 |
<resultMap id="select-product-result" class="array">
|
|
|
65 |
<result property="id" column="PRD_ID"/>
|
|
|
66 |
<result property="code" column="PRD_CODE"/>
|
|
|
67 |
<result property="description" column="PRD_DESCRIPTION"/>
|
|
|
68 |
<result property="suggestedPrice" column="PRD_SUGGESTED_PRICE"/>
|
|
|
69 |
</resultMap>
|
|
|
70 |
</com:TTextHighlighter>
|
|
|
71 |
|
|
|
72 |
<p>In the above example, an array instance would be created for each row
|
|
|
73 |
in the result set and populated with the Product data. The property name
|
|
|
74 |
attributes, like <tt>id</tt>, <tt>code</tt>, and so forth, would be the key of the
|
|
|
75 |
entry, and the value of the mapped columns would be the value of the entry.</p>
|
|
|
76 |
|
|
|
77 |
<p>As shown in the following example, you can also use an implicit Result
|
|
|
78 |
Map with an array type.</p>
|
|
|
79 |
|
|
|
80 |
<com:TTextHighlighter Language="xml" CssClass="source">
|
|
|
81 |
<statement id="selectProductCount" resultClass="array">
|
|
|
82 |
select * from PRODUCT
|
|
|
83 |
</statement>
|
|
|
84 |
</com:TTextHighlighter>
|
|
|
85 |
|
|
|
86 |
<p>What set of entries is returned by the above example depends on what
|
|
|
87 |
columns are in the result set. If the set of column changes (because columns
|
|
|
88 |
are added or removed), the new set of entries would automatically be returned.</p>
|
|
|
89 |
|
|
|
90 |
<div class="note"><b class="tip">Note:</b>
|
|
|
91 |
Certain providers may return column names in upper case or lower case format.
|
|
|
92 |
When accessing values with such a provider, you will have to pass the key name
|
|
|
93 |
in the expected case.
|
|
|
94 |
</div>
|
|
|
95 |
|
|
|
96 |
</com:TContent>
|