Blame | Letzte Änderung | Log anzeigen | RSS feed
<com:TContent ID="body"><h1>Parameter Maps and Inline Parameters</h1><p>Most SQL statements are useful because we can pass them values at runtime.Someone wants a database record with the ID 42, and we need to merge that IDnumber into a select statement. A list of one or more parameters are passed atruntime, and each placeholder is replaced in turn. This is simple but laborintensive, since developers spend a lot of time counting symbols to make sureeverything is in sync.</p><div class="note"><b class="tip">Note:</b>Preceding sections briefly touched on inline parameters, which automaticallymap properties to named parameters. Many iBATIS developers prefer thisapproach. But others prefer to stick to the standard, anonymous approach toSQL parameters by using parameter maps. Sometimes people need to retain thepurity of the SQL statements; other times they need the detailed specificationoffered by parameter maps due to database or provider-specific informationthat needs to be used.</div><h1>Parameter Map</h1><p>A Parameter Map defines an ordered list of values that match up with theplaceholders of a parameterized query statement. While the attributesspecified by the map still need to be in the correct order, each parameter isnamed. You can populate the underlying class in any order, and the ParameterMap ensures each value is passed in the correct order.</p><p>Parameter Maps can be provided as an external element and \emph{inline}.The following example shows an external Parameter Map.</p><com:TTextHighlighter Language="xml" CssClass="source"><parameterMap id="parameterMapIdentifier"[extends="[sqlMapNamespace.]parameterMapId"]><parameterproperty ="propertyName"[column="columnName"][dbType="databaseType"][type="propertyCLRType"][nullValue="nullValueReplacement"][size="columnSize"][precision="columnPrecision"][scale="columnScale"][typeHandler="class.name"]<parameter ... ... /><parameter ... ... /></parameterMap></com:TTextHighlighter><p>In the above example, the parts in <tt>[brackets]</tt> are optional. The<tt>parameterMap</tt> element only requires the id attribute.The following example shows a typical <tt><parameterMap></tt>.</p><com:TTextHighlighter Language="xml" CssClass="source"><parameterMap id="insert-product-param" class="Product"><parameter property="description" /><parameter property="id"/></parameterMap><statement id="insertProduct" parameterMap="insert-product-param">insert into PRODUCT (PRD_DESCRIPTION, PRD_ID) values (?,?);</statement></com:TTextHighlighter><div class="note"><b class="tip">Note:</b>Parameter Map names are always local to the Data Map definition file wherethey are defined. You can refer to a Parameter Map in another Data Mapdefinition file by prefixing the <tt>id</tt> of the Parameter Map with thenamespace of the Data Map (set in the <tt><sqlMap></tt> root element).</div><h2><tt><parameterMap></tt> attributes</h2><p>The <tt><parameterMap></tt> elementaccepts two attributes: <tt>id</tt> (required) and <tt>extends</tt> (optional).</p><h3><tt>id</tt> attribute</h3><p>The required <tt>id</tt> attribute provides aunique identifier for the <tt><parameterMap></tt> within this Data Map.</p><h3><tt>extends</tt> attribute</h3><p>The optional <tt>extends</tt> attribute can be set to the name of another<tt>parameterMap</tt> upon which to base this <tt>parameterMap</tt>. All properties ofthe super <tt>parameterMap</tt> will be included as part of this<tt>parameterMap</tt>, and values from the super <tt>parameterMap</tt> are set beforeany values specified by this <tt>parameterMap</tt>. The effect is similar toextending a class.</p><h1><tt><parameter></tt> Elements</h1><p>The <tt><parameterMap></tt> element holds one or more parameter child elementsthat map object properties to placeholders in a SQL statement. The sectionsthat follow describe each of the attributes.</p><h2><tt>property</tt> attribute</h2><p>The <tt>property</tt> attribute of <tt><parameter></tt> is the name of a property ofthe parameter object. It may also be the name of an entry in an array. Thename can be used more than once depending on the number of times it is neededin the statement. (In an update, you might set a column that is also part ofthe where clause.)</p><h2><tt>direction</tt> attribute</h2><p>The <tt>direction</tt> attribute may be used to indicate a stored procedureparameter's direction.</p><!-- tabular: align=|l|l|, width=(0.2 0.4) --><table class="tabular"><tr><th>Value</th><th>Description</th></tr><tr><td><tt>Input</tt></td><td>input-only</td></tr><tr><td><tt>Output</tt></td><td>output-only</td></tr><tr><td><tt>InputOutput</tt></td><td>bidirectional</td></tr></table><h2><tt>column</tt> attribute</h2><p>The <tt>column</tt> attribute is used to define to the name of a parameter used bya stored procedure.</p><h2><tt>dbType</tt> attribute</h2><p>The <tt>dbType</tt> attribute is used to explicitly specify the database columntype of the parameter to be set by this property. This attribute is normallyonly required if the column is nullable. Although, another reason to use the<tt>dbType</tt> attribute is to explicitly specify date types. Most SQL databaseshave more than one <tt>datetime</tt> type. Usually, a database has at least threedifferent types (DATE, DATETIME, TIMESTAMP). In order for the value to mapcorrectly, you might need to specify the column's <tt>dbType</tt>.</p><div class="note"><b class="tip">Note:</b>Most providers only need the <tt>dbType</tt> specified for nullable columns. Inthis case, you only need to specify the type for the columns that arenullable.</div><h2><tt>type</tt> attribute</h2><p>The <tt>type</tt> attribute is used to specify the type of the parameter'sproperty. This attribute is useful when passing <tt>InputOutput</tt> and<tt>Output</tt> parameters into stored procedures. The framework uses thespecified type to properly handle and set the parameter object's propertieswith the procedure's output values after execution.</p><h2><tt>nullValue</tt> attribute</h2><p>The <tt>nullValue</tt> attribute can be set to any valid value (based on propertytype). The <tt>nullValue</tt> attribute is used to specify an outgoing null valuereplacement. What this means is that when the value is detected in the objectproperty, a NULL will be written to the database (the opposite behavior of aninbound null value replacement). This allows you to use a magic null number inyour application for types that do not support null values (such as int,double, float). When these types of properties contain a matching null value(for example, say, -9999), a NULL will be written to the database instead ofthe value.</p><div class="tip"><b class="tip">Tip:</b>For round-trip transparency of null values, you must also specify databasecolumns null value replacements in your <a href="?page=Manual.ResultMaps">Result Map</a>.</div><h2><tt>size</tt> attribute</h2><p>The <tt>size</tt> attribute sets the maximum size of the data within the column.</p><h2><tt>precision</tt> attribute</h2><p>The <tt>precision</tt> attribute is used to set the maximum number of digits usedto represent the property value.</p><h2><tt>scale</tt> attribute</h2><p>The <tt>scale</tt> attribute sets the number of decimal places used to resolve theproperty value.</p><h2><tt>typeHandler</tt> attribute</h2><p>The <tt>typeHandler</tt> attribute allows the use of a<a href="?page=Manual.CustomTypeHandlers">Custom Type Handler</a>. This allows you to extend the DataMapper'scapabilities in handling types that are specific to your database provider,are not handled by your database provider, or just happen to be a part of yourapplication design. You can create custom type handlers to deal with storingand retrieving booleans from your database for example.</p></com:TContent>