Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<com:TContent ID="body">
2
<h1>Parameter Maps and Inline Parameters</h1>
3
 
4
<p>Most SQL statements are useful because we can pass them values at runtime.
5
Someone wants a database record with the ID 42, and we need to merge that ID
6
number into a select statement. A list of one or more parameters are passed at
7
runtime, and each placeholder is replaced in turn. This is simple but labor
8
intensive, since developers spend a lot of time counting symbols to make sure
9
everything is in sync.</p>
10
 
11
<div class="note"><b class="tip">Note:</b>
12
Preceding sections briefly touched on inline parameters, which automatically
13
map properties to named parameters. Many iBATIS developers prefer this
14
approach. But others prefer to stick to the standard, anonymous approach to
15
SQL parameters by using parameter maps. Sometimes people need to retain the
16
purity of the SQL statements; other times they need the detailed specification
17
offered by parameter maps due to database or provider-specific information
18
that needs to be used.
19
</div>
20
 
21
<h1>Parameter Map</h1>
22
<p>A Parameter Map defines an ordered list of values that match up with the
23
placeholders of a parameterized query statement. While the attributes
24
specified by the map still need to be in the correct order, each parameter is
25
named. You can populate the underlying class in any order, and the Parameter
26
Map ensures each value is passed in the correct order.</p>
27
 
28
<p>Parameter Maps can be provided as an external element and \emph{inline}.
29
The following example shows an external Parameter Map.</p>
30
 
31
<com:TTextHighlighter Language="xml" CssClass="source">
32
<parameterMap id="parameterMapIdentifier"
33
  [extends="[sqlMapNamespace.]parameterMapId"]>
34
  <parameter
35
    property ="propertyName"
36
    [column="columnName"]
37
    [dbType="databaseType"]
38
    [type="propertyCLRType"]
39
    [nullValue="nullValueReplacement"]
40
    [size="columnSize"]
41
    [precision="columnPrecision"]
42
    [scale="columnScale"]
43
    [typeHandler="class.name"]
44
  <parameter ... ... />
45
  <parameter ... ... />
46
</parameterMap>
47
</com:TTextHighlighter>
48
 
49
<p>In the above example, the parts in <tt>[brackets]</tt> are optional. The
50
<tt>parameterMap</tt> element only requires the id attribute.
51
The following example shows a typical <tt>&lt;parameterMap&gt;</tt>.</p>
52
 
53
<com:TTextHighlighter Language="xml" CssClass="source">
54
<parameterMap id="insert-product-param" class="Product">
55
  <parameter property="description" />
56
  <parameter property="id"/>
57
</parameterMap>
58
 
59
<statement id="insertProduct" parameterMap="insert-product-param">
60
  insert into PRODUCT (PRD_DESCRIPTION, PRD_ID) values (?,?);
61
</statement>
62
</com:TTextHighlighter>
63
 
64
<div class="note"><b class="tip">Note:</b>
65
Parameter Map names are always local to the Data Map definition file where
66
they are defined. You can refer to a Parameter Map in another Data Map
67
definition file by prefixing the <tt>id</tt> of the Parameter Map with the
68
namespace of the Data Map (set in the <tt>&lt;sqlMap&gt;</tt> root element).
69
</div>
70
 
71
<h2><tt>&lt;parameterMap&gt;</tt> attributes</h2>
72
 
73
<p>The <tt>&lt;parameterMap&gt;</tt> element
74
accepts two attributes: <tt>id</tt> (required) and <tt>extends</tt> (optional).</p>
75
 
76
<h3><tt>id</tt> attribute</h3>
77
 
78
<p>The required <tt>id</tt> attribute provides a
79
unique identifier for the <tt>&lt;parameterMap&gt;</tt> within this Data Map.</p>
80
 
81
<h3><tt>extends</tt> attribute</h3>
82
<p>The optional <tt>extends</tt> attribute can be set to the name of another
83
<tt>parameterMap</tt> upon which to base this <tt>parameterMap</tt>. All properties of
84
the super <tt>parameterMap</tt> will be included as part of this
85
<tt>parameterMap</tt>, and values from the super <tt>parameterMap</tt> are set before
86
any values specified by this <tt>parameterMap</tt>. The effect is similar to
87
extending a class.</p>
88
 
89
<h1><tt>&lt;parameter&gt;</tt> Elements</h1>
90
<p>The <tt>&lt;parameterMap&gt;</tt> element holds one or more parameter child elements
91
that map object properties to placeholders in a SQL statement. The sections
92
that follow describe each of the attributes.</p>
93
 
94
<h2><tt>property</tt> attribute</h2>
95
<p>The <tt>property</tt> attribute of <tt>&lt;parameter&gt;</tt> is the name of a property of
96
the parameter object. It may also be the name of an entry in an array. The
97
name can be used more than once depending on the number of times it is needed
98
in the statement. (In an update, you might set a column that is also part of
99
the where clause.)</p>
100
 
101
<h2><tt>direction</tt> attribute</h2>
102
<p>The <tt>direction</tt> attribute may be used to indicate a stored procedure
103
parameter's direction.</p>
104
 
105
<!-- tabular: align=|l|l|, width=(0.2 0.4) -->
106
<table class="tabular">
107
	<tr><th>Value</th><th>Description</th></tr>
108
	<tr>
109
		<td><tt>Input</tt></td>
110
		<td>input-only</td>
111
	</tr>
112
	<tr>
113
		<td><tt>Output</tt></td>
114
		<td>output-only</td>
115
	</tr>
116
	<tr>
117
		<td><tt>InputOutput</tt></td>
118
		<td>bidirectional</td>
119
	</tr>
120
</table>
121
 
122
<h2><tt>column</tt> attribute</h2>
123
<p>The <tt>column</tt> attribute is used to define to the name of a parameter used by
124
a stored procedure.</p>
125
 
126
<h2><tt>dbType</tt> attribute</h2>
127
<p>The <tt>dbType</tt> attribute is used to explicitly specify the database column
128
type of the parameter to be set by this property. This attribute is normally
129
only required if the column is nullable. Although, another reason to use the
130
<tt>dbType</tt> attribute is to explicitly specify date types. Most SQL databases
131
have more than one <tt>datetime</tt> type. Usually, a database has at least three
132
different types (DATE, DATETIME, TIMESTAMP). In order for the value to map
133
correctly, you might need to specify the column's <tt>dbType</tt>.</p>
134
 
135
<div class="note"><b class="tip">Note:</b>
136
Most providers only need the <tt>dbType</tt> specified for nullable columns. In
137
this case, you only need to specify the type for the columns that are
138
nullable.
139
</div>
140
 
141
<h2><tt>type</tt> attribute</h2>
142
<p>The <tt>type</tt> attribute is used to specify the type of the parameter's
143
property. This attribute is useful when passing <tt>InputOutput</tt> and
144
<tt>Output</tt> parameters into stored procedures. The framework uses the
145
specified type to properly handle and set the parameter object's properties
146
with the procedure's output values after execution.</p>
147
 
148
<h2><tt>nullValue</tt> attribute</h2>
149
 
150
<p>The <tt>nullValue</tt> attribute can be set to any valid value (based on property
151
type). The <tt>nullValue</tt> attribute is used to specify an outgoing null value
152
replacement. What this means is that when the value is detected in the object
153
property, a NULL will be written to the database (the opposite behavior of an
154
inbound null value replacement). This allows you to use a magic null number in
155
your application for types that do not support null values (such as int,
156
double, float). When these types of properties contain a matching null value
157
(for example, say, -9999), a NULL will be written to the database instead of
158
the value.</p>
159
 
160
<div class="tip"><b class="tip">Tip:</b>
161
For round-trip transparency of null values, you must also specify database
162
columns null value replacements in your <a href="?page=Manual.ResultMaps">Result Map</a>.
163
</div>
164
 
165
<h2><tt>size</tt> attribute</h2>
166
<p>The <tt>size</tt> attribute sets the maximum size of the data within the column.</p>
167
 
168
<h2><tt>precision</tt> attribute</h2>
169
<p>The <tt>precision</tt> attribute is used to set the maximum number of digits used
170
to represent the property value.</p>
171
 
172
<h2><tt>scale</tt> attribute</h2>
173
<p>The <tt>scale</tt> attribute sets the number of decimal places used to resolve the
174
property value.</p>
175
 
176
<h2><tt>typeHandler</tt> attribute</h2>
177
<p>The <tt>typeHandler</tt> attribute allows the use of a
178
<a href="?page=Manual.CustomTypeHandlers">Custom Type Handler</a>. This allows you to extend the DataMapper's
179
capabilities in handling types that are specific to your database provider,
180
are not handled by your database provider, or just happen to be a part of your
181
application design. You can create custom type handlers to deal with storing
182
and retrieving booleans from your database for example.</p>
183
 
184
</com:TContent>