You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+4-170Lines changed: 4 additions & 170 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,178 +10,12 @@ MyBatis FreeMarker Support
10
10
11
11
MyBatis FreeMarker Scripting Support.
12
12
13
-
Getting started
14
-
===============
15
-
16
-
## Introduction
17
-
18
-
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.
19
-
20
-
If you are not familiar with FreeMarker syntax, you can view [Template Language Reference](http://freemarker.org/docs/ref.html)
21
-
22
-
## Install
23
-
24
-
mybatis-freemarker is available in Maven Central. So, if you are using maven, you can add this:
25
-
26
-
```xml
27
-
<dependencies>
28
-
<dependency>
29
-
<groupId>org.mybatis.scripting</groupId>
30
-
<artifactId>mybatis-freemarker</artifactId>
31
-
<version>1.1.2</version>
32
-
</dependency>
33
-
</dependencies>
34
-
```
35
-
36
-
If you are using gradle, you can use this snippet:
- Run `mvn install` to build and to automatically install it to your local maven repo
48
-
- Add maven dependency to your project
49
-
50
-
```xml
51
-
<dependency>
52
-
<groupId>org.mybatis.scripting</groupId>
53
-
<artifactId>mybatis-freemarker</artifactId>
54
-
<version>1.2.0-SNAPSHOT</version>
55
-
</dependency>
56
-
```
57
-
58
-
## Configuring
59
-
60
-
### Common
61
-
62
-
- (Optional) Create `mybatis-freemarker.properties` file in your classpath:
63
-
64
-
```
65
-
basePackage=sql
66
-
```
67
-
68
-
This will define base package to search FreeMarker templates. By default it is empty string, so you will need to provide full path to template every time.
69
-
70
-
### XML-driven mappers
71
-
72
-
If your are using annotations-driven mappers, you don't need to do anything more. If you are using XML-driven mappers too, you may need to do next steps:
73
-
74
-
- Register the language driver alias in your mybatis configuration file:
If any whitespace found inside `@Select` 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:
109
-
110
-
```java
111
-
@Lang(FreeMarkerLanguageDriver.class)
112
-
@Select("findName.ftl")
113
-
Name findName(@Param("n") String name);
114
-
```
115
-
116
-
Template will be searched in classpath using `basePackage` property that has already been described above.
117
-
118
-
`findName.ftl` content can be:
119
-
120
-
```
121
-
SELECT *
122
-
FROM names
123
-
where firstName = <@p name="n"/>
124
-
```
125
-
126
-
`<@p name="n"/>` is a custom directive to generate `#{n}` markup. This markup further will be passed into MyBatis engine, and it will replace this to `?`-parameter. You can't write `#{paramName}` directly, because FreeMarker supports this syntax natively (alghough it is deprecated). So, to get `?`-parameters to prepared statements works, you need to use `${r"#{paramName}"}` verbose syntax, or this directive. By the way, in XML files `${r"#{paramName}"}` is more preferrable because you don't need wrap it using `CDATA` statements. In annotations and in external templates `<@p/>` directive is more neat.
127
-
128
-
## Usage in XML-driven mappers
129
-
130
-
As in annotations, you can write inline scripts or template names.
131
-
132
-
```xml
133
-
<!-- This is handled by FreeMarker too, because it is included into select nodes AS IS -->
select * from names where id = <![CDATA[ <@p name='id'/> ]]> and id = ${id}
148
-
</select>
149
-
```
150
-
151
-
## Prepared statements parameters
152
-
153
-
`<@p/>` directive can be used in two scenarios:
154
-
155
-
- To pass parameters to prepared statements AS IS:
156
-
157
-
`<@p name='id'/>` (will be translated to `#{id}`, and value already presents in parameter object)
158
-
159
-
- To pass any value as prepared statements parameter
160
-
161
-
`<@p value=someValue/>` will be converted to `#{_p0}`, and `_p0` parameter will be automatically added to parameters map. It is convenient to use in loops like this:
162
-
163
-
```ftl
164
-
select * from names where firstName in (
165
-
<#list ids as id>
166
-
<@p value=id/>
167
-
<#if id_has_next>,</#if>
168
-
</#list>
169
-
)
170
-
```
171
-
172
-
This markup will be translated to
173
-
174
-
```sql
175
-
select*from names where firstName in (#{_p0}, #{_p1}, #{_p2})
176
-
```
177
-
178
-
and there are no need to care about escaping. All this stuff will be done automatically by JDBC driver.
179
-
180
-
Unfortunately, you can't use this syntax if passing one object as parameter and without `@Param` annotation. The `UnsupportedOperationException` will be thrown. It is because appending additional parameters to some object in general is very hard. When you are using `@Param` annotated args, MyBatis will use `Map` 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 `@Param` annotation.
13
+
Requirements
14
+
----------
181
15
182
-
## Examples
16
+
* master(under active development) : Version 1.2.x requires MyBatis 3.5+, FreeMarker 2.3.22+ and Java 8+
17
+
* 1.1.x(latest released line) : Version 1.1.2 requires MyBatis 3.4+, FreeMarker 2.3.22+ and Java 7+
183
18
184
-
You can view full-featured example of configuring and of both XML-mapper and annotations-driven mapper usage in [test suite](https://github.com/elw00d/mybatis-freemarker/tree/master/src/test)
0 commit comments