| 1 |
lars |
1 |
<com:TContent ID="body">
|
|
|
2 |
|
|
|
3 |
<h1>DataMapper Configuration Elements</h1>
|
|
|
4 |
|
|
|
5 |
<p>Sometimes the values we use in an XML configuration file occur in more than
|
|
|
6 |
one element. Often, there are values that change when we move the application
|
|
|
7 |
from one server to another. To help you manage configuration values, you can
|
|
|
8 |
specify a standard properties file (with name=value entries) as part of a
|
|
|
9 |
DataMapper configuration. Each named value in the properties file becomes a
|
|
|
10 |
shell variable that can be used in the DataMapper configuration file and your
|
|
|
11 |
Data Map definition files.
|
|
|
12 |
</p>
|
|
|
13 |
|
|
|
14 |
<h2><tt><properties></tt> attributes</h2>
|
|
|
15 |
<p>The <tt><properties></tt> element can accept one <tt>resource</tt> attribute to
|
|
|
16 |
specify the location of the properties file.</p>
|
|
|
17 |
|
|
|
18 |
<!-- tabular: align=|l|l|, width=(0.2 0.7) -->
|
|
|
19 |
<table class="tabular">
|
|
|
20 |
<tr>
|
|
|
21 |
<th>Attribute</th>
|
|
|
22 |
<th>Description</th>
|
|
|
23 |
</tr>
|
|
|
24 |
<tr>
|
|
|
25 |
<td><tt>resource</tt></td>
|
|
|
26 |
<td>Specify the properties file to be loaded from the directory relative
|
|
|
27 |
to the current file.
|
|
|
28 |
Example: <tt>resource="properties.config"</tt>
|
|
|
29 |
</td>
|
|
|
30 |
</tr>
|
|
|
31 |
</table>
|
|
|
32 |
<p>
|
|
|
33 |
For example, if the "<tt>properties.config</tt>" file contains</p>
|
|
|
34 |
<com:TTextHighlighter Language="xml" CssClass="source">
|
|
|
35 |
<?xml version="1.0" encoding="utf-8" ?>
|
|
|
36 |
<settings>
|
|
|
37 |
<add key="username" value="albert" />
|
|
|
38 |
</settings>
|
|
|
39 |
</com:TTextHighlighter>
|
|
|
40 |
|
|
|
41 |
<p>then all elements in the DataMapper configuration can use the variable
|
|
|
42 |
<tt>${username}</tt> to insert the value "albert". For example:</p>
|
|
|
43 |
|
|
|
44 |
<com:TTextHighlighter Language="xml" CssClass="source">
|
|
|
45 |
<provider ConnectionString="mysql://${username}:..." ... />
|
|
|
46 |
</com:TTextHighlighter>
|
|
|
47 |
|
|
|
48 |
<div class="tip"><b class="tip">Tip:</b>
|
|
|
49 |
Properties are handy during building, testing, and deployment by making it
|
|
|
50 |
easy to reconfigure your application for multiple environments.
|
|
|
51 |
</div>
|
|
|
52 |
|
|
|
53 |
<h2><tt><property></tt> element and attributes</h2>
|
|
|
54 |
<p>You can also specify more than one properties file or add property keys and
|
|
|
55 |
values directly into your <tt>SqlMap.config</tt> file by using <tt><property></tt>
|
|
|
56 |
elements.</p>
|
|
|
57 |
|
|
|
58 |
<!-- tabular: align=|l|l|, width=(0.2 0.7) -->
|
|
|
59 |
<table class="tabular">
|
|
|
60 |
<tr>
|
|
|
61 |
<th>Attribute</th>
|
|
|
62 |
<th>Description</th>
|
|
|
63 |
</tr>
|
|
|
64 |
<tr>
|
|
|
65 |
<td><tt>resource</tt></td>
|
|
|
66 |
<td>Specify the properties file to be loaded from the directory relative
|
|
|
67 |
to the current file.
|
|
|
68 |
Example: <tt>resource="properties.config"</tt>
|
|
|
69 |
</td>
|
|
|
70 |
</tr>
|
|
|
71 |
<tr>
|
|
|
72 |
<td><tt>key</tt></td>
|
|
|
73 |
<td>Defines a property key (variable) name.
|
|
|
74 |
Example: <tt>key="username"</tt>
|
|
|
75 |
</td>
|
|
|
76 |
</tr>
|
|
|
77 |
<tr>
|
|
|
78 |
<td><tt>value</tt></td>
|
|
|
79 |
<td> Defines a value that will be used by the DataMapper in place of the
|
|
|
80 |
the specified property key/variable.
|
|
|
81 |
Example: <tt>value="mydbuser"</tt>
|
|
|
82 |
</td>
|
|
|
83 |
</tr>
|
|
|
84 |
</table>
|
|
|
85 |
<p>For example:</p>
|
|
|
86 |
|
|
|
87 |
<com:TTextHighlighter Language="xml" CssClass="source">
|
|
|
88 |
<properties>
|
|
|
89 |
<property resource="myProperties.config"/>
|
|
|
90 |
<property resource="anotherProperties.config"/>
|
|
|
91 |
<property key="host" value="ibatis.com" />
|
|
|
92 |
</properties>
|
|
|
93 |
</com:TTextHighlighter>
|
|
|
94 |
|
|
|
95 |
<h2>The <tt><typeHandler></tt> Element</h2>
|
|
|
96 |
<p>The <tt><typeHandler></tt> element allows for the configuration and use of a
|
|
|
97 |
Custom Type Handler (see the Custom Type Handler section). This extends the
|
|
|
98 |
DataMapper's capabilities in handling types that are specific to your database
|
|
|
99 |
provider, are not handled by your database provider, or just happen to be a
|
|
|
100 |
part of your application design.</p>
|
|
|
101 |
|
|
|
102 |
<com:TTextHighlighter Language="xml" CssClass="source">
|
|
|
103 |
<typeHandler type="date" callback="TDateTimeHandler"/>
|
|
|
104 |
</com:TTextHighlighter>
|
|
|
105 |
|
|
|
106 |
<p>The <tt><typeHandler></tt> element has three attributes:</p>
|
|
|
107 |
<!-- tabular: align=|l|l|, width=(0.2 0.7) -->
|
|
|
108 |
<table class="tabular">
|
|
|
109 |
<tr>
|
|
|
110 |
<th>Attribute</th>
|
|
|
111 |
<th>Description</th>
|
|
|
112 |
</tr>
|
|
|
113 |
<tr>
|
|
|
114 |
<td><tt>type</tt></td>
|
|
|
115 |
<td>Refers to the name of the type to handle.
|
|
|
116 |
Example: <tt>type="date"</tt>
|
|
|
117 |
</td>
|
|
|
118 |
</tr>
|
|
|
119 |
<tr>
|
|
|
120 |
<td><tt>dbType</tt></td>
|
|
|
121 |
<td>Indicates the provider dbType to handle.
|
|
|
122 |
Example: <tt>dbType="Varchar2"</tt>
|
|
|
123 |
</td>
|
|
|
124 |
</tr>
|
|
|
125 |
<tr>
|
|
|
126 |
<td><tt>callback</tt></td>
|
|
|
127 |
<td> The custom type handler class name.
|
|
|
128 |
Example: <tt>callback="TDateTimeHandler"</tt>
|
|
|
129 |
</td>
|
|
|
130 |
</tr>
|
|
|
131 |
</table>
|
|
|
132 |
|
|
|
133 |
<h2>The <tt><provider></tt> element and attribute</h2>
|
|
|
134 |
|
|
|
135 |
<p>The <tt><provider></tt> element encloses a <tt><datasource></tt> that configure the
|
|
|
136 |
database system for use by the framework.</p>
|
|
|
137 |
|
|
|
138 |
<!-- tabular: align=|l|l|, width=(0.2 0.7) -->
|
|
|
139 |
<table class="tabular">
|
|
|
140 |
<tr>
|
|
|
141 |
<th>Attribute</th>
|
|
|
142 |
<th>Description</th>
|
|
|
143 |
</tr>
|
|
|
144 |
<tr>
|
|
|
145 |
<td><tt>class</tt></td>
|
|
|
146 |
<td>The database provider class that extends
|
|
|
147 |
<tt>TDatabaseProvider</tt>.
|
|
|
148 |
Example: <tt>class="TAdodbProvider"</tt>
|
|
|
149 |
</td>
|
|
|
150 |
</tr>
|
|
|
151 |
</table>
|
|
|
152 |
|
|
|
153 |
<h2>The <tt><datasource></tt> element and attributes</h2>
|
|
|
154 |
<p>The <tt><datasource></tt> element specifies the connection string.
|
|
|
155 |
The following example shows a <tt><datasource></tt> element for a MySql connection.</p>
|
|
|
156 |
|
|
|
157 |
<com:TTextHighlighter Language="xml" CssClass="source">
|
|
|
158 |
<!-- The ${properties} are defined in an external file, -->
|
|
|
159 |
<!-- but the values could also be coded inline. -->
|
|
|
160 |
|
|
|
161 |
<!-- Connecting to a MySQL database -->
|
|
|
162 |
<provider class="TAdodbProvider" >
|
|
|
163 |
<datasource
|
|
|
164 |
ConnectionString="mysql://${username}:${password}@${host}/${database}" />
|
|
|
165 |
</provider>
|
|
|
166 |
</com:TTextHighlighter>
|
|
|
167 |
|
|
|
168 |
<!-- tabular: align=|l|l|, width=(0.2 0.7) -->
|
|
|
169 |
<table class="tabular">
|
|
|
170 |
<tr>
|
|
|
171 |
<th>Attribute</th>
|
|
|
172 |
<th>Description</th>
|
|
|
173 |
</tr>
|
|
|
174 |
<tr>
|
|
|
175 |
<td><tt>connectionString</tt></td>
|
|
|
176 |
<td>Data Source Name (DSN) connection string.
|
|
|
177 |
Example: <tt>connectionString="mysql://root:pwd@localhost/mydb"</tt>
|
|
|
178 |
</td>
|
|
|
179 |
</tr>
|
|
|
180 |
<tr>
|
|
|
181 |
<td><tt>driver</tt></td>
|
|
|
182 |
<td> Database driver name (mysql, sqlite, etc.).
|
|
|
183 |
Example: <tt>driver="mysql"</tt>
|
|
|
184 |
</td>
|
|
|
185 |
</tr>
|
|
|
186 |
<tr>
|
|
|
187 |
<td><tt>host</tt></td>
|
|
|
188 |
<td>DB host name/IP (and port number) in the format <tt>host[:port]</tt>.
|
|
|
189 |
Example: <tt>host="localhost"</tt>
|
|
|
190 |
</td>
|
|
|
191 |
</tr>
|
|
|
192 |
<tr>
|
|
|
193 |
<td><tt>username</tt></td>
|
|
|
194 |
<td>Database connection username.</td>
|
|
|
195 |
</tr>
|
|
|
196 |
<tr>
|
|
|
197 |
<td><tt>password</tt></td>
|
|
|
198 |
<td>Database connection password.</td>
|
|
|
199 |
</tr>
|
|
|
200 |
<tr>
|
|
|
201 |
<td><tt>database</tt></td>
|
|
|
202 |
<td>Database name to use in the connection.</td>
|
|
|
203 |
</tr>
|
|
|
204 |
</table>
|
|
|
205 |
|
|
|
206 |
<div class="tip"><b class="tip">Tip:</b>
|
|
|
207 |
Use Data Source Name (DSN) connection string or specify the
|
|
|
208 |
necessary individual connection parameters.
|
|
|
209 |
</div>
|
|
|
210 |
|
|
|
211 |
<h2>The <tt><sqlMap></tt> Element</h2>
|
|
|
212 |
<p>On a daily basis, most of your work will be with the Data Maps, which are
|
|
|
213 |
covered in <a href="?page=Manual.WorkingWithDataMaps">Working with Data Maps</a>.
|
|
|
214 |
The Data Maps define the actual SQL
|
|
|
215 |
statements or stored procedures used by your application. The parameter and
|
|
|
216 |
result objects are also defined as part of the Data Map. As your application
|
|
|
217 |
grows, you may have several varieties of Data Map. To help you keep your Data
|
|
|
218 |
Maps organized, you can create any number of Data Map definition files and
|
|
|
219 |
incorporate them by reference in the DataMapper configuration. All of the
|
|
|
220 |
definition files used by a DataMapper instance must be listed in the
|
|
|
221 |
configuration file.</p>
|
|
|
222 |
|
|
|
223 |
The following example shows <tt><sqlMap></tt> elements for loading a set of
|
|
|
224 |
<a href="?page=Manual.WorkingWithDataMaps">Data Map definitions</a>.
|
|
|
225 |
|
|
|
226 |
<com:TTextHighlighter Language="xml" CssClass="source">
|
|
|
227 |
<!-- Relative path from the directory of the
|
|
|
228 |
current file using a property variable -->
|
|
|
229 |
<sqlMap resource="${root}/Maps/Account.xml"/>
|
|
|
230 |
<sqlMap resource="${root}/Maps/Category.xml"/>
|
|
|
231 |
<sqlMap resource="${root}/Maps/Product.xml"/>
|
|
|
232 |
|
|
|
233 |
<!-- Full file path with a property variable -->
|
|
|
234 |
<sqlMap resource="/${projectdir}/MyApp/Maps/Account.xml"/>
|
|
|
235 |
<sqlMap resource="/${projectdir}/MyApp/Maps/Category.xml"/>
|
|
|
236 |
<sqlMap resource="/${projectdir}/MyApp/Maps/Product.xml"/>
|
|
|
237 |
</com:TTextHighlighter>
|
|
|
238 |
|
|
|
239 |
<div class="tip"><b class="tip">Tip:</b>
|
|
|
240 |
Since the application root directory location differs by project type
|
|
|
241 |
(Windows, Web, or library), it is best to use a properties variable to
|
|
|
242 |
indicate the relative path when using the <tt><sqlMap></tt> <tt>resource</tt>
|
|
|
243 |
attribute. Having a variable defined in a properties file makes it easy to
|
|
|
244 |
change the path to all your Data Mapper configuration resources in one
|
|
|
245 |
location (note the <tt>${projectdir}</tt> and <tt>${root}</tt> variables in the
|
|
|
246 |
example above).
|
|
|
247 |
</div>
|
|
|
248 |
|
|
|
249 |
</com:TContent>
|