-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathindex.html
508 lines (396 loc) · 18.6 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
<!DOCTYPE html>
<!--
| Generated by Apache Maven Doxia Site Renderer 2.0.0 from src/site/xdoc/index.xml.vm at 21 Apr 2025
| Rendered using Apache Maven Fluido Skin 2.0.1
-->
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="generator" content="Apache Maven Doxia Site Renderer 2.0.0" />
<meta name="author" content="The MyBatis Team" />
<title>MyBatis FreeMarker – MyBatis FreeMarker</title>
<link rel="stylesheet" href="./css/apache-maven-fluido-2.0.1.min.css" />
<link rel="stylesheet" href="./css/site.css" />
<link rel="stylesheet" href="./css/print.css" media="print" />
<script src="./js/apache-maven-fluido-2.0.1.min.js"></script>
</head>
<body>
<div class="container-fluid container-fluid-top">
<header>
<div id="banner">
<div class="pull-left"></div>
<div class="pull-right"><div id="bannerRight"><h1><a href="https://mybatis.github.io/mybatis-freemarker/"><img class="class java.lang.Object" src="http://mybatis.github.com/images/mybatis-logo.png" /></a></h1></div></div>
<div class="clear"><hr/></div>
</div>
<div id="breadcrumbs">
<ul class="breadcrumb">
<li id="publishDate">Last Published: 15 Sep 2024<span class="divider">|</span>
</li>
<li id="projectVersion">Version: 1.3.1-SNAPSHOT</li>
</ul>
</div>
</header>
<div class="row-fluid">
<header id="leftColumn" class="span2">
<nav class="well sidebar-nav">
<ul class="nav nav-list">
<li class="nav-header">Documentation</li>
<li class="active"><a>Getting started</a></li>
<li class="nav-header">Project Documentation</li>
<li><a href="project-info.html"><span class="icon-chevron-down"></span>Project Information</a>
<ul class="nav nav-list">
<li><a href="ci-management.html">CI Management</a></li>
<li><a href="dependencies.html">Dependencies</a></li>
<li><a href="dependency-info.html">Maven Coordinates</a></li>
<li><a href="distribution-management.html">Distribution Management</a></li>
<li class="active"><a>About</a></li>
<li><a href="issue-management.html">Issue Management</a></li>
<li><a href="licenses.html">Licenses</a></li>
<li><a href="mailing-lists.html">Mailing Lists</a></li>
<li><a href="plugin-management.html">Plugin Management</a></li>
<li><a href="plugins.html">Plugins</a></li>
<li><a href="scm.html">Source Code Management</a></li>
<li><a href="summary.html">Summary</a></li>
<li><a href="team.html">Team</a></li>
</ul></li>
<li><a href="project-reports.html"><span class="icon-chevron-right"></span>Project Reports</a></li>
</ul>
</nav>
<div class="well sidebar-nav">
<div id="poweredBy">
<div class="clear"></div>
<div class="clear"></div>
<a href="https://maven.apache.org/" class="builtBy" target="_blank"><img class="builtBy" alt="Built by Maven" src="./images/logos/maven-feather.png" /></a>
</div>
</div>
</header>
<main id="bodyColumn" class="span10">
<section><a id="Introduction"></a>
<h1>Introduction</h1>
<p>
The mybatis-freemarker is a plugin that helps creating big dynamic SQL queries. You can use it selectively, to only queries that need if statmenets or foreach-loops, for example. But it is possible to use this syntax by default too.
</p>
<p>
If you are not familiar with FreeMarker syntax, you can view
</p>
<ul>
<li>
<a href="http://freemarker.org/docs/ref.html" class="externalLink">Template Language Reference</a>
</li>
</ul>
</section>
<section><a id="Install"></a>
<h1>Install</h1>
<p>
If you are using maven, you can add this:
</p>
<pre class="prettyprint"><code>
<dependency>
<groupId>org.mybatis.scripting</groupId>
<artifactId>mybatis-freemarker</artifactId>
<version>1.3.1-SNAPSHOT</version>
</dependency>
</code></pre>
<p>If you are using gradle, you can use this snippet:</p>
<pre class="prettyprint"><code>
dependencies {
compile("org.mybatis.scripting:mybatis-freemarker:1.3.1-SNAPSHOT")
}</code></pre>
</section>
<section><a id="Install_from_sources"></a>
<h1>Install from sources</h1>
<ul>
<li>Checkout the source code</li>
<li>Run <code>mvn install</code> to build and to automatically install it to your local maven repo</li>
<li>Add maven dependency to your project
<pre class="prettyprint"><code>
<dependency>
<groupId>org.mybatis.scripting</groupId>
<artifactId>mybatis-freemarker</artifactId>
<version>1.3.1-SNAPSHOT</version>
</dependency></code></pre>
</li>
</ul>
</section>
<section><a id="Configuration"></a>
<h1>Configuration</h1>
<section><a id="Common"></a>
<h2>Common</h2>
<p>
By default, the <code>mybatis-freemarker.properties</code> file create in your classpath.
</p>
<ul>
<li>
You can define the base directory to search FreeMarker templates.
By default it is empty string, so you will need to provide full path to template every time.
<pre class="prettyprint"><code>
# 'template-file.base-dir' (kebab-case) can be used too
templateFile.baseDir = sql
</code></pre>
The <code>basePackage</code> property supported for keeping backward compatibility with old versions(1.1.x or under).
<pre class="prettyprint"><code>
# 'base-package' (kebab-case) can be used too
basePackage = sql
</code></pre>
</li>
<li>
You can customize the path provider of SQL provider class that return the SQL template file path.
For details see the <a href="#TemplateFilePathProvider">"TemplateFilePathProvider"</a>
<pre class="prettyprint"><code>
# templateFile.pathProvider.{name} = {value}
# 'template-file.path-provider' (kebab-case) can be used too
templateFile.pathProvider.cacheEnabled = false
</code></pre>
</li>
<li>
Since 1.2.0, you configure the FreeMarker configuration.
About available setting name, please refer to <a href="https://freemarker.apache.org/docs/api/freemarker/template/Configuration.html#setSetting-java.lang.String-java.lang.String-" class="externalLink">the API documentation of FreeMarker</a>.
<pre class="prettyprint"><code>
# freemarkerSettings.{name} = {value}
# 'freemarker-settings' (kebab-case) can be used too
freemarkerSettings.interpolation_syntax = dollar
</code></pre>
</li>
<li>
Since 1.2.0, you can use an any properties file or encoding as follow:
<pre class="prettyprint"><code>
$ java -Dmybatis-freemarker.config.file=mybatis-freemarker_production.properties -Dmybatis-freemarker.config.encoding=Windows-31J ...
</code></pre>
</li>
</ul>
</section>
<section><a id="Scripting_Language_Driver"></a>
<h2>Scripting Language Driver</h2>
<p>
You may need to do next steps:
</p>
<ul>
<li>Register the language driver alias in your mybatis configuration file:
<pre class="prettyprint"><code>
<configuration>
...
<typeAliases>
<typeAlias alias="freemarker" type="org.mybatis.scripting.freemarker.FreeMarkerLanguageDriver"/>
</typeAliases>
...
</configuration></code></pre>
</li>
<li>(Optional) Set the freemarker as your default scripting language:
<pre class="prettyprint"><code>
<configuration>
...
<settings>
<setting name="defaultScriptingLanguage" value="freemarker"/>
</settings>
...
</configuration></code></pre>
</li>
</ul>
</section>
</section>
<section><a id="Usage_in_annotations-driven_mappers"></a>
<h1>Usage in annotations-driven mappers</h1>
<p>Just write your queries using FreeMarker syntax:</p>
<pre class="prettyprint"><code>
@Lang(FreeMarkerLanguageDriver.class)
@Select("select * from names where id in (${ids?join(',')})")
List<Name> findNamesByIds(@Param("ids") List<Integer> ids);</code></pre>
<p>If any whitespace found inside <code>@Select</code> text, it is interpreted as inline script, not template name. It is convenient to avoid creating templates when script is really small. If you have a large SQL script, you can place it in distinct template and write next code:</p>
<pre class="prettyprint"><code>
@Lang(FreeMarkerLanguageDriver.class)
@Select("findName.ftl")
Name findName(@Param("n") String name);</code></pre>
<p>Template will be searched in classpath using <code>basePackage</code> property that has already been described above.</p>
<p><code>findName.ftl</code> content can be:</p>
<pre class="prettyprint"><code>
SELECT *
FROM names
where firstName = <@p name="n"/></code></pre>
<p><code><@p name="n"/></code> is a custom directive to generate <code>#{n}</code> markup.
This markup further will be passed into MyBatis engine, and it will replace this to <code>?</code>-parameter.
You can't write <code>#{paramName}</code> directly, because FreeMarker supports this syntax natively
(alghough it is deprecated). So, to get <code>?</code>-parameters to prepared statements works, you need
to use <code>${r"#{paramName}"}</code> verbose syntax, or this directive. By the way, in XML
files <code>${r"#{paramName}"}</code> is more preferrable because you don't need wrap it using
<code>CDATA</code> statements. In annotations and in external templates <code><@p/></code>
directive is more neat.</p>
</section>
<section><a id="Usage_in_XML-driven_mappers"></a>
<h1>Usage in XML-driven mappers</h1>
<p>As in annotations, you can write inline scripts or template names.</p>
<pre class="prettyprint"><code>
<!-- This is handled by FreeMarker too, because it is included into select nodes AS IS -->
<sql id="cols">id, ${r"firstName"}, lastName</sql>
<select id="findName" resultType="org.mybatis.scripting.freemarker.Name" lang="freemarker">
findName.ftl
</select>
<select id="findNamesByIds" resultType="org.mybatis.scripting.freemarker.Name" lang="freemarker">
select <include refid="cols"/> from names where id in (${ids?join(',')})
</select>
<!-- It is not very convenient - to use CDATA blocks. Better is to create external template
or use more verbose syntax: ${r"#{id}"}. -->
<select id="find" resultType="org.mybatis.scripting.freemarker.Name" lang="freemarker">
select * from names where id = <![CDATA[ <@p name='id'/>]]> and id = ${id}
</select></code></pre>
</section>
<section><a id="Prepared_statements_parameters"></a>
<h1>Prepared statements parameters</h1>
<p><code><@p/></code> directive can be used in two scenarios:</p>
<ul>
<li>To pass parameters to prepared statements AS IS:
<code><@p name='id'/></code> (will be translated to <code>#{id}</code>, and value already presents in parameter object)
</li>
<li>
To pass any value as prepared statements parameter
<code><@p value=someValue/></code> will be converted to <code>#{_p0}</code>, and
<code>_p0</code> parameter will be automatically added to parameters map.
It is convenient to use in loops like this:
<pre class="prettyprint"><code>
select * from names where firstName in (
<#list ids as id>
<@p value=id/>
<#if id_has_next>,</#if>
</#list>
)</code></pre>
This markup will be translated to
<pre class="prettyprint"><code>select * from names where firstName in (#{_p0}, #{_p1}, #{_p2})</code></pre>
and there are no need to care about escaping. All this stuff will be done automatically by JDBC driver.
Unfortunately, you can't use this syntax if passing one object as parameter and without
<code>@Param</code> annotation. The <code>UnsupportedOperationException</code> will be thrown.
It is because appending additional parameters to some object in general is very hard.
When you are using <code>@Param</code> annotated args, MyBatis will use <code>Map</code> to store parameters,
and it is easy to add some generated params. So, if you want to use auto-generated prepared
parameters, please don't forget about <code>@Param</code> annotation.
</li>
</ul>
</section>
<section><a id="TemplateFilePathProvider"></a>
<h1>TemplateFilePathProvider</h1>
<p>
The <code>TemplateFilePathProvider</code> is SQL provider class that return the SQL template file path(Available since 1.2.0, and <b>this class required to use on MyBatis 3.5.1+</b>).
This class use with SQL provider annotation(<code>@InsertProvider</code>, <code>@UpdateProvider</code>, <code>@DeleteProvider</code> and <code>@SelectProvider</code>) as follow:
</p>
<pre class="prettyprint"><code>
package com.example.mapper;
public interface BaseMapper<T> {
@Options(useGeneratedKeys = true, keyProperty = "id")
@InsertProvider(type = TemplateFilePathProvider.class)
void insert(T entity);
@UpdateProvider(type = TemplateFilePathProvider.class)
void update(T entity);
@DeleteProvider(type = TemplateFilePathProvider.class)
void delete(T entity);
@SelectProvider(type = TemplateFilePathProvider.class)
T findById(Integer id);
}
</code></pre>
<pre class="prettyprint"><code>
package com.example.mapper;
public interface NameMapper extends BaseMapper {
@SelectProvider(type = TemplateFilePathProvider.class)
List<Name> findByCondition(NameCondition condition);
}
</code></pre>
<p>
By default implementation, a template file path resolve following format and priority order.
If does not match all, it throw an exception that indicate not found a template file.
</p>
<ul>
<li>
<code>com/example/mapper/NameMapper/NameMapper-{methodName}-{databaseId}.ftl</code>
</li>
<li>
<code>com/example/mapper/NameMapper/NameMapper-{methodName}.ftl</code> (fallback using default database)
</li>
<li>
<code>com/example/mapper/BaseMapper/BaseMapper-{methodName}-{databaseId}.ftl</code> (fallback using declaring class of mapper method)
</li>
<li>
<code>com/example/mapper/BaseMapper/BaseMapper-{methodName}.ftl</code> (fallback using declaring class of mapper method and default database)
</li>
</ul>
<p>
If you want to customize the template file path format,
you can customize using following configuration properties that start with <code>templateFile.pathProvider</code>.
</p>
<ul>
<li>
You can specify the prefix for adding to template file path. Default value is "".
<pre class="prettyprint"><code>
# 'template-file.path-provider.prefix' (kebab-case) can be used too
templateFile.pathProvider.prefix = mapper
</code></pre>
</li>
<li>
You can specify whether includes package path part. Default value is <code>true</code>(= includes package path).
<pre class="prettyprint"><code>
# 'template-file.path-provider.includes-package-path' (kebab-case) can be used too
templateFile.pathProvider.includesPackagePath = false
</code></pre>
</li>
<li>
You can specify whether separate directory per mapper. Default value is <code>true</code>(= separate directory per mapper).
<pre class="prettyprint"><code>
# 'template-file.path-provider.separate-directory-per-mapper' (kebab-case) can be used too
templateFile.pathProvider.separateDirectoryPerMapper = false
</code></pre>
</li>
<li>
You can specify whether includes mapper name into file name when separate directory per mapper. Default value is <code>true</code>(= includes mapper name).
<pre class="prettyprint"><code>
# 'template-file.path-provider.includes-mapper-name-when-separate-directory' (kebab-case) can be used too
templateFile.pathProvider.includesMapperNameWhenSeparateDirectory = false
</code></pre>
</li>
<li>
You can specify whether cache a resolved template file path. Default value is <code>true</code>(= cache a resolved template file path).
<pre class="prettyprint"><code>
# 'template-file.path-provider.cache-enabled' (kebab-case) can be used too
templateFile.pathProvider.cacheEnabled = false
</code></pre>
</li>
</ul>
<p>
<span class="label important">TIPS</span><br />
Since the MyBatis 3.5.6, you can omit the type attribute at each annotations when specify the default provider class as global configuration as follow:<br />
Mapper interface:
</p>
<pre class="prettyprint"><code>
public interface NameMapper extends BaseMapper {
@SelectProvider
List<Name> findByCondition(NameCondition condition);
}
</code></pre>
Java based configuration:
<pre class="prettyprint"><code>
Configuration configuration = new Configuration();
configuration.setDefaultSqlProviderType(TemplateFilePathProvider.class);
</code></pre>
XML based configuration:
<pre class="prettyprint"><code>
<settings>
<setting name="defaultSqlProviderType" value="org.mybatis.scripting.freemarker.support.TemplateFilePathProvider"/>
</settings>
</code></pre>
</section>
<section><a id="Examples"></a>
<h1>Examples</h1>
<p>You can view full-featured example of configuring and of both XML-mapper and annotations-driven mapper usage
in <a href="https://github.com/mybatis/freemarker-scripting/tree/master/src/test" class="externalLink">test suite</a></p>
</section>
</main>
</div>
</div>
<hr/>
<footer>
<div class="container-fluid">
<div class="row-fluid">
<p>© 2015–2025
<a href="https://www.mybatis.org/">MyBatis.org</a>
</p>
</div>
</div>
</footer>
</body>
</html>